1
1
import ArgumentParser
2
2
import Foundation
3
3
4
- class Order {
5
- let field : Field
4
+ final class Order : ArgumentParser . ExpressibleByArgument {
5
+ let field : String
6
6
let direction : Direction
7
7
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
+ }
11
16
}
12
17
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
19
21
}
20
22
21
23
static func parse( s: String ) -> Order ? {
22
24
let ss : [ String ] = s. split ( separator: " : " ) . map { String ( $0) }
23
25
switch ss. count {
24
26
// found a string candidate for field only
25
27
case 1 :
26
- return parseField ( ss [ 0 ] ) . map {
28
+ return parseField ( s : ss [ 0 ] ) . map {
27
29
Order ( field: $0, direction: Direction . asc)
28
30
}
29
31
// found a string candidates for field and direction
30
32
case 2 :
31
- switch ( parseField ( ss [ 0 ] ) , Direction ( rawValue: ss [ 1 ] ) ) {
33
+ switch ( parseField ( s : ss [ 0 ] ) , Direction ( rawValue: ss [ 1 ] ) ) {
32
34
case let ( . some( f) , . some( d) ) :
33
35
return Order ( field: f, direction: d)
34
36
default :
@@ -39,13 +41,31 @@ class Order {
39
41
}
40
42
}
41
43
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
+ )
46
60
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
+ }
49
69
}
50
70
51
71
enum Direction : String , ExpressibleByArgument {
0 commit comments