refactor: reduce coupling
This commit is contained in:
54
evaluator/nixjit_macros/src/ir.rs
Normal file
54
evaluator/nixjit_macros/src/ir.rs
Normal file
@@ -0,0 +1,54 @@
|
||||
use syn::{
|
||||
FieldsNamed, Ident, Token, Type, parenthesized,
|
||||
parse::{Parse, ParseStream, Result},
|
||||
punctuated::Punctuated,
|
||||
token,
|
||||
};
|
||||
|
||||
pub enum VariantInput {
|
||||
Unit(Ident),
|
||||
Tuple(Ident, Type),
|
||||
Struct(Ident, FieldsNamed),
|
||||
}
|
||||
|
||||
pub struct MacroInput {
|
||||
pub base_name: Ident,
|
||||
pub variants: Punctuated<VariantInput, Token![,]>,
|
||||
}
|
||||
|
||||
impl Parse for VariantInput {
|
||||
fn parse(input: ParseStream) -> Result<Self> {
|
||||
let name: Ident = input.parse()?;
|
||||
|
||||
if input.peek(token::Paren) {
|
||||
let content;
|
||||
parenthesized!(content in input);
|
||||
let ty: Type = content.parse()?;
|
||||
|
||||
if !content.is_empty() {
|
||||
return Err(content.error("Expected a single type inside parentheses"));
|
||||
}
|
||||
|
||||
Ok(VariantInput::Tuple(name, ty))
|
||||
} else if input.peek(token::Brace) {
|
||||
let fields: FieldsNamed = input.parse()?;
|
||||
|
||||
Ok(VariantInput::Struct(name, fields))
|
||||
} else {
|
||||
Ok(VariantInput::Unit(name))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Parse for MacroInput {
|
||||
fn parse(input: ParseStream) -> Result<Self> {
|
||||
let base_name = input.parse()?;
|
||||
input.parse::<Token![,]>()?;
|
||||
let variants = Punctuated::parse_terminated(input)?;
|
||||
|
||||
Ok(MacroInput {
|
||||
base_name,
|
||||
variants,
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user