11import ArgumentParser
22import Foundation
33
4- class Order {
5- let field : Field
4+ final class Order : ArgumentParser . ExpressibleByArgument {
5+ let field : String
66 let direction : Direction
77
8- init ( field: Field , direction: Direction ) {
9- self . field = field
10- self . direction = direction
8+ public init ? ( argument: String ) {
9+ let o = Order . parse ( s: argument. lowercased ( ) )
10+ if o != nil {
11+ field = o!. field
12+ direction = o!. direction
13+ } else {
14+ return nil
15+ }
1116 }
1217
13- func sort( events: [ Event ] ) -> [ Event ] {
14- if field == . start {
15- if direction == . asc {
16- } else { }
17- }
18- return events
18+ init ( field: String , direction: Direction ) {
19+ self . field = field
20+ self . direction = direction
1921 }
2022
2123 static func parse( s: String ) -> Order ? {
2224 let ss : [ String ] = s. split ( separator: " : " ) . map { String ( $0) }
2325 switch ss. count {
2426 // found a string candidate for field only
2527 case 1 :
26- return parseField ( ss [ 0 ] ) . map {
28+ return parseField ( s : ss [ 0 ] ) . map {
2729 Order ( field: $0, direction: Direction . asc)
2830 }
2931 // found a string candidates for field and direction
3032 case 2 :
31- switch ( parseField ( ss [ 0 ] ) , Direction ( rawValue: ss [ 1 ] ) ) {
33+ switch ( parseField ( s : ss [ 0 ] ) , Direction ( rawValue: ss [ 1 ] ) ) {
3234 case let ( . some( f) , . some( d) ) :
3335 return Order ( field: f, direction: d)
3436 default :
@@ -39,13 +41,31 @@ class Order {
3941 }
4042 }
4143
42- private static func parseField( _: String ) -> Field ? {
43- nil
44- }
45- }
44+ private static let event =
45+ Event (
46+ id: UUID ( ) . uuidString,
47+ calendar: PlanCalendar . Unknown,
48+ title: Title ( text: " empty " ) ,
49+ schedule: Schedule (
50+ now: Date ( ) ,
51+ startDate: Date ( ) ,
52+ endDate: Date ( ) ,
53+ allDay: false
54+ ) ,
55+ location: " " ,
56+ meeting: Meeting ( organizer: " " , attendees: [ ] ) ,
57+ services: [ : ] ,
58+ tags: [ ]
59+ )
4660
47- enum Field : String , ExpressibleByArgument {
48- case start
61+ private static func parseField( s: String ) -> String ? {
62+ do {
63+ try Object . valueForKeyPath ( event, s)
64+ return s
65+ } catch {
66+ return nil
67+ }
68+ }
4969}
5070
5171enum Direction : String , ExpressibleByArgument {
0 commit comments