diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 2ef6e39b..aa841d86 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -15,6 +15,8 @@ https://github.com/oxidecomputer/typify/compare/v0.10.1\...HEAD[Full list of commits] +* Handle the `const` keyword (#1115) + == 0.10.1 (released 2026-11-16) https://github.com/oxidecomputer/typify/compare/v0.10.0\...v0.10.1[Full list of commits] diff --git a/typify-impl/src/convert.rs b/typify-impl/src/convert.rs index e03e1af5..f830e7d9 100644 --- a/typify-impl/src/convert.rs +++ b/typify-impl/src/convert.rs @@ -53,6 +53,36 @@ impl TypeSpace { } match schema { + // If both const and enum are present and the enumerated values + // *don't* contain the const value, then there's no value that + // satisfies this schema. + SchemaObject { + metadata, + const_value: Some(const_value), + enum_values: Some(enum_values), + .. + } if !enum_values.contains(const_value) => { + let (type_entry, _) = self.convert_never(type_name, original_schema)?; + Ok((type_entry, metadata)) + } + + // Treat const like a lone enum value. From the case above we know + // we can ignore enum_values. + SchemaObject { + metadata, + const_value: Some(const_value), + .. + } => { + let new_schema = SchemaObject { + const_value: None, + enum_values: Some(vec![const_value.clone()]), + ..schema.clone() + }; + let (type_entry, _) = + self.convert_schema_object(type_name, original_schema, &new_schema)?; + Ok((type_entry, metadata)) + } + // If we have a schema that has an instance type array that's // exactly two elements and one of them is Null, we have the // equivalent of an Option where T is the type defined by the @@ -406,7 +436,7 @@ impl TypeSpace { instance_type: _, format: _, enum_values: _, - const_value: _, + const_value: None, subschemas: _, number: _, string: _, @@ -584,28 +614,6 @@ impl TypeSpace { } } - // TODO let's not bother with const values at the moment. In the - // future we could create types that have a single value with a - // newtype wrapper, but it's too much of a mess for too little - // value at the moment. Instead, we act as though this const_value - // field were None. - SchemaObject { - metadata, - const_value: Some(_), - .. - } => { - let new_schema = SchemaObject { - const_value: None, - ..schema.clone() - }; - self.convert_schema_object(type_name, original_schema, &new_schema) - .map(|(te, m)| match m { - Some(_) if m == metadata => (te, metadata), - Some(_) => panic!("unexpected metadata value"), - None => (te, &None), - }) - } - // In actual, not-made-up, in-the-wild specs, I've seen the type // field enumerate all possibilities... perhaps to emphasize their // seriousness of the schema representing **anything**. In that diff --git a/typify-impl/tests/github.out b/typify-impl/tests/github.out index 53d82841..7af039c6 100644 --- a/typify-impl/tests/github.out +++ b/typify-impl/tests/github.out @@ -10680,29 +10680,911 @@ impl ::std::convert::TryFrom<::std::string::String> for GithubAppAuthorizationRe #[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug)] #[serde(deny_unknown_fields)] pub struct GithubOrg { - pub avatar_url: ::std::string::String, + pub avatar_url: GithubOrgAvatarUrl, #[serde(default)] pub email: (), - pub events_url: ::std::string::String, - pub followers_url: ::std::string::String, - pub following_url: ::std::string::String, - pub gists_url: ::std::string::String, - pub gravatar_id: ::std::string::String, - pub html_url: ::std::string::String, - pub id: i64, - pub login: ::std::string::String, - #[serde(skip_serializing_if = "::std::option::Option::is_none")] - pub name: ::std::option::Option<::std::string::String>, - pub node_id: ::std::string::String, - pub organizations_url: ::std::string::String, - pub received_events_url: ::std::string::String, - pub repos_url: ::std::string::String, + pub events_url: GithubOrgEventsUrl, + pub followers_url: GithubOrgFollowersUrl, + pub following_url: GithubOrgFollowingUrl, + pub gists_url: GithubOrgGistsUrl, + pub gravatar_id: GithubOrgGravatarId, + pub html_url: GithubOrgHtmlUrl, + pub id: GithubOrgId, + pub login: GithubOrgLogin, + #[serde(skip_serializing_if = "::std::option::Option::is_none")] + pub name: ::std::option::Option, + pub node_id: GithubOrgNodeId, + pub organizations_url: GithubOrgOrganizationsUrl, + pub received_events_url: GithubOrgReceivedEventsUrl, + pub repos_url: GithubOrgReposUrl, pub site_admin: bool, - pub starred_url: ::std::string::String, - pub subscriptions_url: ::std::string::String, + pub starred_url: GithubOrgStarredUrl, + pub subscriptions_url: GithubOrgSubscriptionsUrl, #[serde(rename = "type")] - pub type_: ::std::string::String, - pub url: ::std::string::String, + pub type_: GithubOrgType, + pub url: GithubOrgUrl, +} +#[doc = "`GithubOrgAvatarUrl`"] +#[derive( + :: serde :: Deserialize, + :: serde :: Serialize, + Clone, + Copy, + Debug, + Eq, + Hash, + Ord, + PartialEq, + PartialOrd, +)] +pub enum GithubOrgAvatarUrl { + #[serde(rename = "https://avatars.githubusercontent.com/u/9919?v=4")] + HttpsAvatarsGithubusercontentComU9919V4, +} +impl ::std::fmt::Display for GithubOrgAvatarUrl { + fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { + match *self { + Self::HttpsAvatarsGithubusercontentComU9919V4 => { + f.write_str("https://avatars.githubusercontent.com/u/9919?v=4") + } + } + } +} +impl ::std::str::FromStr for GithubOrgAvatarUrl { + type Err = self::error::ConversionError; + fn from_str(value: &str) -> ::std::result::Result { + match value { + "https://avatars.githubusercontent.com/u/9919?v=4" => { + Ok(Self::HttpsAvatarsGithubusercontentComU9919V4) + } + _ => Err("invalid value".into()), + } + } +} +impl ::std::convert::TryFrom<&str> for GithubOrgAvatarUrl { + type Error = self::error::ConversionError; + fn try_from(value: &str) -> ::std::result::Result { + value.parse() + } +} +impl ::std::convert::TryFrom<::std::string::String> for GithubOrgAvatarUrl { + type Error = self::error::ConversionError; + fn try_from( + value: ::std::string::String, + ) -> ::std::result::Result { + value.parse() + } +} +#[doc = "`GithubOrgEventsUrl`"] +#[derive( + :: serde :: Deserialize, + :: serde :: Serialize, + Clone, + Copy, + Debug, + Eq, + Hash, + Ord, + PartialEq, + PartialOrd, +)] +pub enum GithubOrgEventsUrl { + #[serde(rename = "https://api.github.com/users/github/events{/privacy}")] + HttpsApiGithubComUsersGithubEventsPrivacy, +} +impl ::std::fmt::Display for GithubOrgEventsUrl { + fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { + match *self { + Self::HttpsApiGithubComUsersGithubEventsPrivacy => { + f.write_str("https://api.github.com/users/github/events{/privacy}") + } + } + } +} +impl ::std::str::FromStr for GithubOrgEventsUrl { + type Err = self::error::ConversionError; + fn from_str(value: &str) -> ::std::result::Result { + match value { + "https://api.github.com/users/github/events{/privacy}" => { + Ok(Self::HttpsApiGithubComUsersGithubEventsPrivacy) + } + _ => Err("invalid value".into()), + } + } +} +impl ::std::convert::TryFrom<&str> for GithubOrgEventsUrl { + type Error = self::error::ConversionError; + fn try_from(value: &str) -> ::std::result::Result { + value.parse() + } +} +impl ::std::convert::TryFrom<::std::string::String> for GithubOrgEventsUrl { + type Error = self::error::ConversionError; + fn try_from( + value: ::std::string::String, + ) -> ::std::result::Result { + value.parse() + } +} +#[doc = "`GithubOrgFollowersUrl`"] +#[derive( + :: serde :: Deserialize, + :: serde :: Serialize, + Clone, + Copy, + Debug, + Eq, + Hash, + Ord, + PartialEq, + PartialOrd, +)] +pub enum GithubOrgFollowersUrl { + #[serde(rename = "https://api.github.com/users/github/followers")] + HttpsApiGithubComUsersGithubFollowers, +} +impl ::std::fmt::Display for GithubOrgFollowersUrl { + fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { + match *self { + Self::HttpsApiGithubComUsersGithubFollowers => { + f.write_str("https://api.github.com/users/github/followers") + } + } + } +} +impl ::std::str::FromStr for GithubOrgFollowersUrl { + type Err = self::error::ConversionError; + fn from_str(value: &str) -> ::std::result::Result { + match value { + "https://api.github.com/users/github/followers" => { + Ok(Self::HttpsApiGithubComUsersGithubFollowers) + } + _ => Err("invalid value".into()), + } + } +} +impl ::std::convert::TryFrom<&str> for GithubOrgFollowersUrl { + type Error = self::error::ConversionError; + fn try_from(value: &str) -> ::std::result::Result { + value.parse() + } +} +impl ::std::convert::TryFrom<::std::string::String> for GithubOrgFollowersUrl { + type Error = self::error::ConversionError; + fn try_from( + value: ::std::string::String, + ) -> ::std::result::Result { + value.parse() + } +} +#[doc = "`GithubOrgFollowingUrl`"] +#[derive( + :: serde :: Deserialize, + :: serde :: Serialize, + Clone, + Copy, + Debug, + Eq, + Hash, + Ord, + PartialEq, + PartialOrd, +)] +pub enum GithubOrgFollowingUrl { + #[serde(rename = "https://api.github.com/users/github/following{/other_user}")] + HttpsApiGithubComUsersGithubFollowingOtherUser, +} +impl ::std::fmt::Display for GithubOrgFollowingUrl { + fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { + match *self { + Self::HttpsApiGithubComUsersGithubFollowingOtherUser => { + f.write_str("https://api.github.com/users/github/following{/other_user}") + } + } + } +} +impl ::std::str::FromStr for GithubOrgFollowingUrl { + type Err = self::error::ConversionError; + fn from_str(value: &str) -> ::std::result::Result { + match value { + "https://api.github.com/users/github/following{/other_user}" => { + Ok(Self::HttpsApiGithubComUsersGithubFollowingOtherUser) + } + _ => Err("invalid value".into()), + } + } +} +impl ::std::convert::TryFrom<&str> for GithubOrgFollowingUrl { + type Error = self::error::ConversionError; + fn try_from(value: &str) -> ::std::result::Result { + value.parse() + } +} +impl ::std::convert::TryFrom<::std::string::String> for GithubOrgFollowingUrl { + type Error = self::error::ConversionError; + fn try_from( + value: ::std::string::String, + ) -> ::std::result::Result { + value.parse() + } +} +#[doc = "`GithubOrgGistsUrl`"] +#[derive( + :: serde :: Deserialize, + :: serde :: Serialize, + Clone, + Copy, + Debug, + Eq, + Hash, + Ord, + PartialEq, + PartialOrd, +)] +pub enum GithubOrgGistsUrl { + #[serde(rename = "https://api.github.com/users/github/gists{/gist_id}")] + HttpsApiGithubComUsersGithubGistsGistId, +} +impl ::std::fmt::Display for GithubOrgGistsUrl { + fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { + match *self { + Self::HttpsApiGithubComUsersGithubGistsGistId => { + f.write_str("https://api.github.com/users/github/gists{/gist_id}") + } + } + } +} +impl ::std::str::FromStr for GithubOrgGistsUrl { + type Err = self::error::ConversionError; + fn from_str(value: &str) -> ::std::result::Result { + match value { + "https://api.github.com/users/github/gists{/gist_id}" => { + Ok(Self::HttpsApiGithubComUsersGithubGistsGistId) + } + _ => Err("invalid value".into()), + } + } +} +impl ::std::convert::TryFrom<&str> for GithubOrgGistsUrl { + type Error = self::error::ConversionError; + fn try_from(value: &str) -> ::std::result::Result { + value.parse() + } +} +impl ::std::convert::TryFrom<::std::string::String> for GithubOrgGistsUrl { + type Error = self::error::ConversionError; + fn try_from( + value: ::std::string::String, + ) -> ::std::result::Result { + value.parse() + } +} +#[doc = "`GithubOrgGravatarId`"] +#[derive( + :: serde :: Deserialize, + :: serde :: Serialize, + Clone, + Copy, + Debug, + Eq, + Hash, + Ord, + PartialEq, + PartialOrd, +)] +pub enum GithubOrgGravatarId { + #[serde(rename = "")] + X, +} +impl ::std::fmt::Display for GithubOrgGravatarId { + fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { + match *self { + Self::X => f.write_str(""), + } + } +} +impl ::std::str::FromStr for GithubOrgGravatarId { + type Err = self::error::ConversionError; + fn from_str(value: &str) -> ::std::result::Result { + match value { + "" => Ok(Self::X), + _ => Err("invalid value".into()), + } + } +} +impl ::std::convert::TryFrom<&str> for GithubOrgGravatarId { + type Error = self::error::ConversionError; + fn try_from(value: &str) -> ::std::result::Result { + value.parse() + } +} +impl ::std::convert::TryFrom<::std::string::String> for GithubOrgGravatarId { + type Error = self::error::ConversionError; + fn try_from( + value: ::std::string::String, + ) -> ::std::result::Result { + value.parse() + } +} +#[doc = "`GithubOrgHtmlUrl`"] +#[derive( + :: serde :: Deserialize, + :: serde :: Serialize, + Clone, + Copy, + Debug, + Eq, + Hash, + Ord, + PartialEq, + PartialOrd, +)] +pub enum GithubOrgHtmlUrl { + #[serde(rename = "https://github.com/github")] + HttpsGithubComGithub, +} +impl ::std::fmt::Display for GithubOrgHtmlUrl { + fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { + match *self { + Self::HttpsGithubComGithub => f.write_str("https://github.com/github"), + } + } +} +impl ::std::str::FromStr for GithubOrgHtmlUrl { + type Err = self::error::ConversionError; + fn from_str(value: &str) -> ::std::result::Result { + match value { + "https://github.com/github" => Ok(Self::HttpsGithubComGithub), + _ => Err("invalid value".into()), + } + } +} +impl ::std::convert::TryFrom<&str> for GithubOrgHtmlUrl { + type Error = self::error::ConversionError; + fn try_from(value: &str) -> ::std::result::Result { + value.parse() + } +} +impl ::std::convert::TryFrom<::std::string::String> for GithubOrgHtmlUrl { + type Error = self::error::ConversionError; + fn try_from( + value: ::std::string::String, + ) -> ::std::result::Result { + value.parse() + } +} +#[doc = "`GithubOrgId`"] +#[derive(:: serde :: Serialize, Clone, Debug)] +#[serde(transparent)] +pub struct GithubOrgId(i64); +impl ::std::ops::Deref for GithubOrgId { + type Target = i64; + fn deref(&self) -> &i64 { + &self.0 + } +} +impl ::std::convert::From for i64 { + fn from(value: GithubOrgId) -> Self { + value.0 + } +} +impl ::std::str::FromStr for GithubOrgId { + type Err = self::error::ConversionError; + fn from_str(value: &str) -> ::std::result::Result { + value + .parse::() + .map_err(|e| ::std::string::ToString::to_string(&e).into()) + .and_then(Self::try_from) + } +} +impl ::std::convert::TryFrom for GithubOrgId { + type Error = self::error::ConversionError; + fn try_from(value: i64) -> ::std::result::Result { + if ![9919_i64].contains(&value) { + Err("invalid value".into()) + } else { + Ok(Self(value)) + } + } +} +impl<'de> ::serde::Deserialize<'de> for GithubOrgId { + fn deserialize(deserializer: D) -> ::std::result::Result + where + D: ::serde::Deserializer<'de>, + { + Self::try_from(::deserialize(deserializer)?) + .map_err(|e| ::custom(e.to_string())) + } +} +#[doc = "`GithubOrgLogin`"] +#[derive( + :: serde :: Deserialize, + :: serde :: Serialize, + Clone, + Copy, + Debug, + Eq, + Hash, + Ord, + PartialEq, + PartialOrd, +)] +pub enum GithubOrgLogin { + #[serde(rename = "github")] + Github, +} +impl ::std::fmt::Display for GithubOrgLogin { + fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { + match *self { + Self::Github => f.write_str("github"), + } + } +} +impl ::std::str::FromStr for GithubOrgLogin { + type Err = self::error::ConversionError; + fn from_str(value: &str) -> ::std::result::Result { + match value { + "github" => Ok(Self::Github), + _ => Err("invalid value".into()), + } + } +} +impl ::std::convert::TryFrom<&str> for GithubOrgLogin { + type Error = self::error::ConversionError; + fn try_from(value: &str) -> ::std::result::Result { + value.parse() + } +} +impl ::std::convert::TryFrom<::std::string::String> for GithubOrgLogin { + type Error = self::error::ConversionError; + fn try_from( + value: ::std::string::String, + ) -> ::std::result::Result { + value.parse() + } +} +#[doc = "`GithubOrgName`"] +#[derive( + :: serde :: Deserialize, + :: serde :: Serialize, + Clone, + Copy, + Debug, + Eq, + Hash, + Ord, + PartialEq, + PartialOrd, +)] +pub enum GithubOrgName { + GitHub, +} +impl ::std::fmt::Display for GithubOrgName { + fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { + match *self { + Self::GitHub => f.write_str("GitHub"), + } + } +} +impl ::std::str::FromStr for GithubOrgName { + type Err = self::error::ConversionError; + fn from_str(value: &str) -> ::std::result::Result { + match value { + "GitHub" => Ok(Self::GitHub), + _ => Err("invalid value".into()), + } + } +} +impl ::std::convert::TryFrom<&str> for GithubOrgName { + type Error = self::error::ConversionError; + fn try_from(value: &str) -> ::std::result::Result { + value.parse() + } +} +impl ::std::convert::TryFrom<::std::string::String> for GithubOrgName { + type Error = self::error::ConversionError; + fn try_from( + value: ::std::string::String, + ) -> ::std::result::Result { + value.parse() + } +} +#[doc = "`GithubOrgNodeId`"] +#[derive( + :: serde :: Deserialize, + :: serde :: Serialize, + Clone, + Copy, + Debug, + Eq, + Hash, + Ord, + PartialEq, + PartialOrd, +)] +pub enum GithubOrgNodeId { + #[serde(rename = "MDEyOk9yZ2FuaXphdGlvbjk5MTk=")] + MdEyOk9yZ2FuaXphdGlvbjk5MTk, +} +impl ::std::fmt::Display for GithubOrgNodeId { + fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { + match *self { + Self::MdEyOk9yZ2FuaXphdGlvbjk5MTk => f.write_str("MDEyOk9yZ2FuaXphdGlvbjk5MTk="), + } + } +} +impl ::std::str::FromStr for GithubOrgNodeId { + type Err = self::error::ConversionError; + fn from_str(value: &str) -> ::std::result::Result { + match value { + "MDEyOk9yZ2FuaXphdGlvbjk5MTk=" => Ok(Self::MdEyOk9yZ2FuaXphdGlvbjk5MTk), + _ => Err("invalid value".into()), + } + } +} +impl ::std::convert::TryFrom<&str> for GithubOrgNodeId { + type Error = self::error::ConversionError; + fn try_from(value: &str) -> ::std::result::Result { + value.parse() + } +} +impl ::std::convert::TryFrom<::std::string::String> for GithubOrgNodeId { + type Error = self::error::ConversionError; + fn try_from( + value: ::std::string::String, + ) -> ::std::result::Result { + value.parse() + } +} +#[doc = "`GithubOrgOrganizationsUrl`"] +#[derive( + :: serde :: Deserialize, + :: serde :: Serialize, + Clone, + Copy, + Debug, + Eq, + Hash, + Ord, + PartialEq, + PartialOrd, +)] +pub enum GithubOrgOrganizationsUrl { + #[serde(rename = "https://api.github.com/users/github/orgs")] + HttpsApiGithubComUsersGithubOrgs, +} +impl ::std::fmt::Display for GithubOrgOrganizationsUrl { + fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { + match *self { + Self::HttpsApiGithubComUsersGithubOrgs => { + f.write_str("https://api.github.com/users/github/orgs") + } + } + } +} +impl ::std::str::FromStr for GithubOrgOrganizationsUrl { + type Err = self::error::ConversionError; + fn from_str(value: &str) -> ::std::result::Result { + match value { + "https://api.github.com/users/github/orgs" => { + Ok(Self::HttpsApiGithubComUsersGithubOrgs) + } + _ => Err("invalid value".into()), + } + } +} +impl ::std::convert::TryFrom<&str> for GithubOrgOrganizationsUrl { + type Error = self::error::ConversionError; + fn try_from(value: &str) -> ::std::result::Result { + value.parse() + } +} +impl ::std::convert::TryFrom<::std::string::String> for GithubOrgOrganizationsUrl { + type Error = self::error::ConversionError; + fn try_from( + value: ::std::string::String, + ) -> ::std::result::Result { + value.parse() + } +} +#[doc = "`GithubOrgReceivedEventsUrl`"] +#[derive( + :: serde :: Deserialize, + :: serde :: Serialize, + Clone, + Copy, + Debug, + Eq, + Hash, + Ord, + PartialEq, + PartialOrd, +)] +pub enum GithubOrgReceivedEventsUrl { + #[serde(rename = "https://api.github.com/users/github/received_events")] + HttpsApiGithubComUsersGithubReceivedEvents, +} +impl ::std::fmt::Display for GithubOrgReceivedEventsUrl { + fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { + match *self { + Self::HttpsApiGithubComUsersGithubReceivedEvents => { + f.write_str("https://api.github.com/users/github/received_events") + } + } + } +} +impl ::std::str::FromStr for GithubOrgReceivedEventsUrl { + type Err = self::error::ConversionError; + fn from_str(value: &str) -> ::std::result::Result { + match value { + "https://api.github.com/users/github/received_events" => { + Ok(Self::HttpsApiGithubComUsersGithubReceivedEvents) + } + _ => Err("invalid value".into()), + } + } +} +impl ::std::convert::TryFrom<&str> for GithubOrgReceivedEventsUrl { + type Error = self::error::ConversionError; + fn try_from(value: &str) -> ::std::result::Result { + value.parse() + } +} +impl ::std::convert::TryFrom<::std::string::String> for GithubOrgReceivedEventsUrl { + type Error = self::error::ConversionError; + fn try_from( + value: ::std::string::String, + ) -> ::std::result::Result { + value.parse() + } +} +#[doc = "`GithubOrgReposUrl`"] +#[derive( + :: serde :: Deserialize, + :: serde :: Serialize, + Clone, + Copy, + Debug, + Eq, + Hash, + Ord, + PartialEq, + PartialOrd, +)] +pub enum GithubOrgReposUrl { + #[serde(rename = "https://api.github.com/users/github/repos")] + HttpsApiGithubComUsersGithubRepos, +} +impl ::std::fmt::Display for GithubOrgReposUrl { + fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { + match *self { + Self::HttpsApiGithubComUsersGithubRepos => { + f.write_str("https://api.github.com/users/github/repos") + } + } + } +} +impl ::std::str::FromStr for GithubOrgReposUrl { + type Err = self::error::ConversionError; + fn from_str(value: &str) -> ::std::result::Result { + match value { + "https://api.github.com/users/github/repos" => { + Ok(Self::HttpsApiGithubComUsersGithubRepos) + } + _ => Err("invalid value".into()), + } + } +} +impl ::std::convert::TryFrom<&str> for GithubOrgReposUrl { + type Error = self::error::ConversionError; + fn try_from(value: &str) -> ::std::result::Result { + value.parse() + } +} +impl ::std::convert::TryFrom<::std::string::String> for GithubOrgReposUrl { + type Error = self::error::ConversionError; + fn try_from( + value: ::std::string::String, + ) -> ::std::result::Result { + value.parse() + } +} +#[doc = "`GithubOrgStarredUrl`"] +#[derive( + :: serde :: Deserialize, + :: serde :: Serialize, + Clone, + Copy, + Debug, + Eq, + Hash, + Ord, + PartialEq, + PartialOrd, +)] +pub enum GithubOrgStarredUrl { + #[serde(rename = "https://api.github.com/users/github/starred{/owner}{/repo}")] + HttpsApiGithubComUsersGithubStarredOwnerRepo, +} +impl ::std::fmt::Display for GithubOrgStarredUrl { + fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { + match *self { + Self::HttpsApiGithubComUsersGithubStarredOwnerRepo => { + f.write_str("https://api.github.com/users/github/starred{/owner}{/repo}") + } + } + } +} +impl ::std::str::FromStr for GithubOrgStarredUrl { + type Err = self::error::ConversionError; + fn from_str(value: &str) -> ::std::result::Result { + match value { + "https://api.github.com/users/github/starred{/owner}{/repo}" => { + Ok(Self::HttpsApiGithubComUsersGithubStarredOwnerRepo) + } + _ => Err("invalid value".into()), + } + } +} +impl ::std::convert::TryFrom<&str> for GithubOrgStarredUrl { + type Error = self::error::ConversionError; + fn try_from(value: &str) -> ::std::result::Result { + value.parse() + } +} +impl ::std::convert::TryFrom<::std::string::String> for GithubOrgStarredUrl { + type Error = self::error::ConversionError; + fn try_from( + value: ::std::string::String, + ) -> ::std::result::Result { + value.parse() + } +} +#[doc = "`GithubOrgSubscriptionsUrl`"] +#[derive( + :: serde :: Deserialize, + :: serde :: Serialize, + Clone, + Copy, + Debug, + Eq, + Hash, + Ord, + PartialEq, + PartialOrd, +)] +pub enum GithubOrgSubscriptionsUrl { + #[serde(rename = "https://api.github.com/users/github/subscriptions")] + HttpsApiGithubComUsersGithubSubscriptions, +} +impl ::std::fmt::Display for GithubOrgSubscriptionsUrl { + fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { + match *self { + Self::HttpsApiGithubComUsersGithubSubscriptions => { + f.write_str("https://api.github.com/users/github/subscriptions") + } + } + } +} +impl ::std::str::FromStr for GithubOrgSubscriptionsUrl { + type Err = self::error::ConversionError; + fn from_str(value: &str) -> ::std::result::Result { + match value { + "https://api.github.com/users/github/subscriptions" => { + Ok(Self::HttpsApiGithubComUsersGithubSubscriptions) + } + _ => Err("invalid value".into()), + } + } +} +impl ::std::convert::TryFrom<&str> for GithubOrgSubscriptionsUrl { + type Error = self::error::ConversionError; + fn try_from(value: &str) -> ::std::result::Result { + value.parse() + } +} +impl ::std::convert::TryFrom<::std::string::String> for GithubOrgSubscriptionsUrl { + type Error = self::error::ConversionError; + fn try_from( + value: ::std::string::String, + ) -> ::std::result::Result { + value.parse() + } +} +#[doc = "`GithubOrgType`"] +#[derive( + :: serde :: Deserialize, + :: serde :: Serialize, + Clone, + Copy, + Debug, + Eq, + Hash, + Ord, + PartialEq, + PartialOrd, +)] +pub enum GithubOrgType { + Organization, +} +impl ::std::fmt::Display for GithubOrgType { + fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { + match *self { + Self::Organization => f.write_str("Organization"), + } + } +} +impl ::std::str::FromStr for GithubOrgType { + type Err = self::error::ConversionError; + fn from_str(value: &str) -> ::std::result::Result { + match value { + "Organization" => Ok(Self::Organization), + _ => Err("invalid value".into()), + } + } +} +impl ::std::convert::TryFrom<&str> for GithubOrgType { + type Error = self::error::ConversionError; + fn try_from(value: &str) -> ::std::result::Result { + value.parse() + } +} +impl ::std::convert::TryFrom<::std::string::String> for GithubOrgType { + type Error = self::error::ConversionError; + fn try_from( + value: ::std::string::String, + ) -> ::std::result::Result { + value.parse() + } +} +#[doc = "`GithubOrgUrl`"] +#[derive( + :: serde :: Deserialize, + :: serde :: Serialize, + Clone, + Copy, + Debug, + Eq, + Hash, + Ord, + PartialEq, + PartialOrd, +)] +pub enum GithubOrgUrl { + #[serde(rename = "https://api.github.com/users/github")] + HttpsApiGithubComUsersGithub, +} +impl ::std::fmt::Display for GithubOrgUrl { + fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { + match *self { + Self::HttpsApiGithubComUsersGithub => { + f.write_str("https://api.github.com/users/github") + } + } + } +} +impl ::std::str::FromStr for GithubOrgUrl { + type Err = self::error::ConversionError; + fn from_str(value: &str) -> ::std::result::Result { + match value { + "https://api.github.com/users/github" => Ok(Self::HttpsApiGithubComUsersGithub), + _ => Err("invalid value".into()), + } + } +} +impl ::std::convert::TryFrom<&str> for GithubOrgUrl { + type Error = self::error::ConversionError; + fn try_from(value: &str) -> ::std::result::Result { + value.parse() + } +} +impl ::std::convert::TryFrom<::std::string::String> for GithubOrgUrl { + type Error = self::error::ConversionError; + fn try_from( + value: ::std::string::String, + ) -> ::std::result::Result { + value.parse() + } } #[doc = "A wiki page is created or updated."] #[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug)] @@ -36520,15 +37402,15 @@ impl ::std::convert::TryFrom<::std::string::String> for WatchStartedAction { #[serde(untagged)] pub enum WebhookEvents { Variant0(::std::vec::Vec), - Variant1([::std::string::String; 1usize]), + Variant1([WebhookEventsVariant1Item; 1usize]), } impl ::std::convert::From<::std::vec::Vec> for WebhookEvents { fn from(value: ::std::vec::Vec) -> Self { Self::Variant0(value) } } -impl ::std::convert::From<[::std::string::String; 1usize]> for WebhookEvents { - fn from(value: [::std::string::String; 1usize]) -> Self { +impl ::std::convert::From<[WebhookEventsVariant1Item; 1usize]> for WebhookEvents { + fn from(value: [WebhookEventsVariant1Item; 1usize]) -> Self { Self::Variant1(value) } } @@ -36763,6 +37645,53 @@ impl ::std::convert::TryFrom<::std::string::String> for WebhookEventsVariant0Ite value.parse() } } +#[doc = "`WebhookEventsVariant1Item`"] +#[derive( + :: serde :: Deserialize, + :: serde :: Serialize, + Clone, + Copy, + Debug, + Eq, + Hash, + Ord, + PartialEq, + PartialOrd, +)] +pub enum WebhookEventsVariant1Item { + #[serde(rename = "*")] + X, +} +impl ::std::fmt::Display for WebhookEventsVariant1Item { + fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { + match *self { + Self::X => f.write_str("*"), + } + } +} +impl ::std::str::FromStr for WebhookEventsVariant1Item { + type Err = self::error::ConversionError; + fn from_str(value: &str) -> ::std::result::Result { + match value { + "*" => Ok(Self::X), + _ => Err("invalid value".into()), + } + } +} +impl ::std::convert::TryFrom<&str> for WebhookEventsVariant1Item { + type Error = self::error::ConversionError; + fn try_from(value: &str) -> ::std::result::Result { + value.parse() + } +} +impl ::std::convert::TryFrom<::std::string::String> for WebhookEventsVariant1Item { + type Error = self::error::ConversionError; + fn try_from( + value: ::std::string::String, + ) -> ::std::result::Result { + value.parse() + } +} #[doc = "`Workflow`"] #[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug)] #[serde(deny_unknown_fields)] diff --git a/typify/tests/schemas/various-enums-json-schema.rs b/typify/tests/schemas/various-enums-json-schema.rs index 75d19e67..5307883e 100644 --- a/typify/tests/schemas/various-enums-json-schema.rs +++ b/typify/tests/schemas/various-enums-json-schema.rs @@ -206,6 +206,185 @@ impl ::std::convert::TryFrom<::std::string::String> for CommentedVariants { value.parse() } } +#[doc = "`ConstInteger`"] +#[derive(:: serde :: Serialize, Clone, Debug)] +#[serde(transparent)] +pub struct ConstInteger(i64); +impl ::std::ops::Deref for ConstInteger { + type Target = i64; + fn deref(&self) -> &i64 { + &self.0 + } +} +impl ::std::convert::From for i64 { + fn from(value: ConstInteger) -> Self { + value.0 + } +} +impl ::std::str::FromStr for ConstInteger { + type Err = self::error::ConversionError; + fn from_str(value: &str) -> ::std::result::Result { + value + .parse::() + .map_err(|e| ::std::string::ToString::to_string(&e).into()) + .and_then(Self::try_from) + } +} +impl ::std::convert::TryFrom for ConstInteger { + type Error = self::error::ConversionError; + fn try_from(value: i64) -> ::std::result::Result { + if ![42_i64].contains(&value) { + Err("invalid value".into()) + } else { + Ok(Self(value)) + } + } +} +impl<'de> ::serde::Deserialize<'de> for ConstInteger { + fn deserialize(deserializer: D) -> ::std::result::Result + where + D: ::serde::Deserializer<'de>, + { + Self::try_from(::deserialize(deserializer)?) + .map_err(|e| ::custom(e.to_string())) + } +} +impl ::schemars::JsonSchema for ConstInteger { + fn schema_name() -> ::std::string::String { + "ConstInteger".to_string() + } + fn json_schema(g: &mut ::schemars::r#gen::SchemaGenerator) -> ::schemars::schema::Schema { + let mut schema = ::json_schema(g).into_object(); + schema.enum_values = ::std::option::Option::Some( + [::serde_json::Value::Number(::serde_json::Number::from( + 42i64, + ))] + .into_iter() + .collect(), + ); + schema.into() + } +} +#[doc = "`ConstProperty`"] +#[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug, schemars :: JsonSchema)] +pub struct ConstProperty { + #[doc = "always a widget"] + #[serde(default = "defaults::const_property_kind")] + pub kind: ConstPropertyKind, +} +impl ::std::default::Default for ConstProperty { + fn default() -> Self { + Self { + kind: defaults::const_property_kind(), + } + } +} +impl ConstProperty { + pub fn builder() -> builder::ConstProperty { + Default::default() + } +} +#[doc = "always a widget"] +#[derive( + :: serde :: Deserialize, + :: serde :: Serialize, + Clone, + Copy, + Debug, + Eq, + Hash, + Ord, + PartialEq, + PartialOrd, + schemars :: JsonSchema, +)] +pub enum ConstPropertyKind { + #[serde(rename = "widget")] + Widget, +} +impl ::std::fmt::Display for ConstPropertyKind { + fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { + match *self { + Self::Widget => f.write_str("widget"), + } + } +} +impl ::std::str::FromStr for ConstPropertyKind { + type Err = self::error::ConversionError; + fn from_str(value: &str) -> ::std::result::Result { + match value { + "widget" => Ok(Self::Widget), + _ => Err("invalid value".into()), + } + } +} +impl ::std::convert::TryFrom<&str> for ConstPropertyKind { + type Error = self::error::ConversionError; + fn try_from(value: &str) -> ::std::result::Result { + value.parse() + } +} +impl ::std::convert::TryFrom<::std::string::String> for ConstPropertyKind { + type Error = self::error::ConversionError; + fn try_from( + value: ::std::string::String, + ) -> ::std::result::Result { + value.parse() + } +} +impl ::std::default::Default for ConstPropertyKind { + fn default() -> Self { + ConstPropertyKind::Widget + } +} +#[doc = "`ConstString`"] +#[derive( + :: serde :: Deserialize, + :: serde :: Serialize, + Clone, + Copy, + Debug, + Eq, + Hash, + Ord, + PartialEq, + PartialOrd, + schemars :: JsonSchema, +)] +pub enum ConstString { + #[serde(rename = "fixed")] + Fixed, +} +impl ::std::fmt::Display for ConstString { + fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { + match *self { + Self::Fixed => f.write_str("fixed"), + } + } +} +impl ::std::str::FromStr for ConstString { + type Err = self::error::ConversionError; + fn from_str(value: &str) -> ::std::result::Result { + match value { + "fixed" => Ok(Self::Fixed), + _ => Err("invalid value".into()), + } + } +} +impl ::std::convert::TryFrom<&str> for ConstString { + type Error = self::error::ConversionError; + fn try_from(value: &str) -> ::std::result::Result { + value.parse() + } +} +impl ::std::convert::TryFrom<::std::string::String> for ConstString { + type Error = self::error::ConversionError; + fn try_from( + value: ::std::string::String, + ) -> ::std::result::Result { + value.parse() + } +} #[doc = "`DiskAttachment`"] #[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug, schemars :: JsonSchema)] pub struct DiskAttachment { @@ -1119,6 +1298,44 @@ impl ::std::convert::TryFrom<::std::string::String> for VariantsDifferByPunct { } #[doc = " Types for composing complex structures."] pub mod builder { + #[derive(Clone, Debug)] + pub struct ConstProperty { + kind: ::std::result::Result, + } + impl ::std::default::Default for ConstProperty { + fn default() -> Self { + Self { + kind: Ok(super::defaults::const_property_kind()), + } + } + } + impl ConstProperty { + pub fn kind(mut self, value: T) -> Self + where + T: ::std::convert::TryInto, + T::Error: ::std::fmt::Display, + { + self.kind = value + .try_into() + .map_err(|e| format!("error converting supplied value for kind: {e}")); + self + } + } + impl ::std::convert::TryFrom for super::ConstProperty { + type Error = super::error::ConversionError; + fn try_from( + value: ConstProperty, + ) -> ::std::result::Result { + Ok(Self { kind: value.kind? }) + } + } + impl ::std::convert::From for ConstProperty { + fn from(value: super::ConstProperty) -> Self { + Self { + kind: Ok(value.kind), + } + } + } #[derive(Clone, Debug)] pub struct DiskAttachment { alternate: ::std::result::Result, @@ -1215,6 +1432,12 @@ pub mod builder { } } } +#[doc = " Generation of default values for serde."] +pub mod defaults { + pub(super) fn const_property_kind() -> super::ConstPropertyKind { + super::ConstPropertyKind::Widget + } +} #[doc = " Error types."] pub mod error { #[doc = r" Error from a `TryFrom` or `FromStr` implementation."] diff --git a/typify/tests/schemas/various-enums.json b/typify/tests/schemas/various-enums.json index 4f460f8a..4379a4d6 100644 --- a/typify/tests/schemas/various-enums.json +++ b/typify/tests/schemas/various-enums.json @@ -420,6 +420,24 @@ 4096 ] } + }, + "ConstString": { + "type": "string", + "const": "fixed" + }, + "ConstInteger": { + "type": "integer", + "const": 42 + }, + "ConstProperty": { + "type": "object", + "properties": { + "kind": { + "description": "always a widget", + "const": "widget", + "default": "widget" + } + } } } } diff --git a/typify/tests/schemas/various-enums.rs b/typify/tests/schemas/various-enums.rs index 229f49c2..16cc30e2 100644 --- a/typify/tests/schemas/various-enums.rs +++ b/typify/tests/schemas/various-enums.rs @@ -184,6 +184,167 @@ impl ::std::convert::TryFrom<::std::string::String> for CommentedVariants { value.parse() } } +#[doc = "`ConstInteger`"] +#[derive(:: serde :: Serialize, Clone, Debug)] +#[serde(transparent)] +pub struct ConstInteger(i64); +impl ::std::ops::Deref for ConstInteger { + type Target = i64; + fn deref(&self) -> &i64 { + &self.0 + } +} +impl ::std::convert::From for i64 { + fn from(value: ConstInteger) -> Self { + value.0 + } +} +impl ::std::str::FromStr for ConstInteger { + type Err = self::error::ConversionError; + fn from_str(value: &str) -> ::std::result::Result { + value + .parse::() + .map_err(|e| ::std::string::ToString::to_string(&e).into()) + .and_then(Self::try_from) + } +} +impl ::std::convert::TryFrom for ConstInteger { + type Error = self::error::ConversionError; + fn try_from(value: i64) -> ::std::result::Result { + if ![42_i64].contains(&value) { + Err("invalid value".into()) + } else { + Ok(Self(value)) + } + } +} +impl<'de> ::serde::Deserialize<'de> for ConstInteger { + fn deserialize(deserializer: D) -> ::std::result::Result + where + D: ::serde::Deserializer<'de>, + { + Self::try_from(::deserialize(deserializer)?) + .map_err(|e| ::custom(e.to_string())) + } +} +#[doc = "`ConstProperty`"] +#[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug)] +pub struct ConstProperty { + #[doc = "always a widget"] + #[serde(default = "defaults::const_property_kind")] + pub kind: ConstPropertyKind, +} +impl ::std::default::Default for ConstProperty { + fn default() -> Self { + Self { + kind: defaults::const_property_kind(), + } + } +} +impl ConstProperty { + pub fn builder() -> builder::ConstProperty { + Default::default() + } +} +#[doc = "always a widget"] +#[derive( + :: serde :: Deserialize, + :: serde :: Serialize, + Clone, + Copy, + Debug, + Eq, + Hash, + Ord, + PartialEq, + PartialOrd, +)] +pub enum ConstPropertyKind { + #[serde(rename = "widget")] + Widget, +} +impl ::std::fmt::Display for ConstPropertyKind { + fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { + match *self { + Self::Widget => f.write_str("widget"), + } + } +} +impl ::std::str::FromStr for ConstPropertyKind { + type Err = self::error::ConversionError; + fn from_str(value: &str) -> ::std::result::Result { + match value { + "widget" => Ok(Self::Widget), + _ => Err("invalid value".into()), + } + } +} +impl ::std::convert::TryFrom<&str> for ConstPropertyKind { + type Error = self::error::ConversionError; + fn try_from(value: &str) -> ::std::result::Result { + value.parse() + } +} +impl ::std::convert::TryFrom<::std::string::String> for ConstPropertyKind { + type Error = self::error::ConversionError; + fn try_from( + value: ::std::string::String, + ) -> ::std::result::Result { + value.parse() + } +} +impl ::std::default::Default for ConstPropertyKind { + fn default() -> Self { + ConstPropertyKind::Widget + } +} +#[doc = "`ConstString`"] +#[derive( + :: serde :: Deserialize, + :: serde :: Serialize, + Clone, + Copy, + Debug, + Eq, + Hash, + Ord, + PartialEq, + PartialOrd, +)] +pub enum ConstString { + #[serde(rename = "fixed")] + Fixed, +} +impl ::std::fmt::Display for ConstString { + fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { + match *self { + Self::Fixed => f.write_str("fixed"), + } + } +} +impl ::std::str::FromStr for ConstString { + type Err = self::error::ConversionError; + fn from_str(value: &str) -> ::std::result::Result { + match value { + "fixed" => Ok(Self::Fixed), + _ => Err("invalid value".into()), + } + } +} +impl ::std::convert::TryFrom<&str> for ConstString { + type Error = self::error::ConversionError; + fn try_from(value: &str) -> ::std::result::Result { + value.parse() + } +} +impl ::std::convert::TryFrom<::std::string::String> for ConstString { + type Error = self::error::ConversionError; + fn try_from( + value: ::std::string::String, + ) -> ::std::result::Result { + value.parse() + } +} #[doc = "`DiskAttachment`"] #[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug)] pub struct DiskAttachment { @@ -1027,6 +1188,44 @@ impl ::std::convert::TryFrom<::std::string::String> for VariantsDifferByPunct { } #[doc = " Types for composing complex structures."] pub mod builder { + #[derive(Clone, Debug)] + pub struct ConstProperty { + kind: ::std::result::Result, + } + impl ::std::default::Default for ConstProperty { + fn default() -> Self { + Self { + kind: Ok(super::defaults::const_property_kind()), + } + } + } + impl ConstProperty { + pub fn kind(mut self, value: T) -> Self + where + T: ::std::convert::TryInto, + T::Error: ::std::fmt::Display, + { + self.kind = value + .try_into() + .map_err(|e| format!("error converting supplied value for kind: {e}")); + self + } + } + impl ::std::convert::TryFrom for super::ConstProperty { + type Error = super::error::ConversionError; + fn try_from( + value: ConstProperty, + ) -> ::std::result::Result { + Ok(Self { kind: value.kind? }) + } + } + impl ::std::convert::From for ConstProperty { + fn from(value: super::ConstProperty) -> Self { + Self { + kind: Ok(value.kind), + } + } + } #[derive(Clone, Debug)] pub struct DiskAttachment { alternate: ::std::result::Result, @@ -1123,6 +1322,12 @@ pub mod builder { } } } +#[doc = " Generation of default values for serde."] +pub mod defaults { + pub(super) fn const_property_kind() -> super::ConstPropertyKind { + super::ConstPropertyKind::Widget + } +} #[doc = " Error types."] pub mod error { #[doc = r" Error from a `TryFrom` or `FromStr` implementation."]