Skip to content

Commit

Permalink
Fully qualify serde::Deserialize and serde::Serialize in outputs (#634)
Browse files Browse the repository at this point in the history
  • Loading branch information
novafacing authored Jul 30, 2024
1 parent c284d87 commit c8f9cf6
Show file tree
Hide file tree
Showing 33 changed files with 11,719 additions and 2,888 deletions.
4 changes: 1 addition & 3 deletions cargo-typify/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ is installed. Install rustfmt with rustup component add rustfmt
#![allow(clippy::match_single_binding)]
#![allow(clippy::clone_on_copy)]

use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Deserialize, Serialize)]
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
#[serde(untagged)]
pub enum IdOrName {
Id(uuid::Uuid),
Expand Down
2 changes: 0 additions & 2 deletions cargo-typify/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,6 @@ pub fn convert(args: &CliArgs) -> Result<String> {
#![allow(clippy::needless_lifetimes)]
#![allow(clippy::match_single_binding)]
#![allow(clippy::clone_on_copy)]
use serde::{Deserialize, Serialize};
";

let contents = format!("{intro}\n{}", type_space.to_stream());
Expand Down
10 changes: 4 additions & 6 deletions cargo-typify/tests/outputs/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
#![allow(clippy::match_single_binding)]
#![allow(clippy::clone_on_copy)]

use serde::{Deserialize, Serialize};

#[doc = r" Error types."]
pub mod error {
#[doc = r" Error from a TryFrom or FromStr implementation."]
Expand Down Expand Up @@ -41,7 +39,7 @@ pub mod error {
#[doc = "}"]
#[doc = r" ```"]
#[doc = r" </details>"]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[derive(Clone, Debug, serde :: Deserialize, serde :: Serialize)]
pub struct Fruit(pub serde_json::Map<String, serde_json::Value>);
impl std::ops::Deref for Fruit {
type Target = serde_json::Map<String, serde_json::Value>;
Expand Down Expand Up @@ -91,7 +89,7 @@ impl From<serde_json::Map<String, serde_json::Value>> for Fruit {
#[doc = "}"]
#[doc = r" ```"]
#[doc = r" </details>"]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[derive(Clone, Debug, serde :: Deserialize, serde :: Serialize)]
#[serde(untagged)]
pub enum FruitOrVeg {
Veg(Veggie),
Expand Down Expand Up @@ -136,7 +134,7 @@ impl From<Fruit> for FruitOrVeg {
#[doc = "}"]
#[doc = r" ```"]
#[doc = r" </details>"]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[derive(Clone, Debug, serde :: Deserialize, serde :: Serialize)]
pub struct Veggie {
#[doc = "Do I like this vegetable?"]
#[serde(rename = "veggieLike")]
Expand Down Expand Up @@ -182,7 +180,7 @@ impl Veggie {
#[doc = "}"]
#[doc = r" ```"]
#[doc = r" </details>"]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[derive(Clone, Debug, serde :: Deserialize, serde :: Serialize)]
pub struct Veggies {
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub fruits: Vec<String>,
Expand Down
10 changes: 4 additions & 6 deletions cargo-typify/tests/outputs/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
#![allow(clippy::match_single_binding)]
#![allow(clippy::clone_on_copy)]

use serde::{Deserialize, Serialize};

#[doc = r" Error types."]
pub mod error {
#[doc = r" Error from a TryFrom or FromStr implementation."]
Expand Down Expand Up @@ -41,7 +39,7 @@ pub mod error {
#[doc = "}"]
#[doc = r" ```"]
#[doc = r" </details>"]
#[derive(Clone, Debug, Deserialize, ExtraDerive, Serialize)]
#[derive(Clone, Debug, ExtraDerive, serde :: Deserialize, serde :: Serialize)]
pub struct Fruit(pub serde_json::Map<String, serde_json::Value>);
impl std::ops::Deref for Fruit {
type Target = serde_json::Map<String, serde_json::Value>;
Expand Down Expand Up @@ -91,7 +89,7 @@ impl From<serde_json::Map<String, serde_json::Value>> for Fruit {
#[doc = "}"]
#[doc = r" ```"]
#[doc = r" </details>"]
#[derive(Clone, Debug, Deserialize, ExtraDerive, Serialize)]
#[derive(Clone, Debug, ExtraDerive, serde :: Deserialize, serde :: Serialize)]
#[serde(untagged)]
pub enum FruitOrVeg {
Veg(Veggie),
Expand Down Expand Up @@ -136,7 +134,7 @@ impl From<Fruit> for FruitOrVeg {
#[doc = "}"]
#[doc = r" ```"]
#[doc = r" </details>"]
#[derive(Clone, Debug, Deserialize, ExtraDerive, Serialize)]
#[derive(Clone, Debug, ExtraDerive, serde :: Deserialize, serde :: Serialize)]
pub struct Veggie {
#[doc = "Do I like this vegetable?"]
#[serde(rename = "veggieLike")]
Expand Down Expand Up @@ -177,7 +175,7 @@ impl From<&Veggie> for Veggie {
#[doc = "}"]
#[doc = r" ```"]
#[doc = r" </details>"]
#[derive(Clone, Debug, Deserialize, ExtraDerive, Serialize)]
#[derive(Clone, Debug, ExtraDerive, serde :: Deserialize, serde :: Serialize)]
pub struct Veggies {
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub fruits: Vec<String>,
Expand Down
10 changes: 4 additions & 6 deletions cargo-typify/tests/outputs/multi_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
#![allow(clippy::match_single_binding)]
#![allow(clippy::clone_on_copy)]

use serde::{Deserialize, Serialize};

#[doc = r" Error types."]
pub mod error {
#[doc = r" Error from a TryFrom or FromStr implementation."]
Expand Down Expand Up @@ -41,7 +39,7 @@ pub mod error {
#[doc = "}"]
#[doc = r" ```"]
#[doc = r" </details>"]
#[derive(AnotherDerive, Clone, Debug, Deserialize, ExtraDerive, Serialize)]
#[derive(AnotherDerive, Clone, Debug, ExtraDerive, serde :: Deserialize, serde :: Serialize)]
pub struct Fruit(pub serde_json::Map<String, serde_json::Value>);
impl std::ops::Deref for Fruit {
type Target = serde_json::Map<String, serde_json::Value>;
Expand Down Expand Up @@ -91,7 +89,7 @@ impl From<serde_json::Map<String, serde_json::Value>> for Fruit {
#[doc = "}"]
#[doc = r" ```"]
#[doc = r" </details>"]
#[derive(AnotherDerive, Clone, Debug, Deserialize, ExtraDerive, Serialize)]
#[derive(AnotherDerive, Clone, Debug, ExtraDerive, serde :: Deserialize, serde :: Serialize)]
#[serde(untagged)]
pub enum FruitOrVeg {
Veg(Veggie),
Expand Down Expand Up @@ -136,7 +134,7 @@ impl From<Fruit> for FruitOrVeg {
#[doc = "}"]
#[doc = r" ```"]
#[doc = r" </details>"]
#[derive(AnotherDerive, Clone, Debug, Deserialize, ExtraDerive, Serialize)]
#[derive(AnotherDerive, Clone, Debug, ExtraDerive, serde :: Deserialize, serde :: Serialize)]
pub struct Veggie {
#[doc = "Do I like this vegetable?"]
#[serde(rename = "veggieLike")]
Expand Down Expand Up @@ -177,7 +175,7 @@ impl From<&Veggie> for Veggie {
#[doc = "}"]
#[doc = r" ```"]
#[doc = r" </details>"]
#[derive(AnotherDerive, Clone, Debug, Deserialize, ExtraDerive, Serialize)]
#[derive(AnotherDerive, Clone, Debug, ExtraDerive, serde :: Deserialize, serde :: Serialize)]
pub struct Veggies {
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub fruits: Vec<String>,
Expand Down
10 changes: 4 additions & 6 deletions cargo-typify/tests/outputs/no-builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
#![allow(clippy::match_single_binding)]
#![allow(clippy::clone_on_copy)]

use serde::{Deserialize, Serialize};

#[doc = r" Error types."]
pub mod error {
#[doc = r" Error from a TryFrom or FromStr implementation."]
Expand Down Expand Up @@ -41,7 +39,7 @@ pub mod error {
#[doc = "}"]
#[doc = r" ```"]
#[doc = r" </details>"]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[derive(Clone, Debug, serde :: Deserialize, serde :: Serialize)]
pub struct Fruit(pub serde_json::Map<String, serde_json::Value>);
impl std::ops::Deref for Fruit {
type Target = serde_json::Map<String, serde_json::Value>;
Expand Down Expand Up @@ -91,7 +89,7 @@ impl From<serde_json::Map<String, serde_json::Value>> for Fruit {
#[doc = "}"]
#[doc = r" ```"]
#[doc = r" </details>"]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[derive(Clone, Debug, serde :: Deserialize, serde :: Serialize)]
#[serde(untagged)]
pub enum FruitOrVeg {
Veg(Veggie),
Expand Down Expand Up @@ -136,7 +134,7 @@ impl From<Fruit> for FruitOrVeg {
#[doc = "}"]
#[doc = r" ```"]
#[doc = r" </details>"]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[derive(Clone, Debug, serde :: Deserialize, serde :: Serialize)]
pub struct Veggie {
#[doc = "Do I like this vegetable?"]
#[serde(rename = "veggieLike")]
Expand Down Expand Up @@ -177,7 +175,7 @@ impl From<&Veggie> for Veggie {
#[doc = "}"]
#[doc = r" ```"]
#[doc = r" </details>"]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[derive(Clone, Debug, serde :: Deserialize, serde :: Serialize)]
pub struct Veggies {
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub fruits: Vec<String>,
Expand Down
7 changes: 2 additions & 5 deletions example-build/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@ fn main() {
let mut type_space = TypeSpace::new(TypeSpaceSettings::default().with_struct_builder(true));
type_space.add_root_schema(schema).unwrap();

let contents = format!(
"{}\n{}",
"use serde::{Deserialize, Serialize};",
prettyplease::unparse(&syn::parse2::<syn::File>(type_space.to_stream()).unwrap())
);
let contents =
prettyplease::unparse(&syn::parse2::<syn::File>(type_space.to_stream()).unwrap());

let mut out_file = Path::new(&env::var("OUT_DIR").unwrap()).to_path_buf();
out_file.push("codegen.rs");
Expand Down
4 changes: 2 additions & 2 deletions typify-impl/src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1467,7 +1467,7 @@ mod tests {
)*
/// ```
/// </details>
#[derive(Clone, Debug, Deserialize, Serialize)]
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
pub enum ResultX {
Ok(u32),
Err(String),
Expand Down Expand Up @@ -1519,7 +1519,7 @@ mod tests {
#[doc = "true"]
/// ```
/// </details>
#[derive(A, B, C, Clone, D, Debug, Deserialize, Serialize)]
#[derive(A, B, C, Clone, D, Debug, serde::Deserialize, serde::Serialize)]
pub enum ResultX {
Ok(u32),
Err(String),
Expand Down
6 changes: 3 additions & 3 deletions typify-impl/src/type_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ impl TypeEntry {
}

pub(crate) fn output(&self, type_space: &TypeSpace, output: &mut OutputSpace) {
let derive_set = ["Serialize", "Deserialize", "Debug", "Clone"]
let derive_set = ["serde::Serialize", "serde::Deserialize", "Debug", "Clone"]
.into_iter()
.collect::<BTreeSet<_>>();

Expand Down Expand Up @@ -1344,7 +1344,7 @@ impl TypeEntry {

// We're going to impl Deserialize so we can remove it
// from the set of derived impls.
derive_set.remove("Deserialize");
derive_set.remove("serde::Deserialize");

// TODO: if a user were to derive schemars::JsonSchema, it
// wouldn't be accurate.
Expand Down Expand Up @@ -1428,7 +1428,7 @@ impl TypeEntry {

// We're going to impl Deserialize so we can remove it
// from the set of derived impls.
derive_set.remove("Deserialize");
derive_set.remove("serde::Deserialize");

// TODO: if a user were to derive schemars::JsonSchema, it
// wouldn't be accurate.
Expand Down
27 changes: 23 additions & 4 deletions typify-impl/tests/generator.out
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,16 @@ mod types {
#[doc = r" ```"]
#[doc = r" </details>"]
#[derive(
Clone, Debug, Deserialize, Eq, Hash, JsonSchema, Ord, PartialEq, PartialOrd, Serialize,
Clone,
Debug,
Eq,
Hash,
JsonSchema,
Ord,
PartialEq,
PartialOrd,
serde :: Deserialize,
serde :: Serialize,
)]
pub struct AllTheTraits {
pub ok: String,
Expand Down Expand Up @@ -85,7 +94,7 @@ mod types {
#[doc = "}"]
#[doc = r" ```"]
#[doc = r" </details>"]
#[derive(Clone, Debug, Deserialize, JsonSchema, Serialize)]
#[derive(Clone, Debug, JsonSchema, serde :: Deserialize, serde :: Serialize)]
pub struct CompoundType {
pub value1: String,
pub value2: u64,
Expand Down Expand Up @@ -121,7 +130,7 @@ mod types {
#[doc = "}"]
#[doc = r" ```"]
#[doc = r" </details>"]
#[derive(Clone, Debug, Deserialize, JsonSchema, Serialize)]
#[derive(Clone, Debug, JsonSchema, serde :: Deserialize, serde :: Serialize)]
pub struct Pair {
#[serde(default = "defaults::pair_a")]
pub a: StringEnum,
Expand Down Expand Up @@ -154,7 +163,17 @@ mod types {
#[doc = r" ```"]
#[doc = r" </details>"]
#[derive(
Clone, Copy, Debug, Deserialize, Eq, Hash, JsonSchema, Ord, PartialEq, PartialOrd, Serialize,
Clone,
Copy,
Debug,
Eq,
Hash,
JsonSchema,
Ord,
PartialEq,
PartialOrd,
serde :: Deserialize,
serde :: Serialize,
)]
pub enum StringEnum {
One,
Expand Down
Loading

0 comments on commit c8f9cf6

Please sign in to comment.