schema_generator 0.1.1

schema_generator is a Rust crate initially created for parsing XML files containing forestry data and generating corresponding Rust structs. It parses XML documents and automatically generates Rust data structures that map XML elements to fields, including attributes, child elements, and text content. It integrates with the serde library for serialization and deserialization. While it was originally designed for forestry-related XML data, it can be used with any XML data structure, making it a versatile tool for developers working with XML. Additionally, schema_generator supports JSON to XML conversion.
Documentation



// A struct
#[derive(Debug)]
pub struct XMLStruct {
    name: String,   // The name of the struct
    pub fields: Vec<XMLField>,  // Fields in the struct
}

// A field
#[derive(Debug, Clone)]
pub struct XMLField {
    pub name: String,   // The name of the field
    pub field_type: String,  // The type of the field  
}

fn create_structs(reader: &mut Reader<&[u8]>) -> HashMap<String, XMLStruct> {

    // Stack of structs being constructed
    let mut stack: Vec<XMLStruct> = Vec::new();

    // Finalized structs
    let mut structs: HashMap<String, XMLStruct> = HashMap::new(); 

    loop {
        match reader.read_event() {

            // Handle a start tag:
            Ok(Start(ref e)) => {
                // Create a new struct for this element
                // Push the struct onto the stack
                // If there's a parent struct, add this struct as a field to the parent struct
            },

            // Handle a self-closing tag:
            Ok(Empty(ref e)) => {
                // Create a new struct for this element
                // If there's a parent struct, add this struct as a field to the parent struct
            },

            // Handle an end tag:
            Ok(End(ref e)) => {
                // Pop the current struct from the stack
                // Update the finalized structs with the current struct
                // Merge fields if the struct already exists in the finalized collection of structs
            },

            // End of XML, exit the loop
            Ok(Eof) => break,

            // Handle parsing errors
            Err(e) => panic!("Error at position {}: {:?}", reader.buffer_position(), e),

            // Handle other events
            _ => (),
        }
    }

    structs
}