Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: Re-implement crate using a toml-driven code generator #234

Open
alteous opened this issue Jul 21, 2019 · 2 comments
Open

RFC: Re-implement crate using a toml-driven code generator #234

alteous opened this issue Jul 21, 2019 · 2 comments

Comments

@alteous
Copy link
Member

alteous commented Jul 21, 2019

Based on my conclusions in #198, this tracks the re-implementation of the gltf_derive, gltf_json, and gltf crates using a code generation program (written in Rust, of course!)

@alteous
Copy link
Member Author

alteous commented Jul 21, 2019

This would close #187.

@alteous
Copy link
Member Author

alteous commented Jul 21, 2019

Example toml input

[meta]
id = "accessor::Accessor"
kind = "Struct"
docs = "A typed view into a buffer view."

[fields.buffer_view]
ty = "Index"
of = "buffer::View"
json = "bufferView"
docs = "The parent buffer view this accessor reads from."

[fields.byte_offset]
ty = "Integer"
json = "byteOffset"
docs = "The number of components within the buffer view."
default = "0"

[fields.data_type]
ty = "Enum"
of = "accessor::DataType"
json = "componentType"
docs = "The data type of components in the attribute."

[fields.dimensions]
ty = "Enum"
of = "accessor::Dimensions"
json = "type"
docs = "Specifies if the attribute is a scalar, vector, or matrix."

[fields.min]
ty = "Option"
of = "serde_json::value::RawValue"
docs = "Minimum value of each component in this attribute."

[fields.max]
ty = "Option"
of = "serde_json::value::RawValue"
docs = "Maximum value of each component in this attribute."

[fields.normalized]
ty = "Bool"
docs = "Specifies whether integer data values should be normalized."
default = "false"

Example output

$ cargo run schema/accessor/Accessor.toml | rustfmt
pub mod json {
    pub mod accessor {
        /// A typed view into a buffer view.
        #[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
        pub struct Accessor {
            /// The parent buffer view this accessor reads from.
            pub buffer_view: Index<buffer::View>,
            /// The number of components within the buffer view.
            #[serde(default = "byte_offset_default")]
            #[serde(skip_serializing_if = "byte_offset_is_default")]
            pub byte_offset: u32,
            /// The data type of components in the attribute.
            pub data_type: Checked<accessor::DataType>,
            /// Specifies if the attribute is a scalar, vector, or matrix.
            pub dimensions: Checked<accessor::Dimensions>,
            /// Maximum value of each component in this attribute.
            #[serde(default, skip_serializing_if = "Option::is_none")]
            pub max: Option<serde_json::value::RawValue>,
            /// Minimum value of each component in this attribute.
            #[serde(default, skip_serializing_if = "Option::is_none")]
            pub min: Option<serde_json::value::RawValue>,
            /// Specifies whether integer data values should be normalized.
            #[serde(default = "normalized_default")]
            #[serde(skip_serializing_if = "normalized_is_default")]
            pub normalized: bool,
        }
        fn byte_offset_default() -> u32 {
            0
        }
        fn byte_offset_is_default(x: u32) -> u32 {
            x == 0
        }
        fn normalized_default() -> bool {
            false
        }
        fn normalized_is_default(x: bool) -> bool {
            x == false
        }

    }
}
pub mod accessor {
    /// A typed view into a buffer view.
    #[derive(Clone, Debug)]
    pub struct Accessor<'a> {
        pub(crate) document: &'a Document,
        pub(crate) json: &'a json::Accessor,
    }

    impl<'a> Accessor<'a> {
        /// The parent buffer view this accessor reads from.
        pub fn buffer_view(&self) -> buffer::View<'a> {
            self.document.get(&self.buffer_view)
        }
        /// The number of components within the buffer view.
        pub fn byte_offset(&self) -> u32 {
            self.byte_offset
        }
        /// The data type of components in the attribute.
        pub fn data_type(&self) -> accessor::DataType {
            self.data_type.unwrap()
        }
        /// Specifies if the attribute is a scalar, vector, or matrix.
        pub fn dimensions(&self) -> accessor::Dimensions {
            self.dimensions.unwrap()
        }
        /// Maximum value of each component in this attribute.
        pub fn max(&self) -> Option<&'a serde_json::value::RawValue> {
            self.max.as_ref()
        }
        /// Minimum value of each component in this attribute.
        pub fn min(&self) -> Option<&'a serde_json::value::RawValue> {
            self.min.as_ref()
        }
        /// Specifies whether integer data values should be normalized.
        pub fn normalized(&self) -> bool {
            self.normalized
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant