Skip to content

Commit

Permalink
chore: cleanup schema
Browse files Browse the repository at this point in the history
  • Loading branch information
rzvxa committed Jul 30, 2024
1 parent 3f83a91 commit c9c8d22
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 20 deletions.
12 changes: 6 additions & 6 deletions tasks/ast_codegen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,11 @@ impl GeneratorOutput {
struct CodegenCtx {
ty_table: TypeTable,
ident_table: IdentTable,
schema: Vec<Schema>,
schema: Schema,
}

struct CodegenResult {
/// One schema per definition file
schema: Vec<Schema>,
schema: Schema,
outputs: Vec<(/* generator name */ &'static str, /* output */ GeneratorOutput)>,
}

Expand All @@ -126,9 +125,10 @@ impl CodegenCtx {
}
}

let mut me = Self { ty_table, ident_table, schema: Vec::default() }.link(linker)?;
let schema = mods.into_iter().map(Module::build).collect::<Result<Vec<_>>>()?;
_ = std::mem::replace(&mut me.schema, schema);
let mut me = Self { ty_table, ident_table, schema: Schema::default() }.link(linker)?;
for m in mods {
m.build_in(&mut me.schema)?;
}
Ok(me)
}

Expand Down
19 changes: 5 additions & 14 deletions tasks/ast_codegen/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,9 @@ use crate::{util::NormalizeError, TypeName};

use super::{parse_file, Itertools, PathBuf, Rc, Read, RefCell, Result, TypeDef, TypeRef};

#[derive(Debug, serde::Serialize)]
#[derive(Debug, Default, serde::Serialize)]
pub struct Schema {
source: PathBuf,
definitions: Definitions,
}

#[derive(Debug, serde::Serialize)]
pub struct Definitions {
types: Vec<TypeDef>,
definitions: Vec<TypeDef>,
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -265,16 +259,13 @@ impl Module {
Ok(self)
}

pub fn build(self) -> Result<Schema> {
pub fn build_in(self, schema: &mut Schema) -> Result<()> {
if !self.loaded {
return Err(String::from(LOAD_ERROR));
}

let definitions = Definitions {
// We filter map to get rid of stuff we don't need in our schema.
types: self.items.into_iter().filter_map(|it| (&*it.borrow()).into()).collect(),
};
Ok(Schema { source: self.path, definitions })
schema.definitions.extend(self.items.into_iter().filter_map(|it| (&*it.borrow()).into()));
Ok(())
}
}

Expand Down

0 comments on commit c9c8d22

Please sign in to comment.