// @generated by jacquard-lexicon. DO NOT EDIT.
//
// Lexicon: mov.danabra.Post
//
// 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;
use jacquard_derive::{IntoStatic, lexicon};
use serde::{Serialize, Deserialize};
use crate::at_inlay::Response;

#[lexicon]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase")]
pub struct Post<'a> {
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub foo: Option<PostFoo<'a>>,
    #[serde(borrow)]
    pub uri: AtUri<'a>,
}


#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum PostFoo<'a> {
    Bar,
    Baz,
    Other(CowStr<'a>),
}

impl<'a> PostFoo<'a> {
    pub fn as_str(&self) -> &str {
        match self {
            Self::Bar => "bar",
            Self::Baz => "baz",
            Self::Other(s) => s.as_ref(),
        }
    }
}

impl<'a> From<&'a str> for PostFoo<'a> {
    fn from(s: &'a str) -> Self {
        match s {
            "bar" => Self::Bar,
            "baz" => Self::Baz,
            _ => Self::Other(CowStr::from(s)),
        }
    }
}

impl<'a> From<String> for PostFoo<'a> {
    fn from(s: String) -> Self {
        match s.as_str() {
            "bar" => Self::Bar,
            "baz" => Self::Baz,
            _ => Self::Other(CowStr::from(s)),
        }
    }
}

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

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

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

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


#[lexicon]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase")]
pub struct PostOutput<'a> {
    #[serde(flatten)]
    #[serde(borrow)]
    pub value: Response<'a>,
}

/// Response type for mov.danabra.Post
pub struct PostResponse;
impl jacquard_common::xrpc::XrpcResp for PostResponse {
    const NSID: &'static str = "mov.danabra.Post";
    const ENCODING: &'static str = "application/json";
    type Output<'de> = PostOutput<'de>;
    type Err<'de> = jacquard_common::xrpc::GenericError<'de>;
}

impl<'a> jacquard_common::xrpc::XrpcRequest for Post<'a> {
    const NSID: &'static str = "mov.danabra.Post";
    const METHOD: jacquard_common::xrpc::XrpcMethod = jacquard_common::xrpc::XrpcMethod::Procedure(
        "application/json",
    );
    type Response = PostResponse;
}

/// Endpoint type for mov.danabra.Post
pub struct PostRequest;
impl jacquard_common::xrpc::XrpcEndpoint for PostRequest {
    const PATH: &'static str = "/xrpc/mov.danabra.Post";
    const METHOD: jacquard_common::xrpc::XrpcMethod = jacquard_common::xrpc::XrpcMethod::Procedure(
        "application/json",
    );
    type Request<'de> = Post<'de>;
    type Response = PostResponse;
}

pub mod post_state {

    pub use crate::builder_types::{Set, Unset, IsSet, IsUnset};
    #[allow(unused)]
    use ::core::marker::PhantomData;
    mod sealed {
        pub trait Sealed {}
    }
    /// State trait tracking which required fields have been set
    pub trait State: sealed::Sealed {
        type Uri;
    }
    /// Empty state - all required fields are unset
    pub struct Empty(());
    impl sealed::Sealed for Empty {}
    impl State for Empty {
        type Uri = Unset;
    }
    ///State transition - sets the `uri` field to Set
    pub struct SetUri<S: State = Empty>(PhantomData<fn() -> S>);
    impl<S: State> sealed::Sealed for SetUri<S> {}
    impl<S: State> State for SetUri<S> {
        type Uri = Set<members::uri>;
    }
    /// Marker types for field names
    #[allow(non_camel_case_types)]
    pub mod members {
        ///Marker type for the `uri` field
        pub struct uri(());
    }
}

/// Builder for constructing an instance of this type
pub struct PostBuilder<'a, S: post_state::State> {
    _state: PhantomData<fn() -> S>,
    _fields: (Option<PostFoo<'a>>, Option<AtUri<'a>>),
    _lifetime: PhantomData<&'a ()>,
}

impl<'a> Post<'a> {
    /// Create a new builder for this type
    pub fn new() -> PostBuilder<'a, post_state::Empty> {
        PostBuilder::new()
    }
}

impl<'a> PostBuilder<'a, post_state::Empty> {
    /// Create a new builder with all fields unset
    pub fn new() -> Self {
        PostBuilder {
            _state: PhantomData,
            _fields: (None, None),
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S: post_state::State> PostBuilder<'a, S> {
    /// Set the `foo` field (optional)
    pub fn foo(mut self, value: impl Into<Option<PostFoo<'a>>>) -> Self {
        self._fields.0 = value.into();
        self
    }
    /// Set the `foo` field to an Option value (optional)
    pub fn maybe_foo(mut self, value: Option<PostFoo<'a>>) -> Self {
        self._fields.0 = value;
        self
    }
}

impl<'a, S> PostBuilder<'a, S>
where
    S: post_state::State,
    S::Uri: post_state::IsUnset,
{
    /// Set the `uri` field (required)
    pub fn uri(
        mut self,
        value: impl Into<AtUri<'a>>,
    ) -> PostBuilder<'a, post_state::SetUri<S>> {
        self._fields.1 = Option::Some(value.into());
        PostBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S> PostBuilder<'a, S>
where
    S: post_state::State,
    S::Uri: post_state::IsSet,
{
    /// Build the final struct
    pub fn build(self) -> Post<'a> {
        Post {
            foo: self._fields.0,
            uri: self._fields.1.unwrap(),
            extra_data: Default::default(),
        }
    }
    /// Build the final struct with custom extra_data
    pub fn build_with_data(
        self,
        extra_data: BTreeMap<
            jacquard_common::deps::smol_str::SmolStr,
            jacquard_common::types::value::Data<'a>,
        >,
    ) -> Post<'a> {
        Post {
            foo: self._fields.0,
            uri: self._fields.1.unwrap(),
            extra_data: Some(extra_data),
        }
    }
}