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

#[allow(unused_imports)]
use core::marker::PhantomData;
use jacquard_common::{CowStr, BosStr, DefaultStr, FromStaticStr};
use jacquard_common::deps::bytes::Bytes;
use jacquard_derive::{IntoStatic, open_union};
use serde::{Serialize, Deserialize};
use crate::place_stream::live::subscribe_segments;

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase", bound(deserialize = "S: Deserialize<'de> + BosStr"))]
pub struct SubscribeSegments<S: BosStr = DefaultStr> {
    pub streamer: S,
}


#[open_union]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(tag = "$type", bound(deserialize = "S: Deserialize<'de> + BosStr"))]
pub enum SubscribeSegmentsMessage<S: BosStr = DefaultStr> {
    #[serde(rename = "#segment")]
    Segment(Box<subscribe_segments::Segment>),
}

impl<S: BosStr> SubscribeSegmentsMessage<S> {
    /// Decode a framed DAG-CBOR message (header + body).
    pub fn decode_framed<'de>(
        bytes: &'de [u8],
    ) -> Result<SubscribeSegmentsMessage<S>, jacquard_common::error::DecodeError>
    where
        S: serde::Deserialize<'de>,
    {
        let (header, body) = jacquard_common::xrpc::subscription::parse_event_header(
            bytes,
        )?;
        match header.t.as_str() {
            "#segment" => {
                let variant = jacquard_common::deps::codegen::serde_ipld_dagcbor::from_slice(
                    body,
                )?;
                Ok(Self::Segment(Box::new(variant)))
            }
            unknown => {
                Err(
                    jacquard_common::error::DecodeError::UnknownEventType(unknown.into()),
                )
            }
        }
    }
}


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

#[serde(tag = "error", content = "message")]
pub enum SubscribeSegmentsError {
    /// Catch-all for unknown error codes.
    #[serde(untagged)]
    Other {
        error: jacquard_common::deps::smol_str::SmolStr,
        message: Option<jacquard_common::deps::smol_str::SmolStr>,
    },
}

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

pub type Segment = Bytes;
///Stream response type for
///place.stream.live.subscribeSegments
pub struct SubscribeSegmentsStream;
impl jacquard_common::xrpc::SubscriptionResp for SubscribeSegmentsStream {
    const NSID: &'static str = "place.stream.live.subscribeSegments";
    const ENCODING: jacquard_common::xrpc::MessageEncoding = jacquard_common::xrpc::MessageEncoding::Json;
    type Message<S: BosStr> = SubscribeSegmentsMessage<S>;
    type Error = SubscribeSegmentsError;
}

impl<S: BosStr> jacquard_common::xrpc::XrpcSubscription for SubscribeSegments<S> {
    const NSID: &'static str = "place.stream.live.subscribeSegments";
    const ENCODING: jacquard_common::xrpc::MessageEncoding = jacquard_common::xrpc::MessageEncoding::Json;
    type Stream = SubscribeSegmentsStream;
}

pub struct SubscribeSegmentsEndpoint;
impl jacquard_common::xrpc::SubscriptionEndpoint for SubscribeSegmentsEndpoint {
    const PATH: &'static str = "/xrpc/place.stream.live.subscribeSegments";
    const ENCODING: jacquard_common::xrpc::MessageEncoding = jacquard_common::xrpc::MessageEncoding::Json;
    type Params<S: BosStr> = SubscribeSegments<S>;
    type Stream = SubscribeSegmentsStream;
}

pub mod subscribe_segments_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 Streamer;
    }
    /// Empty state - all required fields are unset
    pub struct Empty(());
    impl sealed::Sealed for Empty {}
    impl State for Empty {
        type Streamer = Unset;
    }
    ///State transition - sets the `streamer` field to Set
    pub struct SetStreamer<St: State = Empty>(PhantomData<fn() -> St>);
    impl<St: State> sealed::Sealed for SetStreamer<St> {}
    impl<St: State> State for SetStreamer<St> {
        type Streamer = Set<members::streamer>;
    }
    /// Marker types for field names
    #[allow(non_camel_case_types)]
    pub mod members {
        ///Marker type for the `streamer` field
        pub struct streamer(());
    }
}

/// Builder for constructing an instance of this type.
pub struct SubscribeSegmentsBuilder<
    St: subscribe_segments_state::State,
    S: BosStr = DefaultStr,
> {
    _state: PhantomData<fn() -> St>,
    _fields: (Option<S>,),
    _type: PhantomData<fn() -> S>,
}

impl SubscribeSegments<DefaultStr> {
    /// Create a new builder for this type, using the default string type (DefaultStr = SmolStr) if needed
    pub fn new() -> SubscribeSegmentsBuilder<
        subscribe_segments_state::Empty,
        DefaultStr,
    > {
        SubscribeSegmentsBuilder::new()
    }
}

impl<S: BosStr> SubscribeSegments<S> {
    /// Create a new builder for this type
    pub fn builder() -> SubscribeSegmentsBuilder<subscribe_segments_state::Empty, S> {
        SubscribeSegmentsBuilder::builder()
    }
}

impl SubscribeSegmentsBuilder<subscribe_segments_state::Empty, DefaultStr> {
    /// Create a new builder with all fields unset, using the default string type, if needed
    pub fn new() -> Self {
        SubscribeSegmentsBuilder {
            _state: PhantomData,
            _fields: (None,),
            _type: PhantomData,
        }
    }
}

impl<S: BosStr> SubscribeSegmentsBuilder<subscribe_segments_state::Empty, S> {
    /// Create a new builder with all fields unset
    pub fn builder() -> Self {
        SubscribeSegmentsBuilder {
            _state: PhantomData,
            _fields: (None,),
            _type: PhantomData,
        }
    }
}

impl<St, S: BosStr> SubscribeSegmentsBuilder<St, S>
where
    St: subscribe_segments_state::State,
    St::Streamer: subscribe_segments_state::IsUnset,
{
    /// Set the `streamer` field (required)
    pub fn streamer(
        mut self,
        value: impl Into<S>,
    ) -> SubscribeSegmentsBuilder<subscribe_segments_state::SetStreamer<St>, S> {
        self._fields.0 = Option::Some(value.into());
        SubscribeSegmentsBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St, S: BosStr> SubscribeSegmentsBuilder<St, S>
where
    St: subscribe_segments_state::State,
    St::Streamer: subscribe_segments_state::IsSet,
{
    /// Build the final struct.
    pub fn build(self) -> SubscribeSegments<S> {
        SubscribeSegments {
            streamer: self._fields.0.unwrap(),
        }
    }
}