11use css:: optimize_value:: optimize_value;
2- use schemars:: { JsonSchema , SchemaGenerator , json_schema} ;
3- use schemars:: Schema ;
42use serde:: { Deserialize , Deserializer , Serialize } ;
53use serde_json:: Value ;
64use std:: collections:: { BTreeMap , HashMap } ;
75
8- /// Schema helper for typography values that accept string or number
9- fn typography_value_schema ( _generator : & mut SchemaGenerator ) -> Schema {
10- json_schema ! ( {
11- "oneOf" : [
12- { "type" : "string" } ,
13- { "type" : "number" } ,
14- { "type" : "null" }
15- ]
16- } )
17- }
18-
196/// ColorEntry stores both the original key (for TypeScript interface) and CSS key (for CSS variables)
207#[ derive( Debug , Clone , Serialize ) ]
218pub struct ColorEntry {
@@ -39,42 +26,6 @@ pub struct ColorTheme {
3926 entries : HashMap < String , ColorEntry > ,
4027}
4128
42- impl JsonSchema for ColorTheme {
43- fn schema_name ( ) -> std:: borrow:: Cow < ' static , str > {
44- std:: borrow:: Cow :: Borrowed ( "ColorTheme" )
45- }
46-
47- fn json_schema ( _generator : & mut SchemaGenerator ) -> Schema {
48- json_schema ! ( {
49- "type" : "object" ,
50- "additionalProperties" : {
51- "oneOf" : [
52- { "type" : "string" , "description" : "Color value (e.g., '#000', 'rgb(0,0,0)')" } ,
53- {
54- "type" : "object" ,
55- "additionalProperties" : {
56- "$ref" : "#/$defs/ColorValue"
57- } ,
58- "description" : "Nested color object"
59- }
60- ]
61- } ,
62- "description" : "Color theme with color name to value mappings. Supports nested objects for color scales." ,
63- "$defs" : {
64- "ColorValue" : {
65- "oneOf" : [
66- { "type" : "string" } ,
67- {
68- "type" : "object" ,
69- "additionalProperties" : { "$ref" : "#/$defs/ColorValue" }
70- }
71- ]
72- }
73- }
74- } )
75- }
76- }
77-
7829/// Recursively flatten a JSON value into ColorEntry list
7930/// interface_prefix uses dots, css_prefix uses dashes
8031fn flatten_color_value (
@@ -194,17 +145,15 @@ where
194145 StringOrNumber :: Float ( n) => Ok ( Some ( n. to_string ( ) ) ) ,
195146 }
196147}
197- #[ derive( Serialize , Deserialize , Debug , JsonSchema ) ]
148+ #[ derive( Serialize , Deserialize , Debug ) ]
198149#[ serde( rename_all = "camelCase" ) ]
199150pub struct Typography {
200151 pub font_family : Option < String > ,
201152 pub font_size : Option < String > ,
202153
203154 #[ serde( deserialize_with = "deserialize_string_from_number" , default ) ]
204- #[ schemars( schema_with = "typography_value_schema" ) ]
205155 pub font_weight : Option < String > ,
206156 #[ serde( deserialize_with = "deserialize_string_from_number" , default ) ]
207- #[ schemars( schema_with = "typography_value_schema" ) ]
208157 pub line_height : Option < String > ,
209158 pub letter_spacing : Option < String > ,
210159}
@@ -235,82 +184,6 @@ impl From<Vec<Option<Typography>>> for Typographies {
235184 }
236185}
237186
238- impl JsonSchema for Typographies {
239- fn schema_name ( ) -> std:: borrow:: Cow < ' static , str > {
240- std:: borrow:: Cow :: Borrowed ( "Typographies" )
241- }
242-
243- fn json_schema ( generator : & mut SchemaGenerator ) -> Schema {
244- let typography_schema = generator. subschema_for :: < Typography > ( ) ;
245- json_schema ! ( {
246- "oneOf" : [
247- {
248- "type" : "array" ,
249- "items" : {
250- "oneOf" : [
251- typography_schema,
252- { "type" : "null" }
253- ]
254- } ,
255- "description" : "Traditional array format: array of Typography objects or null for each breakpoint"
256- } ,
257- {
258- "type" : "object" ,
259- "properties" : {
260- "fontFamily" : {
261- "oneOf" : [
262- { "type" : "string" } ,
263- { "type" : "array" , "items" : { "oneOf" : [ { "type" : "string" } , { "type" : "null" } ] } } ,
264- { "type" : "null" }
265- ]
266- } ,
267- "fontStyle" : {
268- "oneOf" : [
269- { "type" : "string" } ,
270- { "type" : "array" , "items" : { "oneOf" : [ { "type" : "string" } , { "type" : "null" } ] } } ,
271- { "type" : "null" }
272- ]
273- } ,
274- "fontWeight" : {
275- "oneOf" : [
276- { "type" : "string" } ,
277- { "type" : "number" } ,
278- { "type" : "array" , "items" : { "oneOf" : [ { "type" : "string" } , { "type" : "number" } , { "type" : "null" } ] } } ,
279- { "type" : "null" }
280- ]
281- } ,
282- "fontSize" : {
283- "oneOf" : [
284- { "type" : "string" } ,
285- { "type" : "array" , "items" : { "oneOf" : [ { "type" : "string" } , { "type" : "null" } ] } } ,
286- { "type" : "null" }
287- ]
288- } ,
289- "lineHeight" : {
290- "oneOf" : [
291- { "type" : "string" } ,
292- { "type" : "number" } ,
293- { "type" : "array" , "items" : { "oneOf" : [ { "type" : "string" } , { "type" : "number" } , { "type" : "null" } ] } } ,
294- { "type" : "null" }
295- ]
296- } ,
297- "letterSpacing" : {
298- "oneOf" : [
299- { "type" : "string" } ,
300- { "type" : "array" , "items" : { "oneOf" : [ { "type" : "string" } , { "type" : "null" } ] } } ,
301- { "type" : "null" }
302- ]
303- }
304- } ,
305- "additionalProperties" : false ,
306- "description" : "Compact object format: each property can be a single value or array of values per breakpoint"
307- }
308- ] ,
309- "description" : "Typography definition supporting both traditional array format and compact object format"
310- } )
311- }
312- }
313-
314187/// Helper to deserialize a typography property that can be either a single value or an array
315188fn deserialize_typo_prop ( value : & Value ) -> Result < Vec < Option < String > > , String > {
316189 match value {
@@ -460,28 +333,17 @@ impl<'de> Deserialize<'de> for Typographies {
460333 }
461334}
462335
463- #[ derive( Serialize , Deserialize , Debug , JsonSchema ) ]
336+ #[ derive( Serialize , Deserialize , Debug ) ]
464337#[ serde( rename_all = "camelCase" ) ]
465338pub struct Theme {
466- /// Color themes by mode name (e.g., "light", "dark")
467339 #[ serde( default ) ]
468340 pub colors : BTreeMap < String , ColorTheme > ,
469- /// Breakpoints in pixels for responsive typography (default: [0, 480, 768, 992, 1280, 1600])
470341 #[ serde( default = "default_breakpoints" ) ]
471342 pub breakpoints : Vec < u16 > ,
472- /// Typography definitions by name (e.g., "h1", "body", "caption")
473343 #[ serde( default ) ]
474344 pub typography : BTreeMap < String , Typographies > ,
475345}
476346
477- /// Root devup.json configuration
478- #[ derive( Serialize , Deserialize , Debug , JsonSchema ) ]
479- pub struct DevupJson {
480- /// Theme configuration including colors, typography, and breakpoints
481- #[ serde( default ) ]
482- pub theme : Option < Theme > ,
483- }
484-
485347fn default_breakpoints ( ) -> Vec < u16 > {
486348 vec ! [ 0 , 480 , 768 , 992 , 1280 , 1600 ]
487349}
0 commit comments