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

use jacquard_common::CowStr;

#[allow(unused_imports)]
use jacquard_common::deps::codegen::unicode_segmentation::UnicodeSegmentation;
use jacquard_common::types::string::UriValue;
use jacquard_derive::{IntoStatic, lexicon};
use jacquard_lexicon::lexicon::LexiconDoc;
use jacquard_lexicon::schema::LexiconSchema;

#[allow(unused_imports)]
use jacquard_lexicon::validation::{ConstraintError, ValidationPath};
use serde::{Serialize, Deserialize};
/// Attribution for a creative work

#[lexicon]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic, Default)]
#[serde(rename_all = "camelCase")]
pub struct Credit<'a> {
    ///Name of the credited person
    #[serde(borrow)]
    pub name: CowStr<'a>,
    ///Role: composer, lyricist, arranger, etc
    #[serde(borrow)]
    pub role: CreditRole<'a>,
    ///URL to credited person's profile or website
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub url: Option<UriValue<'a>>,
}

/// Role: composer, lyricist, arranger, etc

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum CreditRole<'a> {
    Composer,
    Arranger,
    Lyricist,
    Transcriber,
    Orchestrator,
    Adapter,
    Artist,
    Producer,
    Other(CowStr<'a>),
}

impl<'a> CreditRole<'a> {
    pub fn as_str(&self) -> &str {
        match self {
            Self::Composer => "composer",
            Self::Arranger => "arranger",
            Self::Lyricist => "lyricist",
            Self::Transcriber => "transcriber",
            Self::Orchestrator => "orchestrator",
            Self::Adapter => "adapter",
            Self::Artist => "artist",
            Self::Producer => "producer",
            Self::Other(s) => s.as_ref(),
        }
    }
}

impl<'a> From<&'a str> for CreditRole<'a> {
    fn from(s: &'a str) -> Self {
        match s {
            "composer" => Self::Composer,
            "arranger" => Self::Arranger,
            "lyricist" => Self::Lyricist,
            "transcriber" => Self::Transcriber,
            "orchestrator" => Self::Orchestrator,
            "adapter" => Self::Adapter,
            "artist" => Self::Artist,
            "producer" => Self::Producer,
            _ => Self::Other(CowStr::from(s)),
        }
    }
}

impl<'a> From<String> for CreditRole<'a> {
    fn from(s: String) -> Self {
        match s.as_str() {
            "composer" => Self::Composer,
            "arranger" => Self::Arranger,
            "lyricist" => Self::Lyricist,
            "transcriber" => Self::Transcriber,
            "orchestrator" => Self::Orchestrator,
            "adapter" => Self::Adapter,
            "artist" => Self::Artist,
            "producer" => Self::Producer,
            _ => Self::Other(CowStr::from(s)),
        }
    }
}

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

impl<'a> AsRef<str> for CreditRole<'a> {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

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

impl<'de, 'a> serde::Deserialize<'de> for CreditRole<'a>
where
    'de: 'a,
{
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let s = <&'de str>::deserialize(deserializer)?;
        Ok(Self::from(s))
    }
}

impl<'a> Default for CreditRole<'a> {
    fn default() -> Self {
        Self::Other(Default::default())
    }
}

impl jacquard_common::IntoStatic for CreditRole<'_> {
    type Output = CreditRole<'static>;
    fn into_static(self) -> Self::Output {
        match self {
            CreditRole::Composer => CreditRole::Composer,
            CreditRole::Arranger => CreditRole::Arranger,
            CreditRole::Lyricist => CreditRole::Lyricist,
            CreditRole::Transcriber => CreditRole::Transcriber,
            CreditRole::Orchestrator => CreditRole::Orchestrator,
            CreditRole::Adapter => CreditRole::Adapter,
            CreditRole::Artist => CreditRole::Artist,
            CreditRole::Producer => CreditRole::Producer,
            CreditRole::Other(v) => CreditRole::Other(v.into_static()),
        }
    }
}

impl<'a> LexiconSchema for Credit<'a> {
    fn nsid() -> &'static str {
        "io.sound.credit"
    }
    fn def_name() -> &'static str {
        "main"
    }
    fn lexicon_doc() -> LexiconDoc<'static> {
        lexicon_doc_io_sound_credit()
    }
    fn validate(&self) -> Result<(), ConstraintError> {
        {
            let value = &self.name;
            #[allow(unused_comparisons)]
            if <str>::len(value.as_ref()) > 640usize {
                return Err(ConstraintError::MaxLength {
                    path: ValidationPath::from_field("name"),
                    max: 640usize,
                    actual: <str>::len(value.as_ref()),
                });
            }
        }
        {
            let value = &self.name;
            {
                let count = UnicodeSegmentation::graphemes(value.as_ref(), true).count();
                if count > 320usize {
                    return Err(ConstraintError::MaxGraphemes {
                        path: ValidationPath::from_field("name"),
                        max: 320usize,
                        actual: count,
                    });
                }
            }
        }
        {
            let value = &self.role;
            #[allow(unused_comparisons)]
            if <str>::len(value.as_ref()) > 64usize {
                return Err(ConstraintError::MaxLength {
                    path: ValidationPath::from_field("role"),
                    max: 64usize,
                    actual: <str>::len(value.as_ref()),
                });
            }
        }
        {
            let value = &self.role;
            {
                let count = UnicodeSegmentation::graphemes(value.as_ref(), true).count();
                if count > 32usize {
                    return Err(ConstraintError::MaxGraphemes {
                        path: ValidationPath::from_field("role"),
                        max: 32usize,
                        actual: count,
                    });
                }
            }
        }
        Ok(())
    }
}

fn lexicon_doc_io_sound_credit() -> LexiconDoc<'static> {
    #[allow(unused_imports)]
    use jacquard_common::{CowStr, deps::smol_str::SmolStr, types::blob::MimeType};
    use jacquard_lexicon::lexicon::*;
    use alloc::collections::BTreeMap;
    LexiconDoc {
        lexicon: Lexicon::Lexicon1,
        id: CowStr::new_static("io.sound.credit"),
        defs: {
            let mut map = BTreeMap::new();
            map.insert(
                SmolStr::new_static("main"),
                LexUserType::Object(LexObject {
                    description: Some(
                        CowStr::new_static("Attribution for a creative work"),
                    ),
                    required: Some(
                        vec![SmolStr::new_static("name"), SmolStr::new_static("role")],
                    ),
                    properties: {
                        #[allow(unused_mut)]
                        let mut map = BTreeMap::new();
                        map.insert(
                            SmolStr::new_static("name"),
                            LexObjectProperty::String(LexString {
                                description: Some(
                                    CowStr::new_static("Name of the credited person"),
                                ),
                                max_length: Some(640usize),
                                max_graphemes: Some(320usize),
                                ..Default::default()
                            }),
                        );
                        map.insert(
                            SmolStr::new_static("role"),
                            LexObjectProperty::String(LexString {
                                description: Some(
                                    CowStr::new_static(
                                        "Role: composer, lyricist, arranger, etc",
                                    ),
                                ),
                                max_length: Some(64usize),
                                max_graphemes: Some(32usize),
                                ..Default::default()
                            }),
                        );
                        map.insert(
                            SmolStr::new_static("url"),
                            LexObjectProperty::String(LexString {
                                description: Some(
                                    CowStr::new_static(
                                        "URL to credited person's profile or website",
                                    ),
                                ),
                                format: Some(LexStringFormat::Uri),
                                ..Default::default()
                            }),
                        );
                        map
                    },
                    ..Default::default()
                }),
            );
            map
        },
        ..Default::default()
    }
}