Move around util fn and set up enum build fn mod.
This commit is contained in:
parent
0842690e6f
commit
42cc6720d1
@ -1,10 +1,10 @@
|
|||||||
use crate::build_fn_gen::make_build_fn_name;
|
|
||||||
use crate::spec::{
|
use crate::spec::{
|
||||||
BooleanBuild, BuildSpec, ChildSpec, EnumBuildSpec, EnumRule, LeafEnumBuildSpec, LeafEnumRule,
|
BooleanBuild, BuildSpec, ChildSpec, EnumBuildSpec, EnumRule, LeafEnumBuildSpec, LeafEnumRule,
|
||||||
LeafEnumRuleBuild, SingleBooleanChildToBuild, SingleChild, SingleChildToBuild,
|
LeafEnumRuleBuild, SingleBooleanChildToBuild, SingleChild, SingleChildToBuild,
|
||||||
SingleLiteralChildToBuild, SingleTypeChildToBuild, SkipChild, StructBuildSpec, VecChild,
|
SingleLiteralChildToBuild, SingleTypeChildToBuild, SkipChild, StructBuildSpec, VecChild,
|
||||||
VecChildToBuild, VecTypeChildToBuild,
|
VecChildToBuild, VecTypeChildToBuild,
|
||||||
};
|
};
|
||||||
|
use crate::util::make_build_fn_name;
|
||||||
use convert_case::{Case, Casing};
|
use convert_case::{Case, Casing};
|
||||||
use yaml_rust2::{Yaml, YamlLoader};
|
use yaml_rust2::{Yaml, YamlLoader};
|
||||||
|
|
||||||
|
|||||||
6
ast-generator/src/enum_build_fn.rs
Normal file
6
ast-generator/src/enum_build_fn.rs
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
use proc_macro2::TokenStream;
|
||||||
|
use crate::spec::EnumBuildSpec;
|
||||||
|
|
||||||
|
pub fn make_enum_build_fn(enum_build_spec: &EnumBuildSpec) -> TokenStream {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
@ -1,14 +1,17 @@
|
|||||||
pub mod deserialize;
|
pub mod deserialize;
|
||||||
mod build_fn_gen;
|
mod enum_build_fn;
|
||||||
mod spec;
|
mod spec;
|
||||||
|
mod struct_build_fn;
|
||||||
mod type_gen;
|
mod type_gen;
|
||||||
|
mod util;
|
||||||
|
|
||||||
use crate::build_fn_gen::make_struct_build_fn;
|
use crate::struct_build_fn::make_struct_build_fn;
|
||||||
use crate::type_gen::make_type;
|
use crate::type_gen::make_type;
|
||||||
use proc_macro2::TokenStream;
|
use proc_macro2::TokenStream;
|
||||||
use quote::quote;
|
use quote::quote;
|
||||||
use spec::BuildSpec;
|
use spec::BuildSpec;
|
||||||
use syn::File;
|
use syn::File;
|
||||||
|
use crate::enum_build_fn::make_enum_build_fn;
|
||||||
|
|
||||||
fn debug_built_spec(build_spec: &BuildSpec, token_stream: &TokenStream) {
|
fn debug_built_spec(build_spec: &BuildSpec, token_stream: &TokenStream) {
|
||||||
println!("*** BuildSpec ***");
|
println!("*** BuildSpec ***");
|
||||||
@ -18,7 +21,7 @@ fn debug_built_spec(build_spec: &BuildSpec, token_stream: &TokenStream) {
|
|||||||
}
|
}
|
||||||
BuildSpec::LeafEnum(leaf_enum_build_spec) => {
|
BuildSpec::LeafEnum(leaf_enum_build_spec) => {
|
||||||
println!("Spec name: {}", leaf_enum_build_spec.name());
|
println!("Spec name: {}", leaf_enum_build_spec.name());
|
||||||
},
|
}
|
||||||
BuildSpec::Struct(struct_build_spec) => {
|
BuildSpec::Struct(struct_build_spec) => {
|
||||||
println!("Spec name: {}", struct_build_spec.name());
|
println!("Spec name: {}", struct_build_spec.name());
|
||||||
}
|
}
|
||||||
@ -30,7 +33,7 @@ fn debug_built_spec(build_spec: &BuildSpec, token_stream: &TokenStream) {
|
|||||||
|
|
||||||
pub struct AstGeneratedFile {
|
pub struct AstGeneratedFile {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub contents: String
|
pub contents: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn token_stream_to_string(token_stream: TokenStream) -> String {
|
fn token_stream_to_string(token_stream: TokenStream) -> String {
|
||||||
@ -39,16 +42,21 @@ fn token_stream_to_string(token_stream: TokenStream) -> String {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn generate_build_file(build_specs: &[BuildSpec]) -> AstGeneratedFile {
|
fn generate_build_file(build_specs: &[BuildSpec]) -> AstGeneratedFile {
|
||||||
let build_fns = build_specs.iter()
|
let build_fns = build_specs
|
||||||
.map(|build_spec| {
|
.iter()
|
||||||
match build_spec {
|
.map(|build_spec| match build_spec {
|
||||||
BuildSpec::Enum(enum_build_spec) => { quote! {} }
|
BuildSpec::Enum(enum_build_spec) => {
|
||||||
BuildSpec::LeafEnum(leaf_enum_build_spec) => { quote! {} }
|
let stream = make_enum_build_fn(enum_build_spec);
|
||||||
BuildSpec::Struct(struct_build_spec) => {
|
debug_built_spec(build_spec, &stream);
|
||||||
let struct_build_fn_stream = make_struct_build_fn(struct_build_spec);
|
stream
|
||||||
debug_built_spec(build_spec, &struct_build_fn_stream);
|
}
|
||||||
struct_build_fn_stream
|
BuildSpec::LeafEnum(leaf_enum_build_spec) => {
|
||||||
}
|
todo!()
|
||||||
|
}
|
||||||
|
BuildSpec::Struct(struct_build_spec) => {
|
||||||
|
let struct_build_fn_stream = make_struct_build_fn(struct_build_spec);
|
||||||
|
debug_built_spec(build_spec, &struct_build_fn_stream);
|
||||||
|
struct_build_fn_stream
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
@ -57,12 +65,13 @@ fn generate_build_file(build_specs: &[BuildSpec]) -> AstGeneratedFile {
|
|||||||
};
|
};
|
||||||
AstGeneratedFile {
|
AstGeneratedFile {
|
||||||
name: String::from("build.rs"),
|
name: String::from("build.rs"),
|
||||||
contents: token_stream_to_string(combined)
|
contents: token_stream_to_string(combined),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn generate_node_file(build_specs: &[BuildSpec]) -> AstGeneratedFile {
|
fn generate_node_file(build_specs: &[BuildSpec]) -> AstGeneratedFile {
|
||||||
let types = build_specs.iter()
|
let types = build_specs
|
||||||
|
.iter()
|
||||||
.map(|build_spec| make_type(build_spec))
|
.map(|build_spec| make_type(build_spec))
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
let combined = quote! {
|
let combined = quote! {
|
||||||
@ -70,12 +79,15 @@ fn generate_node_file(build_specs: &[BuildSpec]) -> AstGeneratedFile {
|
|||||||
};
|
};
|
||||||
AstGeneratedFile {
|
AstGeneratedFile {
|
||||||
name: String::from("node.rs"),
|
name: String::from("node.rs"),
|
||||||
contents: token_stream_to_string(combined)
|
contents: token_stream_to_string(combined),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn generate_files(build_specs: &[BuildSpec]) -> Vec<AstGeneratedFile> {
|
pub fn generate_files(build_specs: &[BuildSpec]) -> Vec<AstGeneratedFile> {
|
||||||
vec![generate_build_file(build_specs), generate_node_file(build_specs)]
|
vec![
|
||||||
|
generate_build_file(build_specs),
|
||||||
|
generate_node_file(build_specs),
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn test_dump() -> String {
|
pub fn test_dump() -> String {
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
use crate::build_fn_gen::make_build_fn_name;
|
use crate::util::make_build_fn_name;
|
||||||
use convert_case::{Case, Casing};
|
use convert_case::{Case, Casing};
|
||||||
|
|
||||||
pub enum BuildSpec {
|
pub enum BuildSpec {
|
||||||
@ -352,7 +352,7 @@ impl SingleTypeChildToBuild {
|
|||||||
optional,
|
optional,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new(
|
pub fn new(
|
||||||
build: &str,
|
build: &str,
|
||||||
var_name: &str,
|
var_name: &str,
|
||||||
|
|||||||
@ -6,10 +6,6 @@ use convert_case::{Case, Casing};
|
|||||||
use proc_macro2::{Ident, TokenStream};
|
use proc_macro2::{Ident, TokenStream};
|
||||||
use quote::{format_ident, quote};
|
use quote::{format_ident, quote};
|
||||||
|
|
||||||
pub fn make_build_fn_name(s: &str) -> String {
|
|
||||||
format!("build_{}", s.to_case(Case::Snake))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn make_vec_child_holder(vec_child: &VecChild) -> TokenStream {
|
fn make_vec_child_holder(vec_child: &VecChild) -> TokenStream {
|
||||||
let (child_ident, child_type_ident) = match vec_child.build() {
|
let (child_ident, child_type_ident) = match vec_child.build() {
|
||||||
VecChildToBuild::Type(vec_type_child) => (
|
VecChildToBuild::Type(vec_type_child) => (
|
||||||
@ -199,7 +195,7 @@ fn make_child_arg(child_spec: &ChildSpec) -> Option<TokenStream> {
|
|||||||
SingleChildToBuild::Long(literal_child) => {
|
SingleChildToBuild::Long(literal_child) => {
|
||||||
let child_ident = get_literal_child_ident(literal_child);
|
let child_ident = get_literal_child_ident(literal_child);
|
||||||
Some(quote! { #child_ident.unwrap() })
|
Some(quote! { #child_ident.unwrap() })
|
||||||
},
|
}
|
||||||
SingleChildToBuild::Double(literal_child) => {
|
SingleChildToBuild::Double(literal_child) => {
|
||||||
let child_ident = get_literal_child_ident(literal_child);
|
let child_ident = get_literal_child_ident(literal_child);
|
||||||
Some(quote! { #child_ident.unwrap() })
|
Some(quote! { #child_ident.unwrap() })
|
||||||
@ -164,7 +164,7 @@ fn make_struct_type(build_spec: &StructBuildSpec) -> TokenStream {
|
|||||||
pub fn make_type(build_spec: &BuildSpec) -> TokenStream {
|
pub fn make_type(build_spec: &BuildSpec) -> TokenStream {
|
||||||
match build_spec {
|
match build_spec {
|
||||||
BuildSpec::Enum(enum_build_spec) => make_enum_type(enum_build_spec),
|
BuildSpec::Enum(enum_build_spec) => make_enum_type(enum_build_spec),
|
||||||
BuildSpec::LeafEnum(leaf_enum_build_spec) => quote! {},
|
BuildSpec::LeafEnum(leaf_enum_build_spec) => todo!(),
|
||||||
BuildSpec::Struct(struct_build_spec) => make_struct_type(struct_build_spec),
|
BuildSpec::Struct(struct_build_spec) => make_struct_type(struct_build_spec),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
5
ast-generator/src/util.rs
Normal file
5
ast-generator/src/util.rs
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
use convert_case::{Case, Casing};
|
||||||
|
|
||||||
|
pub fn make_build_fn_name(s: &str) -> String {
|
||||||
|
format!("build_{}", s.to_case(Case::Snake))
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user