Skip to content

Commit

Permalink
refactor(ast_codegen): alter JSON schema format (#4784)
Browse files Browse the repository at this point in the history
Change the format of schema JSON by altering `serde` attrs.
  • Loading branch information
overlookmotel committed Aug 9, 2024
1 parent c79ca40 commit 3131187
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
2 changes: 1 addition & 1 deletion tasks/ast_codegen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {

if let CliOptions { schema: Some(schema_path), dry_run: false, .. } = cli_options {
let path = schema_path.to_str().expect("invalid path for schema output.");
let schema = serde_json::to_string_pretty(&schema).normalize()?;
let schema = serde_json::to_string_pretty(&schema.defs).normalize()?;
write_all_to(schema.as_bytes(), path)?;
}

Expand Down
16 changes: 15 additions & 1 deletion tasks/ast_codegen/src/schema/defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::{
use super::{with_either, TypeName};

#[derive(Debug, Serialize)]
#[serde(untagged)]
pub enum TypeDef {
Struct(StructDef),
Enum(EnumDef),
Expand Down Expand Up @@ -38,24 +39,29 @@ impl TypeDef {
}

#[derive(Debug, Serialize)]
#[serde(tag = "type", rename = "struct", rename_all = "camelCase")]
pub struct StructDef {
pub id: TypeId,
pub name: String,
#[serde(skip)]
pub visitable: bool,
pub fields: Vec<FieldDef>,
#[serde(skip)]
pub has_lifetime: bool,
pub size_64: usize,
pub align_64: usize,
pub offsets_64: Option<Vec<usize>>,
pub size_32: usize,
pub align_32: usize,
pub offsets_32: Option<Vec<usize>>,
#[serde(skip)]
pub generated_derives: Vec<String>,
#[serde(skip)]
pub markers: OuterMarkers,
}

#[derive(Debug, Serialize)]
#[serde(tag = "type", rename = "enum", rename_all = "camelCase")]
pub struct EnumDef {
pub id: TypeId,
pub name: String,
Expand Down Expand Up @@ -97,6 +103,7 @@ pub struct VariantDef {
pub name: String,
pub fields: Vec<FieldDef>,
pub discriminant: u8,
#[serde(skip)]
pub markers: InnerMarkers,
}

Expand All @@ -112,6 +119,7 @@ impl VariantDef {

#[derive(Debug, Serialize)]
pub struct InheritDef {
#[serde(rename = "super")]
pub super_: TypeRef,
pub variants: Vec<VariantDef>,
}
Expand All @@ -120,13 +128,18 @@ pub struct InheritDef {
pub struct FieldDef {
/// `None` if unnamed
pub name: Option<String>,
#[serde(skip)]
pub vis: Visibility,
#[serde(rename = "type")]
pub typ: TypeRef,
#[serde(skip)]
pub markers: InnerMarkers,
#[serde(skip)]
pub docs: Vec<String>,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum Visibility {
None,
Pub,
Expand Down Expand Up @@ -158,10 +171,11 @@ impl FieldDef {

#[derive(Debug, Serialize)]
pub struct TypeRef {
#[serde(skip)]
pub(super) id: Option<TypeId>,
pub(super) name: TypeName,

#[serde(skip)]
#[serde(rename = "id")]
pub(super) transparent_id: Option<TypeId>,

#[serde(skip)]
Expand Down
2 changes: 1 addition & 1 deletion tasks/ast_codegen/src/schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl<'a> From<crate::util::TypeIdentResult<'a>> for TypeName {

#[derive(Debug, Default, serde::Serialize)]
pub struct Schema {
defs: Vec<TypeDef>,
pub defs: Vec<TypeDef>,
}

impl Schema {
Expand Down

0 comments on commit 3131187

Please sign in to comment.