Skip to content

Support telling serde where it is, i.e. when re-exported as a transitive dependency. #108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions typify-impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ pub struct TypeSpaceSettings {
type_mod: Option<String>,
extra_derives: Vec<String>,
struct_builder: bool,
serde_crate_location: Option<String>,
}

impl TypeSpaceSettings {
Expand All @@ -192,6 +193,11 @@ impl TypeSpaceSettings {
self.struct_builder = struct_builder;
self
}

pub fn with_serde_crate_location(&mut self, crate_location: String) -> &mut Self {
self.serde_crate_location = Some(crate_location);
self
}
}

impl TypeSpace {
Expand Down
58 changes: 58 additions & 0 deletions typify-impl/src/type_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,9 @@ impl TypeEntry {
if *deny_unknown_fields {
serde_options.push(quote! { deny_unknown_fields });
}
if let Some(crate_loc) = &type_space.settings.serde_crate_location {
serde_options.push(quote! { crate = #crate_loc });
}

let serde = (!serde_options.is_empty()).then(|| {
quote! { #[serde( #( #serde_options ),* )] }
Expand Down Expand Up @@ -581,6 +584,9 @@ impl TypeEntry {
if *deny_unknown_fields {
serde_options.push(quote! { deny_unknown_fields });
}
if let Some(crate_loc) = &type_space.settings.serde_crate_location {
serde_options.push(quote! { crate = #crate_loc });
}
let serde =
(!serde_options.is_empty()).then(|| quote! { #[serde( #( #serde_options ),* )] });

Expand Down Expand Up @@ -1182,6 +1188,9 @@ mod tests {
type_entry::{TypeEntry, TypeEntryStruct},
TypeEntryDetails, TypeSpace,
};
use quote::quote;
use schema::Schema;
use schemars::JsonSchema;

#[test]
fn test_ident() {
Expand Down Expand Up @@ -1225,4 +1234,53 @@ mod tests {
let parameter = t.type_parameter_ident(&ts, Some("a"));
assert_eq!(parameter.to_string(), "& 'a SomeType");
}

#[test]
fn test_alternate_crate_location() {
#[allow(dead_code)]
#[derive(Schema, JsonSchema)]
struct MyStruct {
my_enum: MyEnum,
}
#[allow(dead_code)]
#[derive(Schema, JsonSchema)]
enum MyEnum {
A,
}

let (mut type_space, _) = crate::test_util::get_type::<MyStruct>();
type_space
.settings
.with_serde_crate_location("crate::serde_reexport".to_string());
let actual = type_space.to_stream();
let expected = quote! {
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(crate = "crate::serde_reexport")]
pub enum MyEnum {
A,
}
impl ToString for MyEnum {
fn to_string(&self) -> String {
match *self {
Self::A => "A".to_string(),
}
}
}
impl std::str::FromStr for MyEnum {
type Err = &'static str;
fn from_str(value: &str) -> Result<Self, Self::Err> {
match value {
"A" => Ok(Self::A),
_ => Err("invalid value"),
}
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(crate = "crate::serde_reexport")]
pub struct MyStruct {
pub my_enum: MyEnum,
}
};
assert_eq!(actual.to_string(), expected.to_string());
}
}