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

#[allow(unused_imports)]
use alloc::collections::BTreeMap;

#[allow(unused_imports)]
use core::marker::PhantomData;
use jacquard_common::CowStr;
use jacquard_common::types::string::{AtUri, Cid};
use jacquard_derive::{IntoStatic, lexicon, open_union};
use serde::{Serialize, Deserialize};

#[lexicon]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic, Default)]
#[serde(rename_all = "camelCase")]
pub struct RequestCrew<'a> {
    ///Requested permissions (default: ['blob:read', 'blob:write'])
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub permissions: Option<Vec<CowStr<'a>>>,
    ///Requested role (default: 'member')  Defaults to `"member"`.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(default = "_default_request_crew_role")]
    #[serde(borrow)]
    pub role: Option<CowStr<'a>>,
}


#[lexicon]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic, Default)]
#[serde(rename_all = "camelCase")]
pub struct RequestCrewOutput<'a> {
    ///CID of the crew record
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub cid: Option<Cid<'a>>,
    ///Human-readable status message
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub message: Option<CowStr<'a>>,
    ///Result status
    #[serde(borrow)]
    pub status: RequestCrewOutputStatus<'a>,
    ///AT-URI of the crew record (if created or already exists)
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub uri: Option<AtUri<'a>>,
}

/// Result status

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum RequestCrewOutputStatus<'a> {
    Created,
    AlreadyMember,
    Other(CowStr<'a>),
}

impl<'a> RequestCrewOutputStatus<'a> {
    pub fn as_str(&self) -> &str {
        match self {
            Self::Created => "created",
            Self::AlreadyMember => "already_member",
            Self::Other(s) => s.as_ref(),
        }
    }
}

impl<'a> From<&'a str> for RequestCrewOutputStatus<'a> {
    fn from(s: &'a str) -> Self {
        match s {
            "created" => Self::Created,
            "already_member" => Self::AlreadyMember,
            _ => Self::Other(CowStr::from(s)),
        }
    }
}

impl<'a> From<String> for RequestCrewOutputStatus<'a> {
    fn from(s: String) -> Self {
        match s.as_str() {
            "created" => Self::Created,
            "already_member" => Self::AlreadyMember,
            _ => Self::Other(CowStr::from(s)),
        }
    }
}

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

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

impl<'a> serde::Serialize for RequestCrewOutputStatus<'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 RequestCrewOutputStatus<'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 RequestCrewOutputStatus<'a> {
    fn default() -> Self {
        Self::Other(Default::default())
    }
}

impl jacquard_common::IntoStatic for RequestCrewOutputStatus<'_> {
    type Output = RequestCrewOutputStatus<'static>;
    fn into_static(self) -> Self::Output {
        match self {
            RequestCrewOutputStatus::Created => RequestCrewOutputStatus::Created,
            RequestCrewOutputStatus::AlreadyMember => {
                RequestCrewOutputStatus::AlreadyMember
            }
            RequestCrewOutputStatus::Other(v) => {
                RequestCrewOutputStatus::Other(v.into_static())
            }
        }
    }
}


#[open_union]
#[derive(
    Serialize,
    Deserialize,
    Debug,
    Clone,
    PartialEq,
    Eq,
    thiserror::Error,
    miette::Diagnostic,
    IntoStatic
)]

#[serde(tag = "error", content = "message")]
#[serde(bound(deserialize = "'de: 'a"))]
pub enum RequestCrewError<'a> {
    #[serde(rename = "AuthRequired")]
    AuthRequired(Option<CowStr<'a>>),
    #[serde(rename = "RegistrationDisabled")]
    RegistrationDisabled(Option<CowStr<'a>>),
}

impl core::fmt::Display for RequestCrewError<'_> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::AuthRequired(msg) => {
                write!(f, "AuthRequired")?;
                if let Some(msg) = msg {
                    write!(f, ": {}", msg)?;
                }
                Ok(())
            }
            Self::RegistrationDisabled(msg) => {
                write!(f, "RegistrationDisabled")?;
                if let Some(msg) = msg {
                    write!(f, ": {}", msg)?;
                }
                Ok(())
            }
            Self::Unknown(err) => write!(f, "Unknown error: {:?}", err),
        }
    }
}

/// Response type for io.atcr.hold.requestCrew
pub struct RequestCrewResponse;
impl jacquard_common::xrpc::XrpcResp for RequestCrewResponse {
    const NSID: &'static str = "io.atcr.hold.requestCrew";
    const ENCODING: &'static str = "application/json";
    type Output<'de> = RequestCrewOutput<'de>;
    type Err<'de> = RequestCrewError<'de>;
}

impl<'a> jacquard_common::xrpc::XrpcRequest for RequestCrew<'a> {
    const NSID: &'static str = "io.atcr.hold.requestCrew";
    const METHOD: jacquard_common::xrpc::XrpcMethod = jacquard_common::xrpc::XrpcMethod::Procedure(
        "application/json",
    );
    type Response = RequestCrewResponse;
}

/// Endpoint type for io.atcr.hold.requestCrew
pub struct RequestCrewRequest;
impl jacquard_common::xrpc::XrpcEndpoint for RequestCrewRequest {
    const PATH: &'static str = "/xrpc/io.atcr.hold.requestCrew";
    const METHOD: jacquard_common::xrpc::XrpcMethod = jacquard_common::xrpc::XrpcMethod::Procedure(
        "application/json",
    );
    type Request<'de> = RequestCrew<'de>;
    type Response = RequestCrewResponse;
}

fn _default_request_crew_role() -> Option<CowStr<'static>> {
    Some(CowStr::from("member"))
}