@@ -11,22 +11,34 @@ pub enum ClauseType {
11
11
Delete ,
12
12
}
13
13
14
- impl From < & str > for ClauseType {
15
- fn from ( value : & str ) -> Self {
14
+ impl TryFrom < & str > for ClauseType {
15
+ type Error = String ;
16
+
17
+ fn try_from ( value : & str ) -> Result < Self , Self :: Error > {
16
18
match value {
17
- "select" => Self :: Select ,
18
- "where" => Self :: Where ,
19
- "from" => Self :: From ,
20
- "update" => Self :: Update ,
21
- "delete" => Self :: Delete ,
22
- _ => panic ! ( "Unimplemented ClauseType: {}" , value) ,
19
+ "select" => Ok ( Self :: Select ) ,
20
+ "where" => Ok ( Self :: Where ) ,
21
+ "from" => Ok ( Self :: From ) ,
22
+ "update" => Ok ( Self :: Update ) ,
23
+ "delete" => Ok ( Self :: Delete ) ,
24
+ _ => {
25
+ let message = format ! ( "Unimplemented ClauseType: {}" , value) ;
26
+
27
+ // Err on tests, so we notice that we're lacking an implementation immediately.
28
+ if cfg ! ( test) {
29
+ panic ! ( "{}" , message) ;
30
+ }
31
+
32
+ return Err ( message) ;
33
+ }
23
34
}
24
35
}
25
36
}
26
37
27
- impl From < String > for ClauseType {
28
- fn from ( value : String ) -> Self {
29
- ClauseType :: from ( value. as_str ( ) )
38
+ impl TryFrom < String > for ClauseType {
39
+ type Error = String ;
40
+ fn try_from ( value : String ) -> Result < ClauseType , Self :: Error > {
41
+ ClauseType :: try_from ( value. as_str ( ) )
30
42
}
31
43
}
32
44
@@ -93,7 +105,7 @@ impl<'a> CompletionContext<'a> {
93
105
let current_node_kind = current_node. kind ( ) ;
94
106
95
107
match previous_node_kind {
96
- "statement" => self . wrapping_clause_type = Some ( current_node_kind. into ( ) ) ,
108
+ "statement" => self . wrapping_clause_type = current_node_kind. try_into ( ) . ok ( ) ,
97
109
"invocation" => self . is_invocation = true ,
98
110
99
111
_ => { }
@@ -112,7 +124,7 @@ impl<'a> CompletionContext<'a> {
112
124
113
125
// in Treesitter, the Where clause is nested inside other clauses
114
126
"where" => {
115
- self . wrapping_clause_type = Some ( "where" . into ( ) ) ;
127
+ self . wrapping_clause_type = "where" . try_into ( ) . ok ( ) ;
116
128
}
117
129
118
130
_ => { }
@@ -184,7 +196,7 @@ mod tests {
184
196
185
197
let ctx = CompletionContext :: new ( & params) ;
186
198
187
- assert_eq ! ( ctx. wrapping_clause_type, Some ( expected_clause. into ( ) ) ) ;
199
+ assert_eq ! ( ctx. wrapping_clause_type, expected_clause. try_into ( ) . ok ( ) ) ;
188
200
}
189
201
}
190
202
0 commit comments