// @generated by jacquard-lexicon. DO NOT EDIT.
//
// Lexicon: com.atproto.server.updateEmail
//
// 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_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 UpdateEmail<'a> {
    #[serde(borrow)]
    pub email: CowStr<'a>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub email_auth_factor: Option<bool>,
    ///Requires a token from com.atproto.sever.requestEmailUpdate if the account's email has been confirmed.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub token: Option<CowStr<'a>>,
}


#[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 UpdateEmailError<'a> {
    #[serde(rename = "ExpiredToken")]
    ExpiredToken(Option<CowStr<'a>>),
    #[serde(rename = "InvalidToken")]
    InvalidToken(Option<CowStr<'a>>),
    #[serde(rename = "TokenRequired")]
    TokenRequired(Option<CowStr<'a>>),
}

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

/// Response type for com.atproto.server.updateEmail
pub struct UpdateEmailResponse;
impl jacquard_common::xrpc::XrpcResp for UpdateEmailResponse {
    const NSID: &'static str = "com.atproto.server.updateEmail";
    const ENCODING: &'static str = "application/json";
    type Output<'de> = ();
    type Err<'de> = UpdateEmailError<'de>;
}

impl<'a> jacquard_common::xrpc::XrpcRequest for UpdateEmail<'a> {
    const NSID: &'static str = "com.atproto.server.updateEmail";
    const METHOD: jacquard_common::xrpc::XrpcMethod = jacquard_common::xrpc::XrpcMethod::Procedure(
        "application/json",
    );
    type Response = UpdateEmailResponse;
}

/// Endpoint type for com.atproto.server.updateEmail
pub struct UpdateEmailRequest;
impl jacquard_common::xrpc::XrpcEndpoint for UpdateEmailRequest {
    const PATH: &'static str = "/xrpc/com.atproto.server.updateEmail";
    const METHOD: jacquard_common::xrpc::XrpcMethod = jacquard_common::xrpc::XrpcMethod::Procedure(
        "application/json",
    );
    type Request<'de> = UpdateEmail<'de>;
    type Response = UpdateEmailResponse;
}