Skip to content

Commit 8f91b62

Browse files
committed
Intermediate checkpoint.
1 parent e893c6e commit 8f91b62

File tree

9 files changed

+83
-110
lines changed

9 files changed

+83
-110
lines changed

Sources/DiffableTextStylesXNumeric/Helpers/Adapter.swift

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import Foundation
1515

1616
@usableFromInline struct Adapter<Format: NumericTextFormat>: Equatable {
1717
@usableFromInline typealias Scheme = Format.NumericTextScheme
18+
@usableFromInline typealias Value = Format.FormatInput
1819

1920
//=------------------------------------------------------------------------=
2021
// MARK: State
@@ -30,15 +31,26 @@ import Foundation
3031
@inlinable init(_ format: Format) {
3132
self.format = format
3233
self.scheme = format.scheme()
33-
assert(format.locale == scheme.locale)
3434
}
3535

3636
//=------------------------------------------------------------------------=
3737
// MARK: Accessors
3838
//=------------------------------------------------------------------------=
3939

4040
@inlinable var locale: Locale {
41-
scheme.locale
41+
format.locale
42+
}
43+
44+
//=------------------------------------------------------------------------=
45+
// MARK: Defaults
46+
//=------------------------------------------------------------------------=
47+
48+
@inlinable func bounds() -> Bounds<Value> {
49+
scheme.bounds(Value.self)
50+
}
51+
52+
@inlinable func precision() -> Precision<Value> {
53+
scheme.precision(Value.self)
4254
}
4355

4456
//=------------------------------------------------------------------------=

Sources/DiffableTextStylesXNumeric/Helpers/Label.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ import Foundation
3737
//=------------------------------------------------------------------------=
3838

3939
/// Correctness is assert by tests parsing currency formats for all locale-currency pairs.
40-
@inlinable static func currency(_ lexicon: Lexicon, code: String) -> Label {
40+
@inlinable static func currency(_ locale: Locale, code: String, lexicon: Lexicon) -> Label {
4141
let labels = IntegerFormatStyle<Int>
42-
.Currency(code: code).locale(lexicon.locale)
42+
.Currency(code: code).locale(locale)
4343
.precision(.fractionLength(0)).format(0)
4444
.split(separator: lexicon.digits[.zero],
4545
omittingEmptySubsequences: false)

Sources/DiffableTextStylesXNumeric/Helpers/Lexicon.swift

Lines changed: 12 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,24 @@ public final class Lexicon {
2020
// MARK: Constants
2121
//=------------------------------------------------------------------------=
2222

23-
@usableFromInline static let en_US: Lexicon = .ascii()
23+
@usableFromInline static let en_US = Locale(identifier: "en_US")
24+
@usableFromInline static let ascii = Lexicon(
25+
signs: .ascii(), digits: .ascii(), separators: .ascii())
2426

2527
//=------------------------------------------------------------------------=
2628
// MARK: State
2729
//=------------------------------------------------------------------------=
2830

29-
@usableFromInline let locale: Locale
30-
@usableFromInline let signs: Links<Sign>
31-
@usableFromInline let digits: Links<Digit>
31+
@usableFromInline let signs: Links<Sign>
32+
@usableFromInline let digits: Links<Digit>
3233
@usableFromInline let separators: Links<Separator>
3334

3435
//=------------------------------------------------------------------------=
3536
// MARK: Initializers
3637
//=------------------------------------------------------------------------=
3738

38-
@inlinable init(locale: Locale, signs: Links<Sign>,
39-
digits: Links<Digit>, separators: Links<Separator>) {
40-
self.locale = locale; self.signs = signs
41-
self.digits = digits; self.separators = separators
39+
@inlinable init(signs: Links<Sign>, digits: Links<Digit>, separators: Links<Separator>) {
40+
self.signs = signs; self.digits = digits; self.separators = separators
4241
}
4342
}
4443

@@ -52,38 +51,12 @@ extension Lexicon {
5251
// MARK: Initializers
5352
//=------------------------------------------------------------------------=
5453

55-
@inlinable static func ascii() -> Lexicon {
56-
let formatter = NumberFormatter()
57-
formatter.numberStyle = .decimal
58-
formatter.locale = Locale(identifier: "en_US")
59-
//=--------------------------------------=
60-
// MARK: Instantiate
61-
//=--------------------------------------=
62-
return .init(locale: formatter.locale,
63-
signs: .ascii(), digits: .ascii(), separators: .ascii())
54+
@inlinable static func standard(_ formatter: NumberFormatter) -> Lexicon {
55+
Lexicon(signs: .standard(formatter), digits: .standard(formatter), separators: .standard(formatter))
6456
}
6557

66-
@inlinable static func standard(_ locale: Locale) -> Lexicon {
67-
let formatter = NumberFormatter()
68-
formatter.numberStyle = .decimal
69-
formatter.locale = locale
70-
//=--------------------------------------=
71-
// MARK: Instantiate
72-
//=--------------------------------------=
73-
return .init(locale: locale, signs: .standard(formatter),
74-
digits: .standard(formatter), separators: .standard(formatter))
75-
}
76-
77-
@inlinable static func currency(_ locale: Locale, code: String) -> Lexicon {
78-
let formatter = NumberFormatter()
79-
formatter.numberStyle = .decimal
80-
formatter.locale = locale
81-
formatter.currencyCode = code
82-
//=--------------------------------------=
83-
// MARK: Instantiate
84-
//=--------------------------------------=
85-
return .init(locale: locale, signs: .currency(formatter),
86-
digits: .currency(formatter), separators: .currency(formatter))
58+
@inlinable static func currency(_ formatter: NumberFormatter) -> Lexicon {
59+
Lexicon(signs: .currency(formatter), digits: .currency(formatter), separators: .currency(formatter))
8760
}
8861
}
8962

@@ -99,7 +72,7 @@ extension Lexicon {
9972

10073
/// Relies on the fact that implemented styles can parse unformatted numbers.
10174
@inlinable func value<T>(of number: Number, as format: T) throws -> T.Value where T: Format {
102-
try format.locale(Self.en_US.locale).parse(number.characters())
75+
try format.locale(Self.en_US).parse(number.characters())
10376
}
10477

10578
//=------------------------------------------------------------------------=

Sources/DiffableTextStylesXNumeric/Helpers/Reader.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import DiffableTextKit
3636
// MARK: Accessors
3737
//=------------------------------------------------------------------------=
3838

39-
@inlinable var ascii: Lexicon { .en_US }
39+
@inlinable var ascii: Lexicon { .ascii }
4040
}
4141

4242
//=----------------------------------------------------------------------------=

Sources/DiffableTextStylesXNumeric/Helpers/Source.swift

Lines changed: 0 additions & 43 deletions
This file was deleted.

Sources/DiffableTextStylesXNumeric/Models/Scheme+Currency.swift

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,25 @@ import Foundation
2121
// MARK: State
2222
//=------------------------------------------------------------------------=
2323

24-
@usableFromInline let label: Label
25-
@usableFromInline let lexicon: Lexicon
26-
24+
@usableFromInline let label: Label
25+
@usableFromInline let lexicon: Lexicon
26+
@usableFromInline let defaults: Defaults
27+
2728
//=------------------------------------------------------------------------=
2829
// MARK: Initializers
2930
//=------------------------------------------------------------------------=
3031

3132
@inlinable init(_ key: ID) {
32-
self.lexicon = .currency(key.locale, code: key.code)
33-
self.label = .currency(lexicon, code: key.code)
33+
let formatter = NumberFormatter()
34+
formatter.numberStyle = .currency
35+
formatter.locale = key.locale
36+
formatter.currencyCode = key.code
37+
//=--------------------------------------=
38+
// MARK: Instantiate
39+
//=--------------------------------------=
40+
self.lexicon = .currency(formatter)
41+
self.label = .currency(key.locale, code: key.code, lexicon: lexicon)
42+
self.defaults = .init(formatter)
3443
}
3544

3645
//=------------------------------------------------------------------------=
@@ -72,4 +81,28 @@ import Foundation
7281
self.locale = locale; self.code = code
7382
}
7483
}
84+
85+
//*========================================================================*
86+
// MARK: * Defaults
87+
//*========================================================================*
88+
89+
@usableFromInline struct Defaults {
90+
91+
//=--------------------------------------------------------------------=
92+
// MARK: State
93+
//=--------------------------------------------------------------------=
94+
95+
@usableFromInline let fractionLimits: ClosedRange<Int>
96+
97+
//=--------------------------------------------------------------------=
98+
// MARK: Initializers
99+
//=--------------------------------------------------------------------=
100+
101+
@inlinable init(_ formatter: NumberFormatter) {
102+
self.fractionLimits =
103+
formatter.minimumFractionDigits ...
104+
formatter.maximumFractionDigits
105+
}
106+
107+
}
75108
}

Sources/DiffableTextStylesXNumeric/Models/Scheme+Standard.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,14 @@ import Foundation
2727
// MARK: Initializers
2828
//=------------------------------------------------------------------------=
2929

30-
@inlinable init(_ key: ID) {
31-
self.lexicon = .standard(key.locale)
30+
@inlinable init(_ identifier: ID) {
31+
let formatter = NumberFormatter()
32+
formatter.numberStyle = .decimal
33+
formatter.locale = identifier.locale
34+
//=--------------------------------------=
35+
// MARK: Instantiate
36+
//=--------------------------------------=
37+
self.lexicon = .standard(formatter)
3238
}
3339

3440
//=------------------------------------------------------------------------=

Sources/DiffableTextStylesXNumeric/Models/Scheme.swift

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,18 @@ public protocol NumericTextScheme {
3333
@inlinable var lexicon: Lexicon { get }
3434

3535
//=------------------------------------------------------------------------=
36-
// MARK: Autocorrect
36+
// MARK: Defaults
3737
//=------------------------------------------------------------------------=
3838

39-
@inlinable func autocorrect(_ snapshot: inout Snapshot)
40-
}
41-
42-
//=----------------------------------------------------------------------------=
43-
// MARK: + Details
44-
//=----------------------------------------------------------------------------=
45-
46-
extension NumericTextScheme {
39+
@inlinable func bounds<Value>(_ value: Value.Type) -> Bounds<Value>
40+
41+
@inlinable func precision<Value>(_ value: Value.Type) -> Precision<Value>
4742

4843
//=------------------------------------------------------------------------=
49-
// MARK: Accessors
44+
// MARK: Autocorrect
5045
//=------------------------------------------------------------------------=
5146

52-
@inlinable var locale: Locale {
53-
lexicon.locale
54-
}
47+
@inlinable func autocorrect(_ snapshot: inout Snapshot)
5548
}
5649

5750
//*============================================================================*

Sources/DiffableTextStylesXNumeric/Style.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,10 @@ public struct _NumericTextStyle<Format: NumericTextFormat>: DiffableTextStyle {
3232
// MARK: Initializers
3333
//=------------------------------------------------------------------------=
3434

35-
#warning("...")
3635
@inlinable init(_ format: Format) {
3736
self.adapter = Adapter(format)
38-
self.bounds = Bounds()
39-
self.precision = Precision()
37+
self.bounds = adapter.bounds()
38+
self.precision = adapter.precision()
4039
}
4140

4241
//=------------------------------------------------------------------------=

0 commit comments

Comments
 (0)