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

mod highway;
mod printer;

#[cfg(test)]
pub mod test;

use printer::Printable;

pub enum Format {
    Yaml,
    Json,
}

#[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 {}

pub fn to_string(source: &str, format: Format) -> Result<String, Error> {
    let api: OpenAPI = serde_yaml::from_str(&source).map_err(|_| Error::InvalidSource)?;

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

    let mut generated = printer::GeneratedModule::default();

    generated.set_name(api.info.title);
    if let Some(description) = api.info.description {
        generated.set_description(description);
    }

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