// @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;
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")]
pub struct SubscribeSegments<'a> {
    #[serde(borrow)]
    pub streamer: CowStr<'a>,
}


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

impl<'a> SubscribeSegmentsMessage<'a> {
    /// Decode a framed DAG-CBOR message (header + body).
    pub fn decode_framed<'de: 'a>(
        bytes: &'de [u8],
    ) -> Result<SubscribeSegmentsMessage<'a>, jacquard_common::error::DecodeError> {
        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()),
                )
            }
        }
    }
}

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<'de> = SubscribeSegmentsMessage<'de>;
    type Error<'de> = jacquard_common::xrpc::GenericError<'de>;
}

impl<'a> jacquard_common::xrpc::XrpcSubscription for SubscribeSegments<'a> {
    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<'de> = SubscribeSegments<'de>;
    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<S: State = Empty>(PhantomData<fn() -> S>);
    impl<S: State> sealed::Sealed for SetStreamer<S> {}
    impl<S: State> State for SetStreamer<S> {
        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<'a, S: subscribe_segments_state::State> {
    _state: PhantomData<fn() -> S>,
    _fields: (Option<CowStr<'a>>,),
    _lifetime: PhantomData<&'a ()>,
}

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

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

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

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