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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
use crate::client::api::Api;
use crate::client::api::Method;
use crate::client::model::DataType;
use failure::Error;
use handlebars::Handlebars;
use openapiv3::OpenAPI;
use openapiv3::Operation;
use openapiv3::Parameter;
use openapiv3::PathItem;
use openapiv3::ReferenceOr;
use serde_derive::Serialize;
use serde_yaml;
use std::collections::HashSet;
use std::fs::{DirBuilder, File};
use std::io::Write;
use std::path::Path;

mod api;
mod model;

pub fn client(api_path: &str, output_dir: &str, tests: bool) -> Result<(), Error> {
    let mut reg = Handlebars::new();
    reg.register_escape_fn(handlebars::no_escape);
    reg.register_template_string("api_mod", include_str!("resources/api_mod.mustache"))?;
    reg.register_template_string("api", include_str!("resources/api.mustache"))?;
    reg.register_template_string("model_enum", include_str!("resources/model_enum.mustache"))?;
    reg.register_template_string("model_mod", include_str!("resources/model_mod.mustache"))?;
    reg.register_template_string(
        "model_struct",
        include_str!("resources/model_struct.mustache"),
    )?;
    reg.register_template_string(
        "model_newtype",
        include_str!("resources/model_newtype.mustache"),
    )?;
    reg.register_template_string("mod", include_str!("resources/mod.mustache"))?;

    let dest_path = Path::new(&output_dir);
    DirBuilder::new().recursive(true).create(&dest_path)?;

    let spec: OpenAPI = serde_yaml::from_reader(File::open(api_path)?)?;

    DirBuilder::new()
        .recursive(true)
        .create(&dest_path.join("apis"))?;

    let mut configuration = File::create(&dest_path.join("apis/configuration.rs"))?;
    configuration.write_all(include_bytes!("resources/configuration.rs"))?;

    let mut request = File::create(&dest_path.join("apis/request.rs"))?;
    request.write_all(include_bytes!("resources/request.rs"))?;

    let apis = spec_apis(&spec, tests);

    let api_mod = File::create(&dest_path.join("apis/mod.rs"))?;
    reg.render_to_write("api_mod", &apis, api_mod)?;

    for api in &apis {
        let api_file = File::create(&dest_path.join(format!("apis/{}_api.rs", api.snake_id)))?;
        reg.render_to_write("api", &api, api_file)?;
    }

    let models = match spec.components {
        Some(components) => components
            .schemas
            .into_iter()
            .map(|entry| entry.into())
            .collect::<Vec<DataType>>(),
        None => vec![],
    };

    let models_path = dest_path.join("models");
    DirBuilder::new().recursive(true).create(&models_path)?;

    let models_mod = File::create(models_path.join("mod.rs"))?;
    reg.render_to_write("model_mod", &models, models_mod)?;

    for model in &models {
        match model {
            DataType::Struct(_struct) => {
                let model = File::create(models_path.join(format!("{}.rs", _struct.snake_id)))?;
                reg.render_to_write("model_struct", &_struct, model)?;
            }
            DataType::NewType(newtype) => {
                let model = File::create(models_path.join(format!("{}.rs", newtype.snake_id)))?;
                reg.render_to_write("model_newtype", &newtype, model)?;
            }
            DataType::Enum(_enum) => {
                let model = File::create(models_path.join(format!("{}.rs", _enum.snake_id)))?;
                reg.render_to_write("model_enum", &_enum, model)?;
            }
        }
    }

    let mod_file = if output_dir == "src" {
        File::create(dest_path.join("lib.rs"))?
    } else {
        File::create(dest_path.join("mod.rs"))?
    };

    let r#mod = Mod {
        root: output_dir == "src",
    };

    reg.render_to_write("mod", &r#mod, mod_file)?;

    Ok(())
}

#[derive(Debug, Serialize)]
struct Mod {
    root: bool,
}

fn spec_apis(spec: &OpenAPI, tests: bool) -> Vec<Api> {
    paths_tags(spec)
        .into_iter()
        .map(|tag| Api {
            snake_id: tag.clone().unwrap_or("untagged".to_string()).into(),
            pascal_id: tag.clone().unwrap_or("untagged".to_string()).into(),
            methods: spec
                .paths
                .iter()
                .filter(|(_path, reference_or_operations)| {
                    operations_tags(reference_or_operations).contains(&tag)
                })
                .flat_map(|(path, reference_or_operations)| {
                    operations_methods(path, reference_or_operations)
                })
                .collect(),
            tests,
        })
        .collect()
}

fn operations_methods(path: &str, reference_or_operations: &ReferenceOr<PathItem>) -> Vec<Method> {
    match reference_or_operations {
        ReferenceOr::Reference { .. } => unimplemented!(),
        ReferenceOr::Item(operations) => {
            let options =
                vec![
                    operations.get.as_ref().map(|operation| {
                        operation_method("GET".into(), path.to_owned(), &operation)
                    }),
                    operations.post.as_ref().map(|operation| {
                        operation_method("POST".into(), path.to_owned(), &operation)
                    }),
                    operations.put.as_ref().map(|operation| {
                        operation_method("PUT".into(), path.to_owned(), &operation)
                    }),
                    operations.patch.as_ref().map(|operation| {
                        operation_method("PATCH".into(), path.to_owned(), &operation)
                    }),
                    operations.delete.as_ref().map(|operation| {
                        operation_method("DELETE".into(), path.to_owned(), &operation)
                    }),
                ];

            options.into_iter().filter_map(|o| o).collect()
        }
    }
}

fn operation_method(method: String, path: String, operation: &Operation) -> Method {
    Method {
        snake_id: match operation.operation_id.as_ref() {
            Some(operation_id) => operation_id.to_owned(),
            None => format!("{}/{}", method, path),
        }
        .into(),
        path: path,
        http_method: method,
        path_parameters: operation
            .parameters
            .iter()
            .filter_map(|reference_or_parameter| {
                if let ReferenceOr::Item(parameter) = reference_or_parameter {
                    Some(parameter)
                } else {
                    None
                }
            })
            .filter_map(|parameter| {
                if let Parameter::Path { parameter_data, .. } = parameter {
                    Some(parameter_data)
                } else {
                    None
                }
            })
            .map(|parameter_data| parameter_data.into())
            .collect(),
        query_parameters: operation
            .parameters
            .iter()
            .filter_map(|reference_or_parameter| {
                if let ReferenceOr::Item(parameter) = reference_or_parameter {
                    Some(parameter)
                } else {
                    None
                }
            })
            .filter_map(|parameter| {
                if let Parameter::Query { parameter_data, .. } = parameter {
                    Some(parameter_data)
                } else {
                    None
                }
            })
            .map(|parameter_data| parameter_data.into())
            .collect(),
        body: operation
            .request_body
            .as_ref()
            .map(|parameter| parameter.into()),
        returns: operation
            .responses
            .responses
            .get("200")
            .and_then(|reference_or_response| match reference_or_response {
                ReferenceOr::Item(response) => response.content.get("application/json"),
                _ => unimplemented!(),
            })
            .and_then(|reference_or_mediatype| match reference_or_mediatype {
                ReferenceOr::Item(mediatype) => mediatype.schema.as_ref(),
                _ => unimplemented!(),
            })
            .map(|reference_or_schema| reference_or_schema.into()),
    }
}

fn paths_tags(spec: &OpenAPI) -> HashSet<Option<String>> {
    spec.paths
        .iter()
        .flat_map(|(_, operations)| operations_tags(operations))
        .collect()
}

fn operations_tags(reference_or_operations: &ReferenceOr<PathItem>) -> Vec<Option<String>> {
    match reference_or_operations {
        ReferenceOr::Reference { .. } => unimplemented!(),
        ReferenceOr::Item(operations) => {
            let mut tags = operation_tags(operations.get.as_ref());
            tags.append(&mut operation_tags(operations.post.as_ref()));
            tags.append(&mut operation_tags(operations.put.as_ref()));
            tags.append(&mut operation_tags(operations.patch.as_ref()));
            tags.append(&mut operation_tags(operations.delete.as_ref()));

            tags
        }
    }
}

fn operation_tags(operation: Option<&Operation>) -> Vec<Option<String>> {
    match operation {
        Some(operation) => {
            if operation.tags.is_empty() {
                vec![None]
            } else {
                operation.tags.iter().map(|s| Some(s.to_string())).collect()
            }
        }
        None => Vec::new(),
    }
}