Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
raviqqe committed Dec 5, 2023
1 parent 60501da commit 36c1883
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 35 deletions.
40 changes: 5 additions & 35 deletions macro/src/dialect/input.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use proc_macro2::Ident;
use quote::format_ident;
mod input_field;

use self::input_field::InputField;
use std::ops::Deref;
use syn::{bracketed, parse::Parse, punctuated::Punctuated, LitStr, Token};
use syn::{parse::Parse, punctuated::Punctuated, Token};

pub struct DialectInput {
name: String,
Expand Down Expand Up @@ -40,7 +41,7 @@ impl Parse for DialectInput {
InputField::Name(field) => name = Some(field.value()),
InputField::TableGen(td) => table_gen = Some(td.value()),
InputField::TdFile(file) => td_file = Some(file.value()),
InputField::Includes(field) => {
InputField::IncludeDirectories(field) => {
includes = field.into_iter().map(|literal| literal.value()).collect()
}
}
Expand All @@ -54,34 +55,3 @@ impl Parse for DialectInput {
})
}
}

enum InputField {
Name(LitStr),
TableGen(LitStr),
TdFile(LitStr),
Includes(Punctuated<LitStr, Token![,]>),
}

impl Parse for InputField {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
let ident = input.parse::<Ident>()?;

input.parse::<Token![:]>()?;

if ident == format_ident!("name") {
Ok(Self::Name(input.parse()?))
} else if ident == format_ident!("table_gen") {
Ok(Self::TableGen(input.parse()?))
} else if ident == format_ident!("td_file") {
Ok(Self::TdFile(input.parse()?))
} else if ident == format_ident!("include_dirs") {
let content;
bracketed!(content in input);
Ok(Self::Includes(
Punctuated::<LitStr, Token![,]>::parse_terminated(&content)?,
))
} else {
Err(input.error(format!("invalid field {}", ident)))
}
}
}
34 changes: 34 additions & 0 deletions macro/src/dialect/input/input_field.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use proc_macro2::Ident;
use quote::format_ident;
use syn::{bracketed, parse::Parse, punctuated::Punctuated, LitStr, Token};

pub enum InputField {
Name(LitStr),
TableGen(LitStr),
TdFile(LitStr),
IncludeDirectories(Punctuated<LitStr, Token![,]>),
}

impl Parse for InputField {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
let ident = input.parse::<Ident>()?;

input.parse::<Token![:]>()?;

if ident == format_ident!("name") {
Ok(Self::Name(input.parse()?))
} else if ident == format_ident!("table_gen") {
Ok(Self::TableGen(input.parse()?))
} else if ident == format_ident!("td_file") {
Ok(Self::TdFile(input.parse()?))
} else if ident == format_ident!("include_dirs") {
let content;
bracketed!(content in input);
Ok(Self::IncludeDirectories(
Punctuated::<LitStr, Token![,]>::parse_terminated(&content)?,
))
} else {
Err(input.error(format!("invalid field {}", ident)))
}
}
}

0 comments on commit 36c1883

Please sign in to comment.