Skip to content

Commit

Permalink
Fix codegen issues w/ types that conflict with Rust types inc Result (
Browse files Browse the repository at this point in the history
  • Loading branch information
anelson authored Dec 2, 2024
1 parent 1134fee commit f409d37
Show file tree
Hide file tree
Showing 35 changed files with 24,059 additions and 13,643 deletions.
45 changes: 23 additions & 22 deletions cargo-typify/tests/outputs/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ pub mod error {
#[doc = r" ```"]
#[doc = r" </details>"]
#[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug)]
pub struct Fruit(pub ::serde_json::Map<String, ::serde_json::Value>);
pub struct Fruit(pub ::serde_json::Map<::std::string::String, ::serde_json::Value>);
impl ::std::ops::Deref for Fruit {
type Target = ::serde_json::Map<String, ::serde_json::Value>;
fn deref(&self) -> &::serde_json::Map<String, ::serde_json::Value> {
type Target = ::serde_json::Map<::std::string::String, ::serde_json::Value>;
fn deref(&self) -> &::serde_json::Map<::std::string::String, ::serde_json::Value> {
&self.0
}
}
impl From<Fruit> for ::serde_json::Map<String, ::serde_json::Value> {
impl From<Fruit> for ::serde_json::Map<::std::string::String, ::serde_json::Value> {
fn from(value: Fruit) -> Self {
value.0
}
Expand All @@ -57,8 +57,8 @@ impl From<&Fruit> for Fruit {
value.clone()
}
}
impl From<::serde_json::Map<String, ::serde_json::Value>> for Fruit {
fn from(value: ::serde_json::Map<String, ::serde_json::Value>) -> Self {
impl From<::serde_json::Map<::std::string::String, ::serde_json::Value>> for Fruit {
fn from(value: ::serde_json::Map<::std::string::String, ::serde_json::Value>) -> Self {
Self(value)
}
}
Expand Down Expand Up @@ -141,7 +141,7 @@ pub struct Veggie {
pub veggie_like: bool,
#[doc = "The name of the vegetable."]
#[serde(rename = "veggieName")]
pub veggie_name: String,
pub veggie_name: ::std::string::String,
}
impl From<&Veggie> for Veggie {
fn from(value: &Veggie) -> Self {
Expand Down Expand Up @@ -182,10 +182,10 @@ impl Veggie {
#[doc = r" </details>"]
#[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug)]
pub struct Veggies {
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub fruits: Vec<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub vegetables: Vec<Veggie>,
#[serde(default, skip_serializing_if = "::std::vec::Vec::is_empty")]
pub fruits: ::std::vec::Vec<::std::string::String>,
#[serde(default, skip_serializing_if = "::std::vec::Vec::is_empty")]
pub vegetables: ::std::vec::Vec<Veggie>,
}
impl From<&Veggies> for Veggies {
fn from(value: &Veggies) -> Self {
Expand All @@ -201,8 +201,8 @@ impl Veggies {
pub mod builder {
#[derive(Clone, Debug)]
pub struct Veggie {
veggie_like: Result<bool, String>,
veggie_name: Result<String, String>,
veggie_like: ::std::result::Result<bool, ::std::string::String>,
veggie_name: ::std::result::Result<::std::string::String, ::std::string::String>,
}
impl Default for Veggie {
fn default() -> Self {
Expand All @@ -225,7 +225,7 @@ pub mod builder {
}
pub fn veggie_name<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<String>,
T: std::convert::TryInto<::std::string::String>,
T::Error: std::fmt::Display,
{
self.veggie_name = value
Expand All @@ -234,9 +234,9 @@ pub mod builder {
self
}
}
impl std::convert::TryFrom<Veggie> for super::Veggie {
impl ::std::convert::TryFrom<Veggie> for super::Veggie {
type Error = super::error::ConversionError;
fn try_from(value: Veggie) -> Result<Self, super::error::ConversionError> {
fn try_from(value: Veggie) -> ::std::result::Result<Self, super::error::ConversionError> {
Ok(Self {
veggie_like: value.veggie_like?,
veggie_name: value.veggie_name?,
Expand All @@ -253,8 +253,9 @@ pub mod builder {
}
#[derive(Clone, Debug)]
pub struct Veggies {
fruits: Result<Vec<String>, String>,
vegetables: Result<Vec<super::Veggie>, String>,
fruits:
::std::result::Result<::std::vec::Vec<::std::string::String>, ::std::string::String>,
vegetables: ::std::result::Result<::std::vec::Vec<super::Veggie>, ::std::string::String>,
}
impl Default for Veggies {
fn default() -> Self {
Expand All @@ -267,7 +268,7 @@ pub mod builder {
impl Veggies {
pub fn fruits<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<Vec<String>>,
T: std::convert::TryInto<::std::vec::Vec<::std::string::String>>,
T::Error: std::fmt::Display,
{
self.fruits = value
Expand All @@ -277,7 +278,7 @@ pub mod builder {
}
pub fn vegetables<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<Vec<super::Veggie>>,
T: std::convert::TryInto<::std::vec::Vec<super::Veggie>>,
T::Error: std::fmt::Display,
{
self.vegetables = value
Expand All @@ -286,9 +287,9 @@ pub mod builder {
self
}
}
impl std::convert::TryFrom<Veggies> for super::Veggies {
impl ::std::convert::TryFrom<Veggies> for super::Veggies {
type Error = super::error::ConversionError;
fn try_from(value: Veggies) -> Result<Self, super::error::ConversionError> {
fn try_from(value: Veggies) -> ::std::result::Result<Self, super::error::ConversionError> {
Ok(Self {
fruits: value.fruits?,
vegetables: value.vegetables?,
Expand Down
22 changes: 11 additions & 11 deletions cargo-typify/tests/outputs/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ pub mod error {
#[doc = r" ```"]
#[doc = r" </details>"]
#[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug, ExtraDerive)]
pub struct Fruit(pub ::serde_json::Map<String, ::serde_json::Value>);
pub struct Fruit(pub ::serde_json::Map<::std::string::String, ::serde_json::Value>);
impl ::std::ops::Deref for Fruit {
type Target = ::serde_json::Map<String, ::serde_json::Value>;
fn deref(&self) -> &::serde_json::Map<String, ::serde_json::Value> {
type Target = ::serde_json::Map<::std::string::String, ::serde_json::Value>;
fn deref(&self) -> &::serde_json::Map<::std::string::String, ::serde_json::Value> {
&self.0
}
}
impl From<Fruit> for ::serde_json::Map<String, ::serde_json::Value> {
impl From<Fruit> for ::serde_json::Map<::std::string::String, ::serde_json::Value> {
fn from(value: Fruit) -> Self {
value.0
}
Expand All @@ -57,8 +57,8 @@ impl From<&Fruit> for Fruit {
value.clone()
}
}
impl From<::serde_json::Map<String, ::serde_json::Value>> for Fruit {
fn from(value: ::serde_json::Map<String, ::serde_json::Value>) -> Self {
impl From<::serde_json::Map<::std::string::String, ::serde_json::Value>> for Fruit {
fn from(value: ::serde_json::Map<::std::string::String, ::serde_json::Value>) -> Self {
Self(value)
}
}
Expand Down Expand Up @@ -141,7 +141,7 @@ pub struct Veggie {
pub veggie_like: bool,
#[doc = "The name of the vegetable."]
#[serde(rename = "veggieName")]
pub veggie_name: String,
pub veggie_name: ::std::string::String,
}
impl From<&Veggie> for Veggie {
fn from(value: &Veggie) -> Self {
Expand Down Expand Up @@ -177,10 +177,10 @@ impl From<&Veggie> for Veggie {
#[doc = r" </details>"]
#[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug, ExtraDerive)]
pub struct Veggies {
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub fruits: Vec<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub vegetables: Vec<Veggie>,
#[serde(default, skip_serializing_if = "::std::vec::Vec::is_empty")]
pub fruits: ::std::vec::Vec<::std::string::String>,
#[serde(default, skip_serializing_if = "::std::vec::Vec::is_empty")]
pub vegetables: ::std::vec::Vec<Veggie>,
}
impl From<&Veggies> for Veggies {
fn from(value: &Veggies) -> Self {
Expand Down
22 changes: 11 additions & 11 deletions cargo-typify/tests/outputs/multi_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ pub mod error {
#[derive(
:: serde :: Deserialize, :: serde :: Serialize, AnotherDerive, Clone, Debug, ExtraDerive,
)]
pub struct Fruit(pub ::serde_json::Map<String, ::serde_json::Value>);
pub struct Fruit(pub ::serde_json::Map<::std::string::String, ::serde_json::Value>);
impl ::std::ops::Deref for Fruit {
type Target = ::serde_json::Map<String, ::serde_json::Value>;
fn deref(&self) -> &::serde_json::Map<String, ::serde_json::Value> {
type Target = ::serde_json::Map<::std::string::String, ::serde_json::Value>;
fn deref(&self) -> &::serde_json::Map<::std::string::String, ::serde_json::Value> {
&self.0
}
}
impl From<Fruit> for ::serde_json::Map<String, ::serde_json::Value> {
impl From<Fruit> for ::serde_json::Map<::std::string::String, ::serde_json::Value> {
fn from(value: Fruit) -> Self {
value.0
}
Expand All @@ -59,8 +59,8 @@ impl From<&Fruit> for Fruit {
value.clone()
}
}
impl From<::serde_json::Map<String, ::serde_json::Value>> for Fruit {
fn from(value: ::serde_json::Map<String, ::serde_json::Value>) -> Self {
impl From<::serde_json::Map<::std::string::String, ::serde_json::Value>> for Fruit {
fn from(value: ::serde_json::Map<::std::string::String, ::serde_json::Value>) -> Self {
Self(value)
}
}
Expand Down Expand Up @@ -147,7 +147,7 @@ pub struct Veggie {
pub veggie_like: bool,
#[doc = "The name of the vegetable."]
#[serde(rename = "veggieName")]
pub veggie_name: String,
pub veggie_name: ::std::string::String,
}
impl From<&Veggie> for Veggie {
fn from(value: &Veggie) -> Self {
Expand Down Expand Up @@ -185,10 +185,10 @@ impl From<&Veggie> for Veggie {
:: serde :: Deserialize, :: serde :: Serialize, AnotherDerive, Clone, Debug, ExtraDerive,
)]
pub struct Veggies {
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub fruits: Vec<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub vegetables: Vec<Veggie>,
#[serde(default, skip_serializing_if = "::std::vec::Vec::is_empty")]
pub fruits: ::std::vec::Vec<::std::string::String>,
#[serde(default, skip_serializing_if = "::std::vec::Vec::is_empty")]
pub vegetables: ::std::vec::Vec<Veggie>,
}
impl From<&Veggies> for Veggies {
fn from(value: &Veggies) -> Self {
Expand Down
22 changes: 11 additions & 11 deletions cargo-typify/tests/outputs/no-builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ pub mod error {
#[doc = r" ```"]
#[doc = r" </details>"]
#[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug)]
pub struct Fruit(pub ::serde_json::Map<String, ::serde_json::Value>);
pub struct Fruit(pub ::serde_json::Map<::std::string::String, ::serde_json::Value>);
impl ::std::ops::Deref for Fruit {
type Target = ::serde_json::Map<String, ::serde_json::Value>;
fn deref(&self) -> &::serde_json::Map<String, ::serde_json::Value> {
type Target = ::serde_json::Map<::std::string::String, ::serde_json::Value>;
fn deref(&self) -> &::serde_json::Map<::std::string::String, ::serde_json::Value> {
&self.0
}
}
impl From<Fruit> for ::serde_json::Map<String, ::serde_json::Value> {
impl From<Fruit> for ::serde_json::Map<::std::string::String, ::serde_json::Value> {
fn from(value: Fruit) -> Self {
value.0
}
Expand All @@ -57,8 +57,8 @@ impl From<&Fruit> for Fruit {
value.clone()
}
}
impl From<::serde_json::Map<String, ::serde_json::Value>> for Fruit {
fn from(value: ::serde_json::Map<String, ::serde_json::Value>) -> Self {
impl From<::serde_json::Map<::std::string::String, ::serde_json::Value>> for Fruit {
fn from(value: ::serde_json::Map<::std::string::String, ::serde_json::Value>) -> Self {
Self(value)
}
}
Expand Down Expand Up @@ -141,7 +141,7 @@ pub struct Veggie {
pub veggie_like: bool,
#[doc = "The name of the vegetable."]
#[serde(rename = "veggieName")]
pub veggie_name: String,
pub veggie_name: ::std::string::String,
}
impl From<&Veggie> for Veggie {
fn from(value: &Veggie) -> Self {
Expand Down Expand Up @@ -177,10 +177,10 @@ impl From<&Veggie> for Veggie {
#[doc = r" </details>"]
#[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug)]
pub struct Veggies {
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub fruits: Vec<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub vegetables: Vec<Veggie>,
#[serde(default, skip_serializing_if = "::std::vec::Vec::is_empty")]
pub fruits: ::std::vec::Vec<::std::string::String>,
#[serde(default, skip_serializing_if = "::std::vec::Vec::is_empty")]
pub vegetables: ::std::vec::Vec<Veggie>,
}
impl From<&Veggies> for Veggies {
fn from(value: &Veggies) -> Self {
Expand Down
2 changes: 1 addition & 1 deletion typify-impl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ paste = "1.0.15"
rustfmt-wrapper = "0.2.1"
schema = "0.1.0"
schemars = { version = "0.8.21", features = ["uuid1", "impl_json_schema"] }
syn = { version = "2.0.90", features = ["full", "extra-traits"] }
syn = { version = "2.0.90", features = ["full", "extra-traits", "visit-mut"] }
uuid = "1.11.0"
4 changes: 2 additions & 2 deletions typify-impl/src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1470,7 +1470,7 @@ mod tests {
#[derive(::serde::Deserialize, ::serde::Serialize, Clone, Debug)]
pub enum ResultX {
Ok(u32),
Err(String),
Err(::std::string::String),
}

impl From<&ResultX> for ResultX {
Expand Down Expand Up @@ -1522,7 +1522,7 @@ mod tests {
#[derive(::serde::Deserialize, ::serde::Serialize, A, B, C, Clone, D, Debug)]
pub enum ResultX {
Ok(u32),
Err(String),
Err(::std::string::String),
}

impl From<&ResultX> for ResultX {
Expand Down
3 changes: 2 additions & 1 deletion typify-impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1212,7 +1212,8 @@ mod tests {
type_space.add_root_schema(schema).unwrap();
let tokens = type_space.to_stream().to_string();
println!("{}", tokens);
assert!(tokens.contains(" pub struct Somename { pub someproperty : String , }"))
assert!(tokens
.contains(" pub struct Somename { pub someproperty : :: std :: string :: String , }"))
}

#[test]
Expand Down
9 changes: 6 additions & 3 deletions typify-impl/src/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,12 +373,12 @@ pub(crate) fn generate_serde_attr(
let default_fn = match (state, &prop_type.details) {
(StructPropertyState::Optional, TypeEntryDetails::Option(_)) => {
serde_options.push(quote! { default });
serde_options.push(quote! { skip_serializing_if = "Option::is_none" });
serde_options.push(quote! { skip_serializing_if = "::std::option::Option::is_none" });
DefaultFunction::Default
}
(StructPropertyState::Optional, TypeEntryDetails::Vec(_)) => {
serde_options.push(quote! { default });
serde_options.push(quote! { skip_serializing_if = "Vec::is_empty" });
serde_options.push(quote! { skip_serializing_if = "::std::vec::Vec::is_empty" });
DefaultFunction::Default
}
(StructPropertyState::Optional, TypeEntryDetails::Map(key_id, value_id)) => {
Expand Down Expand Up @@ -588,6 +588,9 @@ mod tests {
let mut type_space = TypeSpace::default();
let (ty, _) = type_space.convert_schema(Name::Unknown, &schema).unwrap();
let output = ty.type_name(&type_space).replace(" ", "");
assert_eq!(output, "::serde_json::Map<String,::serde_json::Value>");
assert_eq!(
output,
"::serde_json::Map<::std::string::String,::serde_json::Value>"
);
}
}
Loading

0 comments on commit f409d37

Please sign in to comment.