1- //! Leetcode data models  
1+ //! Leetcode data modelsA  
22use  colored:: Colorize ; 
3- use  serde:: Serialize ; 
3+ use  serde:: { Serialize ,  Deserialize } ; 
4+ pub  use  self :: question:: * ; 
45use  super :: schemas:: problems; 
56
67/// Problem model 
@@ -16,7 +17,7 @@ pub struct Problem {
1617    pub  percent :  f32 , 
1718    pub  slug :  String , 
1819    pub  starred :  bool , 
19-     pub  state :  String , 
20+     pub  status :  String , 
2021} 
2122
2223static  DONE :  & ' static  str  = " ✔" ; 
@@ -34,9 +35,9 @@ impl std::fmt::Display for Problem {
3435        let  mut  level = "" . normal ( ) ; 
3536
3637        if  self . locked  {  lock = LOCK  } ; 
37-         if  self . state  == "ac" . to_string ( )  { 
38+         if  self . status  == "ac" . to_string ( )  { 
3839            done = DONE . green ( ) . bold ( ) ; 
39-         }  else  if  self . state  == "notac"  { 
40+         }  else  if  self . status  == "notac"  { 
4041            done = NDONE . green ( ) . bold ( ) ; 
4142        } 
4243
@@ -90,48 +91,144 @@ impl std::fmt::Display for Problem {
9091    } 
9192} 
9293
93- 
94- /// Description Model 
95- pub  struct  DescData  { 
96-     pub  question :  Question 
97- } 
98- 
99- /// desc.question 
94+ /// desc model 
95+ #[ derive( Debug ,  Default ,  Serialize ,  Deserialize ) ]  
10096pub  struct  Question  { 
101-     pub  content :  String , 
102-     pub  stat :  QuestionStat , 
103-     pub  code_defintion :  Vec < CodeDefintion > , 
104-     pub  sample_text_case :  String , 
105-     pub  enable_run_code :  bool , 
106-     pub  meta_data :  MetaData , 
107-     pub  translated_cotent :  String 
97+     pub  content :  Option < String > , 
98+     #[ serde( deserialize_with = "string_struct" ) ]  
99+     pub  stats :  Stats , 
100+     #[ serde( alias = "codeDefinition" ,  deserialize_with = "string_struct" ) ]  
101+     pub  defs :  CodeDefintion , 
102+     #[ serde( alias = "sampleTestCase" ) ]  
103+     pub  case :  String , 
104+     #[ serde( alias = "metaData" ,  deserialize_with = "string_struct" ) ]  
105+     pub  metadata :  MetaData , 
106+     #[ serde( alias = "enableRunCode" ) ]  
107+     pub  test :  bool , 
108+     #[ serde( alias = "translatedContent" ) ]  
109+     pub  t_content :  Option < String > , 
108110} 
109111
110- pub  struct  QuestionStat  { 
111-     pub  total_accepted :  String , 
112-     pub  total_submission :  String , 
113-     pub  total_accepted_aw :  i64 , 
114-     pub  total_submission_raw :  i64 , 
115-     pub  ac_rate :  String 
116- } 
112+ /// deps of Question 
113+ mod  question { 
114+     use  crate :: err:: Error ; 
115+     use  serde:: { 
116+         Serialize , 
117+         Deserialize , 
118+         Deserializer , 
119+         de:: { 
120+             self , 
121+             Visitor 
122+         } 
123+     } ; 
124+     use  std:: { 
125+         fmt, 
126+         str:: FromStr , 
127+         marker:: PhantomData , 
128+     } ; 
117129
118- pub  struct  CodeDefintion  { 
119-     pub  value :  String , 
120-     pub  text :  String , 
121-     pub  default_code :  String , 
122- } 
130+     /// Code samples 
131+      #[ derive( Debug ,  Default ,  Serialize ,  Deserialize ) ]  
132+     pub  struct  CodeDefintion ( pub  Vec < CodeDefintionInner > ) ; 
123133
124- pub  struct  MetaData  { 
125-     pub  name :  String , 
126-     pub  params :  Vec < Param > , 
127-     pub  r#return :  Return , 
128- } 
134+     /// CodeDefinition Inner struct 
135+      #[ derive( Debug ,  Default ,  Serialize ,  Deserialize ) ]  
136+     pub  struct  CodeDefintionInner  { 
137+         pub  value :  String , 
138+         pub  text :  String , 
139+         #[ serde( alias = "defaultCode" ) ]  
140+         pub  code :  String , 
141+     } 
129142
130- pub  struct  Param  { 
131-     pub  name :  String , 
132-     pub  r#type :  String 
133- } 
143+     /// Question status 
144+      #[ derive( Debug ,  Default ,  Serialize ,  Deserialize ) ]  
145+     pub  struct  Stats  { 
146+         #[ serde( alias = "totalAccepted" ) ]  
147+         tac :  String , 
148+         #[ serde( alias = "totalSubmission" ) ]  
149+         tsm :  String , 
150+         #[ serde( alias = "totalAcceptedRaw" ) ]  
151+         tacr :  i32 , 
152+         #[ serde( alias = "totalSubmissionRaw" ) ]  
153+         tsmr :  i32 , 
154+         #[ serde( alias = "acRate" ) ]  
155+         rate :  String 
156+     } 
157+     
158+     /// Algorithm metadata 
159+      #[ derive( Debug ,  Default ,  Serialize ,  Deserialize ) ]  
160+     pub  struct  MetaData  { 
161+         pub  name :  String , 
162+         pub  params :  Vec < Param > , 
163+         pub  r#return :  Return , 
164+     } 
165+ 
166+     /// Deserialize CodedeFintion from str 
167+      impl  std:: str:: FromStr  for  CodeDefintion  { 
168+         type  Err  = Error ; 
169+ 
170+         fn  from_str ( s :  & str )  -> Result < Self ,  Self :: Err >  { 
171+             Ok ( serde_json:: from_str ( s) ?) 
172+         } 
173+     } 
174+ 
175+     /// Deserialize Stats from str 
176+      impl  std:: str:: FromStr  for  Stats  { 
177+         type  Err  = crate :: err:: Error ; 
134178
135- pub  struct  Return  { 
136-     pub  r#type :  String 
179+         fn  from_str ( s :  & str )  -> Result < Self ,  Self :: Err >  { 
180+             Ok ( serde_json:: from_str ( s) ?) 
181+         } 
182+     } 
183+ 
184+     /// Deserialize MetaData from str 
185+      impl  std:: str:: FromStr  for  MetaData  { 
186+         type  Err  = Error ; 
187+ 
188+         fn  from_str ( s :  & str )  -> Result < Self ,  Self :: Err >  { 
189+             Ok ( serde_json:: from_str ( s) ?) 
190+         } 
191+     } 
192+ 
193+     /// MetaData nested fields 
194+      #[ derive( Debug ,  Default ,  Serialize ,  Deserialize ) ]  
195+     pub  struct  Param  { 
196+         pub  name :  String , 
197+         pub  r#type :  String 
198+     } 
199+ 
200+     /// MetaData nested fields 
201+      #[ derive( Debug ,  Default ,  Serialize ,  Deserialize ) ]  
202+     pub  struct  Return  { 
203+         pub  r#type :  String 
204+     } 
205+ 
206+     /// Master serde_json 
207+      pub  fn  string_struct < ' de ,  T ,  D > ( deserializer :  D )  -> Result < T ,  D :: Error > 
208+     where 
209+         T :  Deserialize < ' de >  + FromStr < Err  = Error > , 
210+         D :  Deserializer < ' de > , 
211+     { 
212+         struct  StringStruct < T > ( PhantomData < fn ( )  -> T > ) ; 
213+         impl < ' de ,  T >  Visitor < ' de >  for  StringStruct < T > 
214+         where 
215+             T :  Deserialize < ' de >  + FromStr < Err  = Error > , 
216+         { 
217+             type  Value  = T ; 
218+             
219+             fn  expecting ( & self ,  formatter :  & mut  fmt:: Formatter )  -> fmt:: Result  { 
220+                 formatter. write_str ( "string" ) 
221+             } 
222+             
223+             fn  visit_str < E > ( self ,  value :  & str )  -> Result < T ,  E > 
224+             where 
225+                 E :  de:: Error , 
226+             { 
227+                 Ok ( FromStr :: from_str ( value) . unwrap ( ) ) 
228+             } 
229+         } 
230+ 
231+         
232+         deserializer. deserialize_str ( StringStruct ( PhantomData ) ) 
233+     } 
137234} 
0 commit comments