use the uuid crate if we detect a uuid

This commit is contained in:
Joshua M. Clulow 2021-09-23 13:54:42 -07:00
parent 2a319675b1
commit 01f184f8fe
1 changed files with 21 additions and 3 deletions

View File

@ -514,6 +514,7 @@ struct TypeSpace {
ref_to_id: BTreeMap<String, TypeId>,
import_chrono: bool,
import_uuid: bool,
}
impl TypeSpace {
@ -524,6 +525,7 @@ impl TypeSpace {
id_to_entry: BTreeMap::new(),
ref_to_id: BTreeMap::new(),
import_chrono: false,
import_uuid: false,
}
}
@ -778,7 +780,7 @@ impl TypeSpace {
openapiv3::Type::String(st) => {
use openapiv3::{
StringFormat::DateTime,
VariantOrUnknownOrEmpty::{Empty, Item},
VariantOrUnknownOrEmpty::{Empty, Item, Unknown},
};
match &st.format {
@ -789,6 +791,13 @@ impl TypeSpace {
TypeDetails::Basic,
)
}
Unknown(x) if x.as_str() == "uuid" => {
self.import_uuid = true;
(
Some("Uuid".to_string()),
TypeDetails::Basic,
)
}
Empty => {
use TypeDetails::{Enumeration, NewType};
@ -813,7 +822,7 @@ impl TypeSpace {
}
}
x => {
bail!("XXX string format {:?}", x);
bail!("XXX string format {:?} {:?}", x, st);
}
}
}
@ -995,6 +1004,9 @@ fn gen(api: &OpenAPI, ts: &mut TypeSpace) -> Result<String> {
if ts.import_chrono {
a(" use chrono::prelude::*;");
}
if ts.import_uuid {
a(" use uuid::Uuid;");
}
a(" use serde::{Serialize, Deserialize};");
a("");
for te in ids.iter().map(|x| ts.id_to_entry.get(x.1).unwrap()) {
@ -1534,6 +1546,11 @@ fn main() -> Result<()> {
} else {
""
};
let uuid = if ts.import_uuid {
"uuid = { version = \"0.8\", features = [\"serde\", \"v4\"] }\n"
} else {
""
};
let tomlout = format!(
"[package]\n\
name = \"{}\"\n\
@ -1543,10 +1560,11 @@ fn main() -> Result<()> {
[dependencies]\n\
anyhow = \"1\"\n\
{}\
{}\
percent-encoding = \"2.1\"\n\
reqwest = {{ version = \"0.11\", features = [\"json\"] }}\n\
serde = {{ version = \"1\", features = [\"derive\"] }}\n",
name, version, chrono,
name, version, chrono, uuid,
);
save(&toml, tomlout.as_str())?;