@@ -7,11 +7,7 @@ use ast_grep_language::{
77 Alias , Bash , CSharp , Cpp , Css , Elixir , Go , Haskell , Html , Java , JavaScript , Json , Kotlin , Lua ,
88 Php , Python , Ruby , Rust , Scala , Swift , Tsx , TypeScript , Yaml , C ,
99} ;
10- use schemars:: {
11- gen:: SchemaGenerator ,
12- schema:: { InstanceType , RootSchema , Schema , SchemaObject } ,
13- schema_for, JsonSchema ,
14- } ;
10+ use schemars:: { json_schema, schema_for, JsonSchema , Schema , SchemaGenerator } ;
1511use serde_json:: { to_writer_pretty, Value } ;
1612
1713use std:: borrow:: Cow ;
@@ -65,117 +61,118 @@ fn generate_lang_schema<T: LanguageExt + Alias>(lang: T, name: &str) -> Result<(
6561 to_writer_pretty ( & mut file, & schema) . context ( "cannot print JSON schema" )
6662}
6763
68- fn tweak_schema ( schema : & mut RootSchema ) -> Result < ( ) > {
64+ fn tweak_schema ( schema : & mut Schema ) -> Result < ( ) > {
6965 // better schema name
70- schema. schema . metadata ( ) . title = Some ( "ast-grep rule" . to_string ( ) ) ;
71- // stopby's rule does not need to be nested
72- simplify_stop_by ( schema) ?;
73- // using rule/relation will be too noisy
74- let description = remove_recursive_rule_relation_description ( schema) ?;
75- // set description to rule
76- let props = & mut schema. schema . object ( ) . properties ;
77- let Schema :: Object ( rule) = props. get_mut ( "rule" ) . context ( "must have rule" ) ? else {
78- bail ! ( "rule's type is not object!" ) ;
79- } ;
80- rule. metadata ( ) . description = description;
66+ // schema.schema.metadata().title = Some("ast-grep rule".to_string());
67+ // // stopby's rule does not need to be nested
68+ // simplify_stop_by(schema)?;
69+ // // using rule/relation will be too noisy
70+ // let description = remove_recursive_rule_relation_description(schema)?;
71+ // // set description to rule
72+ // let props = &mut schema.schema.object().properties;
73+ // let Schema::Object(rule) = props.get_mut("rule").context("must have rule")? else {
74+ // bail!("rule's type is not object!");
75+ // };
76+ // rule.metadata().description = description;
8177 Ok ( ( ) )
8278}
8379
8480fn add_lang_info_to_schema < T : LanguageExt + Alias > (
85- schema : & mut RootSchema ,
81+ schema : & mut Schema ,
8682 lang : T ,
8783 name : & str ,
8884) -> Result < ( ) > {
89- schema. schema . metadata ( ) . title = Some ( format ! ( "ast-grep rule for {name}" ) ) ;
90- let definitions = & mut schema. definitions ;
91- let Schema :: Object ( relation) = definitions
92- . get_mut ( "Relation" )
93- . context ( "must have relation" ) ?
94- else {
95- bail ! ( "Relation's type is not object!" ) ;
96- } ;
97- let relation_props = & mut relation. object ( ) . properties ;
98- let Schema :: Object ( field) = relation_props. get_mut ( "field" ) . context ( "must have field" ) ? else {
99- bail ! ( "field's type is not object!" )
100- } ;
101- field. enum_values = Some ( get_fields ( & lang. get_ts_language ( ) ) ) ;
102- let Schema :: Object ( kind) = relation_props. get_mut ( "kind" ) . context ( "must have kind" ) ? else {
103- bail ! ( "kind's type is not object!" )
104- } ;
105- let named_nodes = get_named_nodes ( & lang. get_ts_language ( ) ) ;
106- kind. enum_values = Some ( named_nodes. clone ( ) ) ;
107- let Schema :: Object ( serializable_rule) = definitions
108- . get_mut ( "SerializableRule" )
109- . context ( "must have SerializableRule" ) ?
110- else {
111- bail ! ( "SerializableRule's type is not object!" ) ;
112- } ;
113- let serializable_rule_props = & mut serializable_rule. object ( ) . properties ;
114- let Schema :: Object ( kind) = serializable_rule_props
115- . get_mut ( "kind" )
116- . context ( "must have kind" ) ?
117- else {
118- bail ! ( "kind's type is not object!" )
119- } ;
120- kind. enum_values = Some ( named_nodes) ;
121- let Schema :: Object ( language) = definitions
122- . get_mut ( "Language" )
123- . context ( "must have Language" ) ?
124- else {
125- bail ! ( "Language's type is not an object!" )
126- } ;
127- language. enum_values = Some (
128- T :: ALIAS
129- . iter ( )
130- . map ( |alias| serde_json:: Value :: String ( alias. to_string ( ) ) )
131- . chain ( std:: iter:: once ( serde_json:: Value :: String ( format ! (
132- "{lang}"
133- ) ) ) )
134- . collect ( ) ,
135- ) ;
85+ // schema.schema.metadata().title = Some(format!("ast-grep rule for {name}"));
86+ // let definitions = &mut schema.definitions;
87+ // let Schema::Object(relation) = definitions
88+ // .get_mut("Relation")
89+ // .context("must have relation")?
90+ // else {
91+ // bail!("Relation's type is not object!");
92+ // };
93+ // let relation_props = &mut relation.object().properties;
94+ // let Schema::Object(field) = relation_props.get_mut("field").context("must have field")? else {
95+ // bail!("field's type is not object!")
96+ // };
97+ // field.enum_values = Some(get_fields(&lang.get_ts_language()));
98+ // let Schema::Object(kind) = relation_props.get_mut("kind").context("must have kind")? else {
99+ // bail!("kind's type is not object!")
100+ // };
101+ // let named_nodes = get_named_nodes(&lang.get_ts_language());
102+ // kind.enum_values = Some(named_nodes.clone());
103+ // let Schema::Object(serializable_rule) = definitions
104+ // .get_mut("SerializableRule")
105+ // .context("must have SerializableRule")?
106+ // else {
107+ // bail!("SerializableRule's type is not object!");
108+ // };
109+ // let serializable_rule_props = &mut serializable_rule.object().properties;
110+ // let Schema::Object(kind) = serializable_rule_props
111+ // .get_mut("kind")
112+ // .context("must have kind")?
113+ // else {
114+ // bail!("kind's type is not object!")
115+ // };
116+ // kind.enum_values = Some(named_nodes);
117+ // let Schema::Object(language) = definitions
118+ // .get_mut("Language")
119+ // .context("must have Language")?
120+ // else {
121+ // bail!("Language's type is not an object!")
122+ // };
123+ // language.enum_values = Some(
124+ // T::ALIAS
125+ // .iter()
126+ // .map(|alias| serde_json::Value::String(alias.to_string()))
127+ // .chain(std::iter::once(serde_json::Value::String(format!(
128+ // "{lang}"
129+ // ))))
130+ // .collect(),
131+ // );
136132 Ok ( ( ) )
137133}
138134
139- fn remove_recursive_rule_relation_description ( schema : & mut RootSchema ) -> Result < Option < String > > {
140- let definitions = & mut schema. definitions ;
141- let Schema :: Object ( relation) = definitions
142- . get_mut ( "Relation" )
143- . context ( "must have relation" ) ?
144- else {
145- bail ! ( "Relation's type is not object!" ) ;
146- } ;
147- relation. metadata ( ) . description = None ;
148- let Schema :: Object ( rule) = definitions
149- . get_mut ( "SerializableRule" )
150- . context ( "must have rule" ) ?
151- else {
152- bail ! ( "SerializableRule's type is not object!" ) ;
153- } ;
154- Ok ( rule. metadata ( ) . description . take ( ) )
135+ fn remove_recursive_rule_relation_description ( schema : & mut Schema ) -> Result < Option < String > > {
136+ // let definitions = &mut schema.definitions;
137+ // let Schema::Object(relation) = definitions
138+ // .get_mut("Relation")
139+ // .context("must have relation")?
140+ // else {
141+ // bail!("Relation's type is not object!");
142+ // };
143+ // relation.metadata().description = None;
144+ // let Schema::Object(rule) = definitions
145+ // .get_mut("SerializableRule")
146+ // .context("must have rule")?
147+ // else {
148+ // bail!("SerializableRule's type is not object!");
149+ // };
150+ // Ok(rule.metadata().description.take())
151+ Ok ( None )
155152}
156153
157- fn simplify_stop_by ( schema : & mut RootSchema ) -> Result < ( ) > {
158- let definitions = & mut schema. definitions ;
159- let Schema :: Object ( stop_by) = definitions
160- . get_mut ( "SerializableStopBy" )
161- . context ( "must have stopby" ) ?
162- else {
163- bail ! ( "StopBy's type is not object!" ) ;
164- } ;
165- let one_ofs = stop_by
166- . subschemas ( )
167- . one_of
168- . as_mut ( )
169- . context ( "should have one_of" ) ?;
170- let Schema :: Object ( rule) = & mut one_ofs[ 1 ] else {
171- bail ! ( "type is not object!" ) ;
172- } ;
173- let rule = rule
174- . object ( )
175- . properties
176- . remove ( "rule" )
177- . context ( "should have rule" ) ?;
178- one_ofs[ 1 ] = rule;
154+ fn simplify_stop_by ( schema : & mut Schema ) -> Result < ( ) > {
155+ // let definitions = &mut schema.definitions;
156+ // let Schema::Object(stop_by) = definitions
157+ // .get_mut("SerializableStopBy")
158+ // .context("must have stopby")?
159+ // else {
160+ // bail!("StopBy's type is not object!");
161+ // };
162+ // let one_ofs = stop_by
163+ // .subschemas()
164+ // .one_of
165+ // .as_mut()
166+ // .context("should have one_of")?;
167+ // let Schema::Object(rule) = &mut one_ofs[1] else {
168+ // bail!("type is not object!");
169+ // };
170+ // let rule = rule
171+ // .object()
172+ // .properties
173+ // .remove("rule")
174+ // .context("should have rule")?;
175+ // one_ofs[1] = rule;
179176 Ok ( ( ) )
180177}
181178
@@ -213,19 +210,18 @@ fn get_fields(lang: &TSLanguage) -> Vec<Value> {
213210struct PlaceholderLang ;
214211// reference: https://github.com/GREsau/schemars/blob/9415fcb57b85f12e07afeb1dd16184bab0e26a84/schemars/src/json_schema_impls/primitives.rs#L8
215212impl JsonSchema for PlaceholderLang {
216- fn schema_id ( ) -> std :: borrow :: Cow < ' static , str > {
213+ fn schema_id ( ) -> Cow < ' static , str > {
217214 Cow :: Borrowed ( "Language" )
218215 }
219- fn schema_name ( ) -> String {
220- String :: from ( "Language" )
216+ fn schema_name ( ) -> Cow < ' static , str > {
217+ Cow :: Borrowed ( "Language" )
221218 }
222219 fn json_schema ( _gen : & mut SchemaGenerator ) -> Schema {
223- SchemaObject {
224- instance_type : Some ( InstanceType :: String . into ( ) ) ,
225- format : None ,
226- ..Default :: default ( )
227- }
228- . into ( )
220+ json_schema ! ( {
221+ "type" : "string" ,
222+ "description" : "Placeholder for language, used in JSON schema only." ,
223+ "example" : "typescript"
224+ } )
229225 }
230226}
231227
0 commit comments