use &str not String for method parameters
This commit is contained in:
parent
afff057f00
commit
8409a6dced
46
src/main.rs
46
src/main.rs
|
@ -478,6 +478,26 @@ impl PartialEq for TypeId {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum UseContext {
|
||||||
|
Module,
|
||||||
|
Return,
|
||||||
|
Parameter,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl UseContext {
|
||||||
|
fn is_module(&self) -> bool {
|
||||||
|
matches!(self, &UseContext::Module)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_return(&self) -> bool {
|
||||||
|
matches!(self, &UseContext::Return)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_parameter(&self) -> bool {
|
||||||
|
matches!(self, &UseContext::Parameter)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct TypeSpace {
|
struct TypeSpace {
|
||||||
next_id: u64,
|
next_id: u64,
|
||||||
|
@ -589,21 +609,27 @@ impl TypeSpace {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render_type(&self, tid: &TypeId, in_mod: bool) -> Result<String> {
|
fn render_type(&self, tid: &TypeId, ctx: UseContext) -> Result<String> {
|
||||||
|
let in_mod = ctx.is_module();
|
||||||
|
|
||||||
if let Some(te) = self.id_to_entry.get(&tid) {
|
if let Some(te) = self.id_to_entry.get(&tid) {
|
||||||
match &te.details {
|
match &te.details {
|
||||||
TypeDetails::Basic => {
|
TypeDetails::Basic => {
|
||||||
if let Some(n) = &te.name {
|
if let Some(n) = &te.name {
|
||||||
Ok(n.to_string())
|
Ok(if n == "String" && ctx.is_parameter() {
|
||||||
|
"&str"
|
||||||
|
} else {
|
||||||
|
n
|
||||||
|
}.to_string())
|
||||||
} else {
|
} else {
|
||||||
bail!("basic type {:?} does not have a name?", tid);
|
bail!("basic type {:?} does not have a name?", tid);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
TypeDetails::Array(itid) => {
|
TypeDetails::Array(itid) => {
|
||||||
Ok(format!("Vec<{}>", self.render_type(itid, in_mod)?))
|
Ok(format!("Vec<{}>", self.render_type(itid, ctx)?))
|
||||||
}
|
}
|
||||||
TypeDetails::Optional(itid) => {
|
TypeDetails::Optional(itid) => {
|
||||||
Ok(format!("Option<{}>", self.render_type(itid, in_mod)?))
|
Ok(format!("Option<{}>", self.render_type(itid, ctx)?))
|
||||||
}
|
}
|
||||||
TypeDetails::Object(_)
|
TypeDetails::Object(_)
|
||||||
| TypeDetails::NewType(_)
|
| TypeDetails::NewType(_)
|
||||||
|
@ -971,7 +997,7 @@ fn gen(api: &OpenAPI, ts: &mut TypeSpace) -> Result<String> {
|
||||||
a(&format!(
|
a(&format!(
|
||||||
" pub {}: {},",
|
" pub {}: {},",
|
||||||
name,
|
name,
|
||||||
ts.render_type(tid, true)?
|
ts.render_type(tid, UseContext::Module)?
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
a(" }");
|
a(" }");
|
||||||
|
@ -983,7 +1009,7 @@ fn gen(api: &OpenAPI, ts: &mut TypeSpace) -> Result<String> {
|
||||||
a(&format!(
|
a(&format!(
|
||||||
" pub struct {}({});",
|
" pub struct {}({});",
|
||||||
n,
|
n,
|
||||||
ts.render_type(tid, true)?,
|
ts.render_type(tid, UseContext::Module)?,
|
||||||
));
|
));
|
||||||
a("");
|
a("");
|
||||||
|
|
||||||
|
@ -1100,7 +1126,7 @@ fn gen(api: &OpenAPI, ts: &mut TypeSpace) -> Result<String> {
|
||||||
if let Some(s) = &mt.schema {
|
if let Some(s) = &mt.schema {
|
||||||
let tid = ts.select(None, s)?;
|
let tid = ts.select(None, s)?;
|
||||||
(
|
(
|
||||||
Some(format!("&{}", ts.render_type(&tid, false)?)),
|
Some(format!("&{}", ts.render_type(&tid, UseContext::Return)?)),
|
||||||
Some("json".to_string()),
|
Some("json".to_string()),
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
|
@ -1131,7 +1157,7 @@ fn gen(api: &OpenAPI, ts: &mut TypeSpace) -> Result<String> {
|
||||||
|
|
||||||
let nam = ¶meter_data.name;
|
let nam = ¶meter_data.name;
|
||||||
let tid = ts.select(None, parameter_data.schema()?)?;
|
let tid = ts.select(None, parameter_data.schema()?)?;
|
||||||
let typ = ts.render_type(&tid, false)?;
|
let typ = ts.render_type(&tid, UseContext::Parameter)?;
|
||||||
a(&format!(" {}: {},", nam, typ));
|
a(&format!(" {}: {},", nam, typ));
|
||||||
}
|
}
|
||||||
openapiv3::Parameter::Query {
|
openapiv3::Parameter::Query {
|
||||||
|
@ -1157,7 +1183,7 @@ fn gen(api: &OpenAPI, ts: &mut TypeSpace) -> Result<String> {
|
||||||
*/
|
*/
|
||||||
ts.id_for_optional(&tid)
|
ts.id_for_optional(&tid)
|
||||||
};
|
};
|
||||||
let typ = ts.render_type(&tid, false)?;
|
let typ = ts.render_type(&tid, UseContext::Parameter)?;
|
||||||
a(&format!(" {}: {},", nam, typ));
|
a(&format!(" {}: {},", nam, typ));
|
||||||
|
|
||||||
query.push((nam.to_string(), !parameter_data.required));
|
query.push((nam.to_string(), !parameter_data.required));
|
||||||
|
@ -1212,7 +1238,7 @@ fn gen(api: &OpenAPI, ts: &mut TypeSpace) -> Result<String> {
|
||||||
let tid = ts.select(None, s)?;
|
let tid = ts.select(None, s)?;
|
||||||
a(&format!(
|
a(&format!(
|
||||||
" ) -> Result<{}> {{",
|
" ) -> Result<{}> {{",
|
||||||
ts.render_type(&tid, false)?
|
ts.render_type(&tid, UseContext::Return)?
|
||||||
));
|
));
|
||||||
} else {
|
} else {
|
||||||
bail!("media type encoding, no schema: {:#?}", mt);
|
bail!("media type encoding, no schema: {:#?}", mt);
|
||||||
|
|
Loading…
Reference in New Issue