// @generated by jacquard-lexicon. DO NOT EDIT.
//
// Lexicon: science.alt.dataset.schemaType
//
// This file was automatically generated from Lexicon schemas.
// Any manual changes will be overwritten on the next regeneration.

use jacquard_common::{CowStr, BosStr, DefaultStr, FromStaticStr};
use jacquard_derive::IntoStatic;
use serde::{Serialize, Deserialize};
/// JSON Schema Draft 7 format for sample type definitions. When schemaType is 'jsonSchema', the schema field must contain an object conforming to science.alt.dataset.schema#jsonSchemaFormat.

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic, Hash)]
pub struct JsonSchema;
impl core::fmt::Display for JsonSchema {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "jsonSchema")
    }
}

/// Schema type identifier for atdata sample definitions. Known values correspond to token definitions in this Lexicon. New schema types can be added as tokens without breaking changes.

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum SchemaType<S: BosStr = DefaultStr> {
    JsonSchema,
    Other(S),
}

impl<S: BosStr> SchemaType<S> {
    pub fn as_str(&self) -> &str {
        match self {
            Self::JsonSchema => "jsonSchema",
            Self::Other(s) => s.as_ref(),
        }
    }
    /// Construct from a string-like value, matching known values.
    pub fn from_value(s: S) -> Self {
        match s.as_ref() {
            "jsonSchema" => Self::JsonSchema,
            _ => Self::Other(s),
        }
    }
}

impl<S: BosStr> AsRef<str> for SchemaType<S> {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

impl<S: BosStr> core::fmt::Display for SchemaType<S> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

impl<S: BosStr> Serialize for SchemaType<S> {
    fn serialize<Ser>(&self, serializer: Ser) -> Result<Ser::Ok, Ser::Error>
    where
        Ser: serde::Serializer,
    {
        serializer.serialize_str(self.as_str())
    }
}

impl<'de, S: Deserialize<'de> + BosStr> Deserialize<'de> for SchemaType<S> {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let s = S::deserialize(deserializer)?;
        Ok(Self::from_value(s))
    }
}

impl<S: BosStr> jacquard_common::IntoStatic for SchemaType<S>
where
    S: BosStr + jacquard_common::IntoStatic,
    S::Output: BosStr,
{
    type Output = SchemaType<S::Output>;
    fn into_static(self) -> Self::Output {
        match self {
            SchemaType::JsonSchema => SchemaType::JsonSchema,
            SchemaType::Other(v) => SchemaType::Other(v.into_static()),
        }
    }
}