reduced methods visibility

This commit is contained in:
pkurchatov
2022-09-07 19:33:26 +03:00
parent 46b7a4ed2f
commit 1b6cb52bc9
24 changed files with 106 additions and 112 deletions
+1 -1
View File
@@ -10,7 +10,7 @@ public protocol DivBlockModeling {
public struct DivBlockModelingError: Error, CustomStringConvertible, Equatable {
public let description: String
public init(_ message: String, path: UIElementPath) {
init(_ message: String, path: UIElementPath) {
description = "\(message) [\(path)]"
DivKitLogger.error(description)
}
@@ -36,27 +36,27 @@ import Foundation
import CommonCore
/// Wrapper for Expression that works with any type of value
public struct AnyCalcExpression: CustomStringConvertible {
struct AnyCalcExpression: CustomStringConvertible {
private let expression: CalcExpression
private let describer: () -> String
@usableFromInline
let evaluator: () throws -> Any
/// Evaluator for individual symbols
public typealias SymbolEvaluator = (_ args: [Any]) throws -> Any
typealias SymbolEvaluator = (_ args: [Any]) throws -> Any
/// Symbols that make up an expression
public typealias Symbol = CalcExpression.Symbol
typealias Symbol = CalcExpression.Symbol
/// Runtime error when parsing or evaluating an expression
public typealias Error = CalcExpression.Error
typealias Error = CalcExpression.Error
/// Options for configuring an expression
public typealias Options = CalcExpression.Options
typealias Options = CalcExpression.Options
/// Constructor that accepts parsed expression and constants lookup function
/// Used in Yandex DivKit Expressions
public init(
init(
_ expression: ParsedCalcExpression,
options: Options = [.boolSymbols],
constants: @escaping (String) -> ExpressionResolverValueProvider?
@@ -78,7 +78,7 @@ public struct AnyCalcExpression: CustomStringConvertible {
/// Constructor that accepts parsed expression,
/// constants lookup function and SymbolEvaluators
public init(
init(
_ expression: ParsedCalcExpression,
options: Options = [.boolSymbols],
constants: @escaping (String) -> ExpressionResolverValueProvider?,
@@ -407,7 +407,7 @@ public struct AnyCalcExpression: CustomStringConvertible {
/// Evaluate the expression
@inlinable
public func evaluate<T>() throws -> T {
func evaluate<T>() throws -> T {
let anyValue = try evaluator()
guard let value: T = AnyCalcExpression.cast(anyValue) else {
switch T.self {
@@ -434,11 +434,11 @@ public struct AnyCalcExpression: CustomStringConvertible {
}
/// All symbols used in the expression
public var symbols: Set<Symbol> { expression.symbols }
var symbols: Set<Symbol> { expression.symbols }
/// Returns the optmized, pretty-printed expression if it was valid
/// Otherwise, returns the original (invalid) expression string
public var description: String { describer() }
var description: String { describer() }
}
// MARK: Internal API
@@ -655,15 +655,15 @@ extension AnyCalcExpression {
}
// Literal values
public static let nilValue = Double(bitPattern: nilBits)
public static let trueValue = Double(bitPattern: trueBits)
public static let falseValue = Double(bitPattern: falseBits)
static let nilValue = Double(bitPattern: nilBits)
static let trueValue = Double(bitPattern: trueBits)
static let falseValue = Double(bitPattern: falseBits)
// The values stored in the box
public var values = [Any]()
var values = [Any]()
// Store a value in the box
public func store(_ value: Any) throws -> CalcExpression.Value {
func store(_ value: Any) throws -> CalcExpression.Value {
switch value {
case let doubleValue as Double:
return .number(doubleValue)
@@ -38,14 +38,14 @@ import CommonCore
/// Immutable wrapper for a parsed expression
/// Reusing the same CalcExpression instance for multiple evaluations is more efficient
/// than creating a new one each time you wish to evaluate an expression string
public final class CalcExpression: CustomStringConvertible {
final class CalcExpression: CustomStringConvertible {
private let root: Subexpression
/// Evaluator for individual symbols
public typealias SymbolEvaluator = (_ args: [Value]) throws -> Value
typealias SymbolEvaluator = (_ args: [Value]) throws -> Value
/// Type representing the arity (number of arguments) accepted by a function
public enum Arity: ExpressibleByIntegerLiteral, CustomStringConvertible, Hashable {
enum Arity: ExpressibleByIntegerLiteral, CustomStringConvertible, Hashable {
/// An exact number of arguments
case exactly(Int)
@@ -53,15 +53,15 @@ public final class CalcExpression: CustomStringConvertible {
case atLeast(Int)
/// Any number of arguments
public static let any = atLeast(0)
static let any = atLeast(0)
/// ExpressibleByIntegerLiteral constructor
public init(integerLiteral value: Int) {
init(integerLiteral value: Int) {
self = .exactly(value)
}
/// The human-readable description of the arity
public var description: String {
var description: String {
switch self {
case let .exactly(value):
return "\(value) argument\(value == 1 ? "" : "s")"
@@ -72,17 +72,17 @@ public final class CalcExpression: CustomStringConvertible {
/// No-op Hashable implementation
/// Required to support custom Equatable implementation
public func hash(into _: inout Hasher) {}
func hash(into _: inout Hasher) {}
/// Equatable implementation
/// Note: this works more like a contains() function if
/// one side a range and the other is an exact value
/// This allows foo(x) to match foo(...) in a symbols dictionary
public static func ==(lhs: Arity, rhs: Arity) -> Bool {
static func ==(lhs: Arity, rhs: Arity) -> Bool {
lhs.matches(rhs)
}
public func matches(_ arity: Arity) -> Bool {
func matches(_ arity: Arity) -> Bool {
switch (self, arity) {
case let (.exactly(lhs), .exactly(rhs)),
let (.atLeast(lhs), .atLeast(rhs)):
@@ -95,7 +95,7 @@ public final class CalcExpression: CustomStringConvertible {
}
/// Symbols that make up an expression
public enum Symbol: CustomStringConvertible, Hashable {
enum Symbol: CustomStringConvertible, Hashable {
/// A named variable
case variable(String)
@@ -115,7 +115,7 @@ public final class CalcExpression: CustomStringConvertible {
case array(String)
/// The symbol name
public var name: String {
var name: String {
switch self {
case let .variable(name),
let .infix(name),
@@ -133,7 +133,7 @@ public final class CalcExpression: CustomStringConvertible {
}
/// The human-readable description of the symbol
public var description: String {
var description: String {
switch self {
case .variable:
return "variable \(escapedName)"
@@ -158,7 +158,7 @@ public final class CalcExpression: CustomStringConvertible {
}
/// Runtime error when parsing or evaluating an expression
public enum Error: Swift.Error, CustomStringConvertible, Equatable {
enum Error: Swift.Error, CustomStringConvertible, Equatable {
/// An application-specific error
case message(String)
@@ -178,10 +178,10 @@ public final class CalcExpression: CustomStringConvertible {
case arrayBounds(Symbol, Double)
/// Empty expression
public static let emptyExpression = unexpectedToken("")
static let emptyExpression = unexpectedToken("")
/// The human-readable description of the error
public var description: String {
var description: String {
switch self {
case let .message(message):
return message
@@ -220,23 +220,23 @@ public final class CalcExpression: CustomStringConvertible {
}
/// Options for configuring an expression
public struct Options: OptionSet {
struct Options: OptionSet {
/// Disable optimizations such as constant substitution
public static let noOptimize = Options(rawValue: 1 << 1)
static let noOptimize = Options(rawValue: 1 << 1)
/// Enable standard boolean operators and constants
public static let boolSymbols = Options(rawValue: 1 << 2)
static let boolSymbols = Options(rawValue: 1 << 2)
/// Assume all functions and operators in `symbols` are "pure", i.e.
/// they have no side effects, and always produce the same output
/// for a given set of arguments
public static let pureSymbols = Options(rawValue: 1 << 3)
static let pureSymbols = Options(rawValue: 1 << 3)
/// Packed bitfield of options
public let rawValue: Int
let rawValue: Int
/// Designated initializer
public init(rawValue: Int) {
init(rawValue: Int) {
self.rawValue = rawValue
}
}
@@ -245,7 +245,7 @@ public final class CalcExpression: CustomStringConvertible {
/// Allows for dynamic symbol lookup or generation without any performance overhead
/// Note that both math and boolean symbols are enabled by default - to disable them
/// return `{ _ in throw CalcExpression.Error.undefinedSymbol(symbol) }` from your lookup function
public init(
init(
_ expression: ParsedCalcExpression,
impureSymbols: (Symbol) throws -> SymbolEvaluator?,
pureSymbols: (Symbol) throws -> SymbolEvaluator? = { _ in nil }
@@ -271,7 +271,7 @@ public final class CalcExpression: CustomStringConvertible {
}
/// Verify that the string is a valid identifier
public static func isValidIdentifier(_ string: String) -> Bool {
static func isValidIdentifier(_ string: String) -> Bool {
var characters = UnicodeScalarView(string)
switch characters.parseIdentifier() ?? characters.parseEscapedIdentifier() {
case .symbol(.variable, _, _)?:
@@ -282,7 +282,7 @@ public final class CalcExpression: CustomStringConvertible {
}
/// Verify that the string is a valid operator
public static func isValidOperator(_ string: String) -> Bool {
static func isValidOperator(_ string: String) -> Bool {
var characters = UnicodeScalarView(string)
guard case let .symbol(symbol, _, _)? = characters.parseOperator(),
case let .infix(name) = symbol, name != "(", name != "["
@@ -295,7 +295,7 @@ public final class CalcExpression: CustomStringConvertible {
/// Parse an expression.
/// Returns an opaque struct that cannot be evaluated but can be queried
/// for symbols or used to construct an executable CalcExpression instance
public static func parse(_ expression: String) -> ParsedCalcExpression {
static func parse(_ expression: String) -> ParsedCalcExpression {
var characters = Substring.UnicodeScalarView(expression.unicodeScalars)
return parse(&characters)
}
@@ -306,7 +306,7 @@ public final class CalcExpression: CustomStringConvertible {
/// inside another string, e.g. for implementing string interpolation.
/// If no delimiter string is specified, the method will throw an error
/// if it encounters an unexpected token, but won't consume it
public static func parse(
static func parse(
_ input: inout Substring.UnicodeScalarView,
upTo delimiters: String...
) -> ParsedCalcExpression {
@@ -325,18 +325,18 @@ public final class CalcExpression: CustomStringConvertible {
/// Returns the optmized, pretty-printed expression if it was valid
/// Otherwise, returns the original (invalid) expression string
public var description: String { root.description }
var description: String { root.description }
/// All symbols used in the expression
public var symbols: Set<Symbol> { root.symbols }
var symbols: Set<Symbol> { root.symbols }
/// Evaluate the expression
public func evaluate() throws -> Value {
func evaluate() throws -> Value {
try root.evaluate()
}
/// Standard math symbols
public static let mathSymbols: [Symbol: SymbolEvaluator] = {
static let mathSymbols: [Symbol: SymbolEvaluator] = {
var symbols: [Symbol: SymbolEvaluator] = [:]
// constants
@@ -357,7 +357,7 @@ public final class CalcExpression: CustomStringConvertible {
}()
/// Standard boolean symbols
public static let boolSymbols: [Symbol: SymbolEvaluator] = {
static let boolSymbols: [Symbol: SymbolEvaluator] = {
var symbols: [Symbol: SymbolEvaluator] = [:]
// boolean constants
@@ -592,10 +592,10 @@ public struct ParsedCalcExpression: CustomStringConvertible {
public var description: String { root.description }
/// All symbols used in the expression
public var symbols: Set<CalcExpression.Symbol> { root.symbols }
var symbols: Set<CalcExpression.Symbol> { root.symbols }
/// Any error detected during parsing
public var error: CalcExpression.Error? {
var error: CalcExpression.Error? {
if case let .error(error, _) = root {
return error
}
@@ -765,57 +765,57 @@ private enum Subexpression: CustomStringConvertible {
// Workaround for horribly slow Substring.UnicodeScalarView perf
private struct UnicodeScalarView {
public typealias Index = String.UnicodeScalarView.Index
typealias Index = String.UnicodeScalarView.Index
private let characters: String.UnicodeScalarView
public private(set) var startIndex: Index
public private(set) var endIndex: Index
private(set) var startIndex: Index
private(set) var endIndex: Index
public init(_ unicodeScalars: String.UnicodeScalarView) {
init(_ unicodeScalars: String.UnicodeScalarView) {
characters = unicodeScalars
startIndex = characters.startIndex
endIndex = characters.endIndex
}
public init(_ unicodeScalars: Substring.UnicodeScalarView) {
init(_ unicodeScalars: Substring.UnicodeScalarView) {
self.init(String.UnicodeScalarView(unicodeScalars))
}
public init(_ string: String) {
init(_ string: String) {
self.init(string.unicodeScalars)
}
public var first: UnicodeScalar? {
var first: UnicodeScalar? {
isEmpty ? nil : characters[startIndex]
}
public var isEmpty: Bool {
var isEmpty: Bool {
startIndex >= endIndex
}
public subscript(_ index: Index) -> UnicodeScalar {
subscript(_ index: Index) -> UnicodeScalar {
characters[index]
}
public func index(after index: Index) -> Index {
func index(after index: Index) -> Index {
characters.index(after: index)
}
public func prefix(upTo index: Index) -> UnicodeScalarView {
func prefix(upTo index: Index) -> UnicodeScalarView {
var view = UnicodeScalarView(characters)
view.startIndex = startIndex
view.endIndex = index
return view
}
public func suffix(from index: Index) -> UnicodeScalarView {
func suffix(from index: Index) -> UnicodeScalarView {
var view = UnicodeScalarView(characters)
view.startIndex = index
view.endIndex = endIndex
return view
}
public mutating func popFirst() -> UnicodeScalar? {
mutating func popFirst() -> UnicodeScalar? {
if isEmpty {
return nil
}
@@ -12,7 +12,7 @@ public enum ExpressionError: Error {
case calculating(
expression: String,
scriptInject: String,
calcExpressionError: CalcExpression.Error
description: String
)
case validating(
expression: String,
@@ -242,11 +242,13 @@ extension ExpressionResolver {
).evaluate()
return validatedValue(value: result, validator: link.validator, rawValue: link.rawValue)
} catch let error as CalcExpression.Error {
errorTracker(.calculating(
expression: link.rawValue,
scriptInject: parsedExpression.description,
calcExpressionError: error
))
errorTracker(
.calculating(
expression: link.rawValue,
scriptInject: parsedExpression.description,
description: error.description
)
)
return nil
} catch {
errorTracker(.unknown(error: error))
@@ -272,11 +274,13 @@ extension ExpressionResolver {
).evaluate()
stringValue += value
} catch let error as CalcExpression.Error {
errorTracker(.calculating(
expression: link.rawValue,
scriptInject: parsedExpression.description,
calcExpressionError: error
))
errorTracker(
.calculating(
expression: link.rawValue,
scriptInject: parsedExpression.description,
description: error.description
)
)
return nil
} catch {
errorTracker(.unknown(error: error))
@@ -1,5 +1,5 @@
import Foundation
public protocol ExpressionResolverValueProvider {
protocol ExpressionResolverValueProvider {
func typedValue<T>() -> T?
}
+3 -3
View File
@@ -2,7 +2,7 @@ import Foundation
import CommonCoreTiny
public protocol Function {
protocol Function {
var arity: CalcExpression.Arity { get }
func invoke(args: [Any]) throws -> Any
func verify(signature: FunctionSignature) throws
@@ -209,7 +209,7 @@ struct OverloadedFunction: Function {
}
}
public struct FunctionSignature: Decodable {
struct FunctionSignature: Decodable {
let arguments: [Argument]
let resultType: ArgumentType
@@ -226,7 +226,7 @@ public struct FunctionSignature: Decodable {
}
}
public enum ArgumentType: String, Decodable, CaseIterable {
enum ArgumentType: String, Decodable, CaseIterable {
case string
case number
case integer
+1 -1
View File
@@ -3,7 +3,7 @@ import Foundation
import CommonCore
extension CalcExpression {
public enum Value {
enum Value {
case integer(Int)
case number(Double)
case datetime(Date)
@@ -18,7 +18,7 @@ public protocol DivActionBase: Serializable {
}
extension DivActionBase {
public func makeDivActionParams(
func makeDivActionParams(
cardId: DivCardID,
source: UserInterfaceAction.DivActionSource
) -> UserInterfaceAction.DivActionParams {
@@ -34,14 +34,14 @@ extension DivActionBase {
)
}
public func makeDivActionPayload(
func makeDivActionPayload(
cardId: DivCardID,
source: UserInterfaceAction.DivActionSource
) -> UserInterfaceAction.Payload? {
.divAction(params: makeDivActionParams(cardId: cardId, source: source))
}
public func makeJsonPayload() -> UserInterfaceAction.Payload? {
func makeJsonPayload() -> UserInterfaceAction.Payload? {
guard let payload = payload else {
return nil
}
@@ -29,11 +29,11 @@ extension DivContainer: ActionsHolder, DoubleTapActionsHolder, LongTapActionsHol
extension DivGrid: ActionsHolder, DoubleTapActionsHolder, LongTapActionsHolder {}
extension DivText.Ellipsis: ActionsHolder {
public var action: DivAction? { nil }
var action: DivAction? { nil }
}
extension DivText.Range: ActionsHolder {
public var action: DivAction? { nil }
var action: DivAction? { nil }
}
extension ActionHolder {
@@ -15,7 +15,7 @@ extension DivContainer: DivBlockModeling {
)
}
public func makeBaseBlock(context: DivBlockModelingContext) throws -> Block {
private func makeBaseBlock(context: DivBlockModelingContext) throws -> Block {
let childContext = modified(context) {
$0.parentPath = $0.parentPath + (id ?? DivContainer.type)
}
@@ -5,7 +5,7 @@ public struct DivCustomData {
public let data: [String: Any]
public let children: [Block]
public init(name: String, data: [String: Any], children: [Block] = []) {
init(name: String, data: [String: Any], children: [Block] = []) {
self.name = name
self.data = data
self.children = children
@@ -46,7 +46,7 @@ extension DivData: DivBlockModeling {
#endif
}
public func getCurrentState(stateManager: DivStateManager) -> DivData.State? {
private func getCurrentState(stateManager: DivStateManager) -> DivData.State? {
guard let item = stateManager.get(stateBlockPath: DivData.rootPath) else {
return states.first
}
@@ -4,7 +4,7 @@ import CommonCore
import LayoutKit
extension DivDrawable {
public func makeBlock(
func makeBlock(
context: DivBlockModelingContext,
widthTrait: DivDrawableWidthTrait = .fixed,
corners: CGRect.Corners
@@ -19,14 +19,14 @@ extension DivDrawable {
}
}
public func getWidth(context: DivBlockModelingContext) -> CGFloat {
func getWidth(context: DivBlockModelingContext) -> CGFloat {
switch self {
case let .divShapeDrawable(shapeDrawable):
return shapeDrawable.getWidth(context: context)
}
}
public func getHeight(context: DivBlockModelingContext) -> CGFloat {
func getHeight(context: DivBlockModelingContext) -> CGFloat {
switch self {
case let .divShapeDrawable(shapeDrawable):
return shapeDrawable.getHeight(context: context)
@@ -1,6 +1,6 @@
import Foundation
public enum DivDrawableWidthTrait {
enum DivDrawableWidthTrait {
case resizable
case fixed
}
@@ -4,7 +4,7 @@ import CommonCore
import LayoutKit
extension DivShapeDrawable {
public func makeBlock(
func makeBlock(
context: DivBlockModelingContext,
widthTrait: DivDrawableWidthTrait,
corners: CGRect.Corners
@@ -46,14 +46,14 @@ extension DivShapeDrawable {
}
}
public func getWidth(context: DivBlockModelingContext) -> CGFloat {
func getWidth(context: DivBlockModelingContext) -> CGFloat {
switch shape {
case let .divRoundedRectangleShape(rectangle):
return CGFloat(rectangle.itemWidth.resolveValue(context.expressionResolver) ?? 0)
}
}
public func getHeight(context: DivBlockModelingContext) -> CGFloat {
func getHeight(context: DivBlockModelingContext) -> CGFloat {
switch shape {
case let .divRoundedRectangleShape(rectangle):
let stroke = stroke?.resolveWidth(context.expressionResolver) ?? 0
@@ -3,7 +3,7 @@ import CoreGraphics
import LayoutKit
extension DivPivot {
public func makeAnchorValue(expressionResolver: ExpressionResolver) -> AnchorValue {
func makeAnchorValue(expressionResolver: ExpressionResolver) -> AnchorValue {
switch self {
case let .divPivotPercentage(value):
return .relative(value: value.resolveValue(expressionResolver) ?? 50)
@@ -1,9 +1,7 @@
import CoreGraphics
import LayoutKit
extension DivTransform {
public func makeAnchorPoint(expressionResolver: ExpressionResolver) -> LayoutKit.AnchorPoint {
func makeAnchorPoint(expressionResolver: ExpressionResolver) -> AnchorPoint {
AnchorPoint(
x: pivotX.makeAnchorValue(expressionResolver: expressionResolver),
y: pivotY.makeAnchorValue(expressionResolver: expressionResolver)
@@ -1,6 +1,6 @@
import XCTest
import DivKit
@testable import DivKit
final class ExpressionParsingTests: XCTestCase {
func test_ParseStringExpression() {
@@ -1,10 +1,8 @@
@testable import LayoutKit
@testable import DivKit
import XCTest
import CommonCore
import DivKit
final class DivContainerExtensionsTests: XCTestCase {
func test_WhenDivHasAction_CreatesBlockWithIt() throws {
let block = try makeBlock(fromFile: "with_action") as? DecoratingBlock
@@ -1,8 +1,8 @@
@testable import DivKit
import XCTest
import BaseUI
import CommonCore
import DivKit
import LayoutKit
final class DivGalleryExtensionsTests: XCTestCase {
@@ -1,10 +1,8 @@
@testable import DivKit
@testable import LayoutKit
import XCTest
import CommonCore
import DivKit
final class DivGifImageExtensionsTests: XCTestCase {
func test_WhenDivHasAction_CreatesBlockWithIt() throws {
let block = try makeBlock(fromFile: "with_action") as? DecoratingBlock
@@ -1,10 +1,8 @@
@testable import DivKit
@testable import LayoutKit
import XCTest
import CommonCore
import DivKit
final class DivImageExtensionsTests: XCTestCase {
func test_WhenDivHasAction_CreatesBlockWithIt() throws {
let block = try makeBlock(fromFile: "with_action") as? DecoratingBlock
@@ -1,10 +1,8 @@
@testable import DivKit
@testable import LayoutKit
import XCTest
import CommonCore
import DivKit
final class DivSeparatorExtensionsTests: XCTestCase {
func test_WhenDivHasAction_CreatesBlockWithIt() throws {
let block = try makeBlock(fromFile: "with_action") as? DecoratingBlock