2022-01-15 02:01:59 +00:00
|
|
|
// Copyright 2022 Oxide Computer Company
|
2021-10-17 17:40:22 +00:00
|
|
|
|
2022-04-02 05:30:23 +00:00
|
|
|
use openapiv3::OpenAPI;
|
2021-10-17 17:40:22 +00:00
|
|
|
use proc_macro2::TokenStream;
|
2022-04-02 05:30:23 +00:00
|
|
|
use quote::quote;
|
2021-10-17 17:40:22 +00:00
|
|
|
use thiserror::Error;
|
2022-04-02 05:30:23 +00:00
|
|
|
use typify::TypeSpace;
|
2021-10-17 17:40:22 +00:00
|
|
|
|
|
|
|
use crate::to_schema::ToSchema;
|
|
|
|
|
2022-04-02 05:30:23 +00:00
|
|
|
mod method;
|
2021-10-17 17:40:22 +00:00
|
|
|
mod template;
|
|
|
|
mod to_schema;
|
2022-04-02 05:30:23 +00:00
|
|
|
mod util;
|
2021-10-17 17:40:22 +00:00
|
|
|
|
|
|
|
#[derive(Error, Debug)]
|
|
|
|
pub enum Error {
|
|
|
|
#[error("unexpected value type")]
|
|
|
|
BadValue(String, serde_json::Value),
|
|
|
|
#[error("type error")]
|
|
|
|
TypeError(#[from] typify::Error),
|
|
|
|
#[error("XXX")]
|
|
|
|
BadConversion(String),
|
|
|
|
#[error("invalid operation path")]
|
|
|
|
InvalidPath(String),
|
|
|
|
//#[error("unknown")]
|
|
|
|
//Unknown,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type Result<T> = std::result::Result<T, Error>;
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct Generator {
|
|
|
|
type_space: TypeSpace,
|
2021-10-29 14:16:39 +00:00
|
|
|
inner_type: Option<TokenStream>,
|
|
|
|
pre_hook: Option<TokenStream>,
|
|
|
|
post_hook: Option<TokenStream>,
|
2022-01-05 20:02:46 +00:00
|
|
|
uses_futures: bool,
|
2021-10-17 17:40:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Generator {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self::default()
|
|
|
|
}
|
|
|
|
|
2021-10-29 14:16:39 +00:00
|
|
|
pub fn with_inner_type(&mut self, inner_type: TokenStream) -> &mut Self {
|
|
|
|
self.inner_type = Some(inner_type);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_pre_hook(&mut self, pre_hook: TokenStream) -> &mut Self {
|
|
|
|
self.pre_hook = Some(pre_hook);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_post_hook(&mut self, post_hook: TokenStream) -> &mut Self {
|
|
|
|
self.post_hook = Some(post_hook);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-01-15 02:01:59 +00:00
|
|
|
pub fn with_derive(&mut self, derive: TokenStream) -> &mut Self {
|
|
|
|
self.type_space.add_derive(derive);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-10-17 17:40:22 +00:00
|
|
|
pub fn generate_tokens(&mut self, spec: &OpenAPI) -> Result<TokenStream> {
|
|
|
|
// Convert our components dictionary to schemars
|
|
|
|
let schemas = spec
|
|
|
|
.components
|
|
|
|
.iter()
|
|
|
|
.flat_map(|components| {
|
|
|
|
components.schemas.iter().map(|(name, ref_or_schema)| {
|
|
|
|
(name.clone(), ref_or_schema.to_schema())
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.collect::<Vec<(String, _)>>();
|
|
|
|
|
|
|
|
self.type_space.set_type_mod("types");
|
|
|
|
self.type_space.add_ref_types(schemas)?;
|
|
|
|
|
2021-12-10 02:15:24 +00:00
|
|
|
let raw_methods = spec
|
2021-10-29 14:16:39 +00:00
|
|
|
.paths
|
|
|
|
.iter()
|
|
|
|
.flat_map(|(path, ref_or_item)| {
|
2021-12-10 02:15:24 +00:00
|
|
|
// Exclude externally defined path items.
|
2021-10-29 14:16:39 +00:00
|
|
|
let item = ref_or_item.as_item().unwrap();
|
2021-12-10 02:15:24 +00:00
|
|
|
// TODO punt on paramters that apply to all path items for now.
|
2021-10-29 14:16:39 +00:00
|
|
|
assert!(item.parameters.is_empty());
|
|
|
|
item.iter().map(move |(method, operation)| {
|
|
|
|
(path.as_str(), method, operation)
|
|
|
|
})
|
|
|
|
})
|
2021-10-17 17:40:22 +00:00
|
|
|
.map(|(path, method, operation)| {
|
2021-10-29 14:16:39 +00:00
|
|
|
self.process_operation(
|
|
|
|
operation,
|
|
|
|
&spec.components,
|
|
|
|
path,
|
|
|
|
method,
|
|
|
|
)
|
2021-10-17 17:40:22 +00:00
|
|
|
})
|
|
|
|
.collect::<Result<Vec<_>>>()?;
|
|
|
|
|
2021-12-10 02:15:24 +00:00
|
|
|
let methods = raw_methods
|
|
|
|
.iter()
|
|
|
|
.map(|method| self.process_method(method))
|
|
|
|
.collect::<Result<Vec<_>>>()?;
|
|
|
|
|
2021-10-17 17:40:22 +00:00
|
|
|
let mut types = self
|
|
|
|
.type_space
|
|
|
|
.iter_types()
|
2021-11-02 18:16:55 +00:00
|
|
|
.map(|t| (t.name(), t.definition()))
|
2021-10-17 17:40:22 +00:00
|
|
|
.collect::<Vec<_>>();
|
2021-10-29 14:16:39 +00:00
|
|
|
types.sort_by(|(a_name, _), (b_name, _)| a_name.cmp(b_name));
|
2021-10-17 17:40:22 +00:00
|
|
|
let types = types.into_iter().map(|(_, def)| def);
|
2022-05-12 22:07:48 +00:00
|
|
|
let shared = self.type_space.common_code();
|
2021-10-17 17:40:22 +00:00
|
|
|
|
2021-10-29 14:16:39 +00:00
|
|
|
let inner_property = self.inner_type.as_ref().map(|inner| {
|
|
|
|
quote! {
|
|
|
|
inner: #inner,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
let inner_value = self.inner_type.as_ref().map(|_| {
|
|
|
|
quote! {
|
|
|
|
inner
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-10-17 17:40:22 +00:00
|
|
|
let file = quote! {
|
2022-02-08 16:59:38 +00:00
|
|
|
// Re-export ResponseValue and Error since those are used by the
|
|
|
|
// public interface of Client.
|
2022-03-15 23:11:47 +00:00
|
|
|
pub use progenitor_client::{ByteStream, Error, ResponseValue};
|
2021-10-17 17:40:22 +00:00
|
|
|
|
|
|
|
pub mod types {
|
|
|
|
use serde::{Deserialize, Serialize};
|
2022-05-12 22:07:48 +00:00
|
|
|
#shared
|
2021-10-17 17:40:22 +00:00
|
|
|
#(#types)*
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Client {
|
|
|
|
baseurl: String,
|
|
|
|
client: reqwest::Client,
|
2021-10-29 14:16:39 +00:00
|
|
|
#inner_property
|
2021-10-17 17:40:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Client {
|
2021-10-29 14:16:39 +00:00
|
|
|
pub fn new(
|
|
|
|
baseurl: &str,
|
|
|
|
#inner_property
|
|
|
|
) -> Self {
|
2021-10-17 17:40:22 +00:00
|
|
|
let dur = std::time::Duration::from_secs(15);
|
|
|
|
let client = reqwest::ClientBuilder::new()
|
|
|
|
.connect_timeout(dur)
|
|
|
|
.timeout(dur)
|
|
|
|
.build()
|
|
|
|
.unwrap();
|
2021-10-29 14:16:39 +00:00
|
|
|
Self::new_with_client(baseurl, client, #inner_value)
|
2021-10-17 17:40:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_with_client(
|
|
|
|
baseurl: &str,
|
|
|
|
client: reqwest::Client,
|
2021-10-29 14:16:39 +00:00
|
|
|
#inner_property
|
|
|
|
) -> Self {
|
|
|
|
Self {
|
2021-10-17 17:40:22 +00:00
|
|
|
baseurl: baseurl.to_string(),
|
|
|
|
client,
|
2021-10-29 14:16:39 +00:00
|
|
|
#inner_value
|
2021-10-17 17:40:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-07 06:24:03 +00:00
|
|
|
pub fn baseurl(&self) -> &String {
|
|
|
|
&self.baseurl
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn client(&self) -> &reqwest::Client {
|
|
|
|
&self.client
|
|
|
|
}
|
|
|
|
|
2021-10-17 17:40:22 +00:00
|
|
|
#(#methods)*
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(file)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn generate_text(&mut self, spec: &OpenAPI) -> Result<String> {
|
|
|
|
let output = self.generate_tokens(spec)?;
|
|
|
|
|
2022-01-05 20:02:46 +00:00
|
|
|
// Format the file with rustfmt.
|
2021-10-17 17:40:22 +00:00
|
|
|
let content = rustfmt_wrapper::rustfmt(output).unwrap();
|
|
|
|
|
2022-01-05 20:02:46 +00:00
|
|
|
// Add newlines after end-braces at <= two levels of indentation.
|
2021-10-17 17:40:22 +00:00
|
|
|
Ok(if cfg!(not(windows)) {
|
2022-01-05 20:02:46 +00:00
|
|
|
let regex = regex::Regex::new(r#"(})(\n\s{0,8}[^} ])"#).unwrap();
|
2021-10-17 17:40:22 +00:00
|
|
|
regex.replace_all(&content, "$1\n$2").to_string()
|
|
|
|
} else {
|
2022-01-05 20:02:46 +00:00
|
|
|
let regex = regex::Regex::new(r#"(})(\r\n\s{0,8}[^} ])"#).unwrap();
|
2021-10-17 17:40:22 +00:00
|
|
|
regex.replace_all(&content, "$1\r\n$2").to_string()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn dependencies(&self) -> Vec<String> {
|
|
|
|
let mut deps = vec![
|
2022-03-15 23:11:47 +00:00
|
|
|
"bytes = \"1.1.0\"",
|
|
|
|
"futures-core = \"0.3.21\"",
|
2021-11-02 18:16:55 +00:00
|
|
|
"percent-encoding = \"2.1\"",
|
|
|
|
"serde = { version = \"1.0\", features = [\"derive\"] }",
|
|
|
|
"reqwest = { version = \"0.11\", features = [\"json\", \"stream\"] }",
|
2021-10-17 17:40:22 +00:00
|
|
|
];
|
|
|
|
if self.type_space.uses_uuid() {
|
|
|
|
deps.push(
|
2021-11-02 18:16:55 +00:00
|
|
|
"uuid = { version = \"0.8\", features = [\"serde\", \"v4\"] }",
|
2021-10-17 17:40:22 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
if self.type_space.uses_chrono() {
|
2021-11-02 18:16:55 +00:00
|
|
|
deps.push("chrono = { version = \"0.4\", features = [\"serde\"] }")
|
|
|
|
}
|
2022-01-05 20:02:46 +00:00
|
|
|
if self.uses_futures {
|
|
|
|
deps.push("futures = \"0.3\"")
|
|
|
|
}
|
2021-11-02 18:16:55 +00:00
|
|
|
if self.type_space.uses_serde_json() {
|
|
|
|
deps.push("serde_json = \"1.0\"")
|
2021-10-17 17:40:22 +00:00
|
|
|
}
|
|
|
|
deps.sort_unstable();
|
|
|
|
deps.iter().map(ToString::to_string).collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_type_space(&self) -> &TypeSpace {
|
|
|
|
&self.type_space
|
|
|
|
}
|
|
|
|
}
|