@@ -11,22 +11,34 @@ pub enum ClauseType {
1111 Delete ,
1212}
1313
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 > {
1618 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+ }
2334 }
2435 }
2536}
2637
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 ( ) )
3042 }
3143}
3244
@@ -93,7 +105,7 @@ impl<'a> CompletionContext<'a> {
93105 let current_node_kind = current_node. kind ( ) ;
94106
95107 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 ( ) ,
97109 "invocation" => self . is_invocation = true ,
98110
99111 _ => { }
@@ -112,7 +124,7 @@ impl<'a> CompletionContext<'a> {
112124
113125 // in Treesitter, the Where clause is nested inside other clauses
114126 "where" => {
115- self . wrapping_clause_type = Some ( "where" . into ( ) ) ;
127+ self . wrapping_clause_type = "where" . try_into ( ) . ok ( ) ;
116128 }
117129
118130 _ => { }
@@ -184,7 +196,7 @@ mod tests {
184196
185197 let ctx = CompletionContext :: new ( & params) ;
186198
187- assert_eq ! ( ctx. wrapping_clause_type, Some ( expected_clause. into ( ) ) ) ;
199+ assert_eq ! ( ctx. wrapping_clause_type, expected_clause. try_into ( ) . ok ( ) ) ;
188200 }
189201 }
190202
0 commit comments