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
use openapiv3::OpenAPI;

mod highway;
mod printer;

#[cfg(test)]
pub mod test;

use printer::Printable;

/// Format for OpenAPI3 specification
pub enum Format {
    Yaml,
    Json,
}

/// Describes convertation error
#[derive(Debug)]
pub enum Error {
    InvalidSource,
}

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::InvalidSource => write!(f, "OpenAPI structure cannot be parsed"),
        }
    }
}

impl std::error::Error for Error {}

/// Convert source of OpenAPI3 specification to rust code in string representation
pub fn to_string(source: &str, format: Format) -> Result<String, Error> {
    let api: OpenAPI = match format {
        Format::Yaml => serde_yaml::from_str(&source).map_err(|_| Error::InvalidSource)?,
        Format::Json => serde_json::from_str(&source).map_err(|_| Error::InvalidSource)?,
    };

    // eprintln!("{:#?}", api.components);

    let mut highway_components = highway::Components::new();

    if let Some(components) = api.components {
        // for (name, body) in components.request_bodies.iter() {
        //     match body {
        //         ReferenceOr::Item(body) => {
        //             highway_components.parse_request_body(&name, &body);
        //         }
        //         ReferenceOr::Reference { reference } => {
        //             log::info!("skipping request body reference {}", reference);
        //         }
        //     }
        // }

        for (name, schema) in components.schemas.iter() {
            if let Err(reason) = highway_components.parse_schema(&name, &schema) {
                eprintln!("Failed {} {:#?}", name, reason);
            }
        }
    }

    println!("{:#?}", highway_components);

    let mut generated: printer::GeneratedModule = highway_components.into();

    generated.api.set_name(api.info.title);
    generated.api.set_description(api.info.description);
    generated
        .api
        .set_terms_of_service(api.info.terms_of_service);

    Ok(format!("{}", generated.print()))
}

#[cfg(test)]
mod tests {
    use super::{to_string, Format};
    use crate::test::pretty;
    use insta::assert_snapshot;

    #[test]
    fn yaml_schema_prints() {
        let schema = r###"
openapi: 3.0.1
info:
  title: Demo API.
  version: 0.1.0
  description: Test api
paths:
  "/stub":
    get:
      operationId: stub
      responses:
        303:
          description: "Stub"

components:
  schemas:
    SessionUser:
      description: Current user in a session
      type: object
      required:
        - firstName
        - lastName
      properties:
        firstName:
          type: string
        lastName:
          type: string
        inner:
          type: object
          properties:
            foo:
              type: number
            bar:
              type: integer
            baz:
              type: object
              properties:
                demo:
                  type: string
          required:
            - baz
            - bar
        "###;

        assert_snapshot!(pretty(to_string(&schema, Format::Yaml).unwrap()), @r###"
        #![allow(dead_code, unused_imports)]
        pub mod api {
            #[doc = "Test api"]
            pub struct DemoApi {
                api: actix_swagger::Api,
            }
            impl DemoApi {
                pub fn new() -> Self {
                    Self {
                        api: actix_swagger::Api::new(),
                    }
                }
            }
            impl Default for DemoApi {
                fn default() -> Self {
                    let api = Self::new();
                    api
                }
            }
            impl actix_web::dev::HttpServiceFactory for DemoApi {
                fn register(self, config: &mut actix_web::dev::AppService) {
                    self.api.register(config);
                }
            }
            use super::paths;
            use actix_swagger::{Answer, Method};
            use actix_web::{dev::Factory, FromRequest};
            use std::future::Future;
            impl DemoApi {}
        }
        pub mod components {
            pub mod parameters {
                use serde::{Deserialize, Serialize};
            }
            pub mod request_bodies {
                use serde::{Deserialize, Serialize};
            }
            pub mod responses {
                use serde::{Deserialize, Serialize};
            }
            pub mod schemas {
                use serde::{Deserialize, Serialize};
                #[doc = "Current user in a session"]
                #[derive(Debug, Serialize, Deserialize)]
                pub struct SessionUser {
                    #[serde(rename = "firstName")]
                    pub first_name: String,
                    #[serde(rename = "lastName")]
                    pub last_name: String,
                    pub inner: Option<SessionUserInner>,
                }
                #[derive(Debug, Serialize, Deserialize)]
                pub struct SessionUserInner {
                    pub foo: Option<f32>,
                    pub bar: i32,
                    pub baz: SessionUserInnerBaz,
                }
                #[derive(Debug, Serialize, Deserialize)]
                pub struct SessionUserInnerBaz {
                    pub demo: Option<String>,
                }
            }
        }
        pub mod paths {
            use super::components::{parameters, responses};
        }
        "###);
    }
}