From 304665889c85a0ec6e4d8ff1141fdd812519fceb Mon Sep 17 00:00:00 2001 From: Josiah Bull Date: Sat, 7 Dec 2024 09:30:36 +1300 Subject: [PATCH] test: added maps_custom test case to schemas. --- typify/tests/schemas.rs | 26 ++- typify/tests/schemas/maps_custom.rs | 298 ++++++++++++++++++++++++++++ 2 files changed, 319 insertions(+), 5 deletions(-) create mode 100644 typify/tests/schemas/maps_custom.rs diff --git a/typify/tests/schemas.rs b/typify/tests/schemas.rs index 4b46415c..78dfc8f2 100644 --- a/typify/tests/schemas.rs +++ b/typify/tests/schemas.rs @@ -15,17 +15,33 @@ fn test_schemas() { env_logger::init(); // Make sure output is up to date. for entry in glob("tests/schemas/*.json").expect("Failed to read glob pattern") { - validate_schema(entry.unwrap()).unwrap(); + let entry = entry.unwrap(); + let out_path = entry.clone().with_extension("rs"); + validate_schema(entry, out_path, &mut TypeSpaceSettings::default()).unwrap(); } // Make sure it all compiles. trybuild::TestCases::new().pass("tests/schemas/*.rs"); } -fn validate_schema(path: std::path::PathBuf) -> Result<(), Box> { - let mut out_path = path.clone(); - out_path.set_extension("rs"); +/// Ensure that setting the global config to use a custom map type works. +#[test] +fn test_custom_map() { + validate_schema( + "tests/schemas/maps.json".into(), + "tests/schemas/maps_custom.rs".into(), + TypeSpaceSettings::default().with_map_type("std::collections::BTreeMap".to_string()), + ) + .unwrap(); + + trybuild::TestCases::new().pass("tests/schemas/maps_custom.rs"); +} +fn validate_schema( + path: std::path::PathBuf, + out_path: std::path::PathBuf, + typespace: &mut TypeSpaceSettings, +) -> Result<(), Box> { let file = File::open(path)?; let reader = BufReader::new(file); @@ -40,7 +56,7 @@ fn validate_schema(path: std::path::PathBuf) -> Result<(), Box> { let schema = serde_json::from_value(schema_raw).unwrap(); let mut type_space = TypeSpace::new( - TypeSpaceSettings::default() + typespace .with_replacement( "HandGeneratedType", "String", diff --git a/typify/tests/schemas/maps_custom.rs b/typify/tests/schemas/maps_custom.rs new file mode 100644 index 00000000..5094dae0 --- /dev/null +++ b/typify/tests/schemas/maps_custom.rs @@ -0,0 +1,298 @@ +#[doc = r" Error types."] +pub mod error { + #[doc = r" Error from a TryFrom or FromStr implementation."] + pub struct ConversionError(::std::borrow::Cow<'static, str>); + impl ::std::error::Error for ConversionError {} + impl ::std::fmt::Display for ConversionError { + fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> Result<(), ::std::fmt::Error> { + ::std::fmt::Display::fmt(&self.0, f) + } + } + impl ::std::fmt::Debug for ConversionError { + fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> Result<(), ::std::fmt::Error> { + ::std::fmt::Debug::fmt(&self.0, f) + } + } + impl From<&'static str> for ConversionError { + fn from(value: &'static str) -> Self { + Self(value.into()) + } + } + impl From for ConversionError { + fn from(value: String) -> Self { + Self(value.into()) + } + } +} +#[doc = "DeadSimple"] +#[doc = r""] +#[doc = r"
JSON schema"] +#[doc = r""] +#[doc = r" ```json"] +#[doc = "{"] +#[doc = " \"title\": \"DeadSimple\","] +#[doc = " \"type\": \"object\","] +#[doc = " \"$comment\": \"usual case of a map whose name must come from its title\""] +#[doc = "}"] +#[doc = r" ```"] +#[doc = r"
"] +#[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug)] +pub struct DeadSimple(pub ::serde_json::Map<::std::string::String, ::serde_json::Value>); +impl ::std::ops::Deref for DeadSimple { + 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 for ::serde_json::Map<::std::string::String, ::serde_json::Value> { + fn from(value: DeadSimple) -> Self { + value.0 + } +} +impl From<&DeadSimple> for DeadSimple { + fn from(value: &DeadSimple) -> Self { + value.clone() + } +} +impl From<::serde_json::Map<::std::string::String, ::serde_json::Value>> for DeadSimple { + fn from(value: ::serde_json::Map<::std::string::String, ::serde_json::Value>) -> Self { + Self(value) + } +} +#[doc = "Eh"] +#[doc = r""] +#[doc = r"
JSON schema"] +#[doc = r""] +#[doc = r" ```json"] +#[doc = "{"] +#[doc = " \"type\": \"string\","] +#[doc = " \"format\": \"^a*$\""] +#[doc = "}"] +#[doc = r" ```"] +#[doc = r"
"] +#[derive( + :: serde :: Deserialize, + :: serde :: Serialize, + Clone, + Debug, + Eq, + Hash, + Ord, + PartialEq, + PartialOrd, +)] +pub struct Eh(pub ::std::string::String); +impl ::std::ops::Deref for Eh { + type Target = ::std::string::String; + fn deref(&self) -> &::std::string::String { + &self.0 + } +} +impl From for ::std::string::String { + fn from(value: Eh) -> Self { + value.0 + } +} +impl From<&Eh> for Eh { + fn from(value: &Eh) -> Self { + value.clone() + } +} +impl From<::std::string::String> for Eh { + fn from(value: ::std::string::String) -> Self { + Self(value) + } +} +impl ::std::str::FromStr for Eh { + type Err = ::std::convert::Infallible; + fn from_str(value: &str) -> ::std::result::Result { + Ok(Self(value.to_string())) + } +} +impl ::std::fmt::Display for Eh { + fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { + self.0.fmt(f) + } +} +#[doc = "MapWithDateKeys"] +#[doc = r""] +#[doc = r"
JSON schema"] +#[doc = r""] +#[doc = r" ```json"] +#[doc = "{"] +#[doc = " \"type\": \"object\","] +#[doc = " \"additionalProperties\": {"] +#[doc = " \"$ref\": \"#/definitions/Value\""] +#[doc = " },"] +#[doc = " \"propertyNames\": {"] +#[doc = " \"format\": \"date\""] +#[doc = " },"] +#[doc = " \"$comment\": \"test that a type isn't needed for propertyNames\""] +#[doc = "}"] +#[doc = r" ```"] +#[doc = r"
"] +#[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug)] +pub struct MapWithDateKeys(pub std::collections::BTreeMap); +impl ::std::ops::Deref for MapWithDateKeys { + type Target = std::collections::BTreeMap; + fn deref(&self) -> &std::collections::BTreeMap { + &self.0 + } +} +impl From for std::collections::BTreeMap { + fn from(value: MapWithDateKeys) -> Self { + value.0 + } +} +impl From<&MapWithDateKeys> for MapWithDateKeys { + fn from(value: &MapWithDateKeys) -> Self { + value.clone() + } +} +impl From> for MapWithDateKeys { + fn from(value: std::collections::BTreeMap) -> Self { + Self(value) + } +} +#[doc = "MapWithDateTimeKeys"] +#[doc = r""] +#[doc = r"
JSON schema"] +#[doc = r""] +#[doc = r" ```json"] +#[doc = "{"] +#[doc = " \"type\": \"object\","] +#[doc = " \"additionalProperties\": {"] +#[doc = " \"$ref\": \"#/definitions/Value\""] +#[doc = " },"] +#[doc = " \"propertyNames\": {"] +#[doc = " \"format\": \"date-time\""] +#[doc = " },"] +#[doc = " \"$comment\": \"test that a type isn't needed for propertyNames\""] +#[doc = "}"] +#[doc = r" ```"] +#[doc = r"
"] +#[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug)] +pub struct MapWithDateTimeKeys( + pub std::collections::BTreeMap, Value>, +); +impl ::std::ops::Deref for MapWithDateTimeKeys { + type Target = std::collections::BTreeMap, Value>; + fn deref(&self) -> &std::collections::BTreeMap, Value> { + &self.0 + } +} +impl From + for std::collections::BTreeMap, Value> +{ + fn from(value: MapWithDateTimeKeys) -> Self { + value.0 + } +} +impl From<&MapWithDateTimeKeys> for MapWithDateTimeKeys { + fn from(value: &MapWithDateTimeKeys) -> Self { + value.clone() + } +} +impl From, Value>> + for MapWithDateTimeKeys +{ + fn from( + value: std::collections::BTreeMap, Value>, + ) -> Self { + Self(value) + } +} +#[doc = "MapWithKeys"] +#[doc = r""] +#[doc = r"
JSON schema"] +#[doc = r""] +#[doc = r" ```json"] +#[doc = "{"] +#[doc = " \"type\": \"object\","] +#[doc = " \"additionalProperties\": {"] +#[doc = " \"$ref\": \"#/definitions/Value\""] +#[doc = " },"] +#[doc = " \"propertyNames\": {"] +#[doc = " \"$ref\": \"#/definitions/Eh\""] +#[doc = " }"] +#[doc = "}"] +#[doc = r" ```"] +#[doc = r"
"] +#[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug)] +pub struct MapWithKeys(pub std::collections::BTreeMap); +impl ::std::ops::Deref for MapWithKeys { + type Target = std::collections::BTreeMap; + fn deref(&self) -> &std::collections::BTreeMap { + &self.0 + } +} +impl From for std::collections::BTreeMap { + fn from(value: MapWithKeys) -> Self { + value.0 + } +} +impl From<&MapWithKeys> for MapWithKeys { + fn from(value: &MapWithKeys) -> Self { + value.clone() + } +} +impl From> for MapWithKeys { + fn from(value: std::collections::BTreeMap) -> Self { + Self(value) + } +} +#[doc = "Value"] +#[doc = r""] +#[doc = r"
JSON schema"] +#[doc = r""] +#[doc = r" ```json"] +#[doc = "{"] +#[doc = " \"type\": \"string\""] +#[doc = "}"] +#[doc = r" ```"] +#[doc = r"
"] +#[derive( + :: serde :: Deserialize, + :: serde :: Serialize, + Clone, + Debug, + Eq, + Hash, + Ord, + PartialEq, + PartialOrd, +)] +pub struct Value(pub ::std::string::String); +impl ::std::ops::Deref for Value { + type Target = ::std::string::String; + fn deref(&self) -> &::std::string::String { + &self.0 + } +} +impl From for ::std::string::String { + fn from(value: Value) -> Self { + value.0 + } +} +impl From<&Value> for Value { + fn from(value: &Value) -> Self { + value.clone() + } +} +impl From<::std::string::String> for Value { + fn from(value: ::std::string::String) -> Self { + Self(value) + } +} +impl ::std::str::FromStr for Value { + type Err = ::std::convert::Infallible; + fn from_str(value: &str) -> ::std::result::Result { + Ok(Self(value.to_string())) + } +} +impl ::std::fmt::Display for Value { + fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { + self.0.fmt(f) + } +} +fn main() {}