1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
pub mod syntax_fragments;

use std::{
    env,
    fmt::Debug,
    fs::{File, OpenOptions},
    io::{BufReader, Read, Write},
    path::Path,
    str::FromStr,
};

use crate::{edmx::Edmx, property::Property, utils::run_rustfmt};

use check_keyword::CheckKeyword;
use syntax_fragments::*;

fn start_struct(struct_name: String) -> Vec<u8> {
    [START_PUB_STRUCT, SPACE, struct_name.as_bytes(), OPEN_CURLY].concat()
}
fn end_struct() -> Vec<u8> {
    [LINE_FEED, CLOSE_CURLY, LINE_FEED, LINE_FEED].concat()
}

static DEFAULT_INPUT_DIR: &str = &"./odata";

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
/// Deserialize an SAP OData metadata document
///
/// The metadata file must exist in the `./odata` directory and have the `.xml` extension.
/// For example:
///
/// `odata/`<br>
/// `└── gwsample_basic.xml`
pub fn deserialize_sap_metadata(metadata_file_name: &str) -> Result<Edmx, ParseError> {
    let mut xml_buffer: Vec<u8> = Vec::new();
    let xml_input_pathname = format!("{}/{}.xml", DEFAULT_INPUT_DIR, metadata_file_name);

    let f_xml = File::open(Path::new(&xml_input_pathname))?;
    let _file_size = BufReader::new(f_xml).read_to_end(&mut xml_buffer);
    let xml = String::from_utf8(xml_buffer)?;
    let edmx = Edmx::from_str(&xml)?;

    return Ok(edmx);
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
/// Generate Rust structs from the OData metadata
///
/// Any fields whose names clashe with a Rust reserved word are written in raw format: E.G. `type --> r#type`
pub fn gen_src(metadata_file_name: &str, namespace: &str) {
    let mut out_buffer: Vec<u8> = Vec::new();

    match deserialize_sap_metadata(metadata_file_name) {
        // Deserialization can fail sometimes.
        // This can happen for example, when a quoted XML attribute value contains unescaped double quote characters
        Err(err) => println!("Error: {}", err.msg),
        Ok(edmx) => {
            let out_dir = env::var_os("OUT_DIR").unwrap();
            let output_path = Path::new(&out_dir).join(format!("{}.rs", metadata_file_name));

            let mut out_file = OpenOptions::new()
                .create(true)
                .write(true)
                .truncate(true)
                .open(&output_path)
                .unwrap();

            // If this fails, then either the build script is being run with the wrong value for the namespace, or we're
            // trying to parse from XML that is not valid OData metadata
            if let Some(schema) = edmx.data_services.fetch_schema(namespace) {
                out_buffer.append(&mut [USE_SERDE, LINE_FEED, LINE_FEED].concat());

                // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
                // Transform ComplexType definitions if present
                if let Some(cts) = &schema.complex_types {
                    for ct in cts {
                        let trimmed_name = Property::trim_complex_type_name(&ct.name, namespace);
                        let ct_name = convert_case::Casing::to_case(
                            &String::from_utf8(trimmed_name).unwrap(),
                            convert_case::Case::UpperCamel,
                        );

                        // If the complex type contains only one property and the name suffix is a Rust type, then a
                        // struct does not need to be generated.  This happens with SAP complex types such as
                        // `CT_String` which contains the single property called `String`.
                        // Such complex types are in fact not "complex" at all, and should be replaced with a single
                        // native Rust type
                        if ct.properties.len() > 1 && !ct_name.is_keyword() {
                            let mut props = ct.properties.clone();
                            props.sort();

                            out_buffer.append(&mut derive_str(vec![
                                DeriveTraits::CLONE,
                                DeriveTraits::DEBUG,
                                DeriveTraits::DEFAULT,
                                DeriveTraits::SERIALIZE,
                                DeriveTraits::DESERIALIZE,
                            ]));
                            out_buffer.append(&mut SERDE_RENAME_PASCAL_CASE.to_vec());
                            out_buffer.append(&mut start_struct(ct_name));

                            for prop in props {
                                out_buffer.append(&mut prop.to_rust(namespace));
                            }

                            // Add terminating line feed, close curly brace, then two more line feeds
                            out_buffer.append(&mut end_struct());
                        }
                    }
                }

                // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
                // Transform each EntityType definition to a Rust struct
                //
                // It is possible for an OData service to have zero EntityTypes; in which case, there will also be zero
                // EntitySets. This in turns means that interaction with the OData service is only possible through the
                // FunctionImports
                for entity in &schema.entity_types {
                    let struct_name = convert_case::Casing::to_case(
                        &String::from_utf8(entity.name.clone().into_bytes()).unwrap(),
                        convert_case::Case::UpperCamel,
                    );
                    out_buffer.append(&mut derive_str(vec![
                        DeriveTraits::CLONE,
                        DeriveTraits::DEBUG,
                        DeriveTraits::DEFAULT,
                        DeriveTraits::SERIALIZE,
                        DeriveTraits::DESERIALIZE,
                    ]));
                    out_buffer.append(&mut SERDE_RENAME_PASCAL_CASE.to_vec());
                    out_buffer.append(&mut start_struct(struct_name));

                    let mut props = entity.properties.clone();
                    props.sort();

                    for prop in props {
                        out_buffer.append(&mut prop.to_rust(namespace));
                    }

                    out_buffer.append(&mut end_struct());
                }

                // Create enum + impl for the entity container
                // The values in this enum act as a proxy for the service document
                if let Some(ent_cont) = &schema.entity_container {
                    out_buffer.append(&mut ent_cont.to_enum_with_impl());
                }

                // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
                // TODO Generate function imports
                // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

                // Syntax check and format the generated code
                match run_rustfmt(&out_buffer, &metadata_file_name) {
                    Ok(formatted_bytes) => {
                        out_file.write_all(&formatted_bytes).unwrap();

                        // Tell cargo to watch the input XML file
                        println!(
                            "cargo:rerun-if-changed={}",
                            format!("{}/{}.xml", DEFAULT_INPUT_DIR, metadata_file_name)
                        );
                    },
                    Err(err) => println!("Error: rustfmt ended with {}", err.to_string()),
                }
            } else {
                println!("Required namespace '{}' not found in schema", namespace);
            };
        },
    };

    out_buffer.clear();
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#[derive(Debug)]
pub struct ParseError {
    pub msg: String,
}

impl std::fmt::Display for ParseError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{}", self.msg)
    }
}

impl std::error::Error for ParseError {
    fn description(&self) -> &str {
        &self.msg
    }
}

impl From<std::io::Error> for ParseError {
    fn from(io_err: std::io::Error) -> ParseError {
        ParseError { msg: io_err.to_string() }
    }
}

impl From<std::string::FromUtf8Error> for ParseError {
    fn from(utf8_err: std::string::FromUtf8Error) -> ParseError {
        ParseError { msg: utf8_err.to_string() }
    }
}

impl From<std::io::ErrorKind> for ParseError {
    fn from(io_err: std::io::ErrorKind) -> ParseError {
        ParseError { msg: io_err.to_string() }
    }
}

impl From<quick_xml::DeError> for ParseError {
    fn from(xml_err: quick_xml::DeError) -> ParseError {
        ParseError { msg: xml_err.to_string() }
    }
}