Skip to content

Commit a68b5b9

Browse files
committed
Implement list item delimiter attribute
1 parent 80588e9 commit a68b5b9

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

Sources/FoundationEssentials/AttributedString/FoundationAttributes.swift

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ extension AttributeScopes {
5757
public let presentationIntent: PresentationIntentAttribute
5858
@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
5959
public let markdownSourcePosition: MarkdownSourcePositionAttribute
60+
@available(FoundationPreview 6.2, *)
61+
public let listItemDelimiter: ListItemDelimiterAttribute
6062

6163
@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
6264
public let localizedStringArgumentAttributes: LocalizedStringArgumentAttributes
@@ -440,6 +442,44 @@ extension AttributeScopes.FoundationAttributes {
440442
public typealias Value = AttributedString.MarkdownSourcePosition
441443
}
442444

445+
@frozen
446+
@available(FoundationPreview 6.2, *)
447+
public enum ListItemDelimiterAttribute : CodableAttributedStringKey, ObjectiveCConvertibleAttributedStringKey {
448+
public typealias Value = Character
449+
public typealias ObjectiveCValue = NSString
450+
451+
public static let name = NSAttributedString.Key.listItemDelimiter.rawValue
452+
453+
public static func objectiveCValue(for value: Character) throws -> NSString {
454+
String(value) as NSString
455+
}
456+
457+
public static func value(for object: NSString) throws -> Character {
458+
let stringValue = object as String
459+
guard stringValue.count == 1 else {
460+
throw CocoaError(.coderInvalidValue)
461+
}
462+
return stringValue[stringValue.startIndex]
463+
}
464+
465+
public static func encode(_ value: Character, to encoder: any Encoder) throws {
466+
var container = encoder.singleValueContainer()
467+
try container.encode(String(value))
468+
}
469+
470+
public static func decode(from decoder: any Decoder) throws -> Character {
471+
let container = try decoder.singleValueContainer()
472+
let text = try container.decode(String.self)
473+
guard text.count == 1 else {
474+
throw DecodingError.dataCorrupted(DecodingError.Context(
475+
codingPath: container.codingPath,
476+
debugDescription: "List item delimeter encoded value must contain only one character / grapheme cluster"
477+
))
478+
}
479+
return text[text.startIndex]
480+
}
481+
}
482+
443483
#endif // FOUNDATION_FRAMEWORK
444484

445485
@frozen

0 commit comments

Comments
 (0)