// @generated by jacquard-lexicon. DO NOT EDIT.
//
// Lexicon: sh.weaver.notebook.getPage
//
// 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, open_union};
use serde::{Serialize, Deserialize};
use crate::sh_weaver::notebook::EntryView;
use crate::sh_weaver::notebook::PageView;

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase")]
pub struct GetPage<'a> {
    #[serde(borrow)]
    pub page: AtUri<'a>,
}


#[lexicon]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase")]
pub struct GetPageOutput<'a> {
    #[serde(borrow)]
    pub entries: Vec<EntryView<'a>>,
    #[serde(borrow)]
    pub page: PageView<'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 GetPageError<'a> {
    #[serde(rename = "PageNotFound")]
    PageNotFound(Option<CowStr<'a>>),
}

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

/// Response type for sh.weaver.notebook.getPage
pub struct GetPageResponse;
impl jacquard_common::xrpc::XrpcResp for GetPageResponse {
    const NSID: &'static str = "sh.weaver.notebook.getPage";
    const ENCODING: &'static str = "application/json";
    type Output<'de> = GetPageOutput<'de>;
    type Err<'de> = GetPageError<'de>;
}

impl<'a> jacquard_common::xrpc::XrpcRequest for GetPage<'a> {
    const NSID: &'static str = "sh.weaver.notebook.getPage";
    const METHOD: jacquard_common::xrpc::XrpcMethod = jacquard_common::xrpc::XrpcMethod::Query;
    type Response = GetPageResponse;
}

/// Endpoint type for sh.weaver.notebook.getPage
pub struct GetPageRequest;
impl jacquard_common::xrpc::XrpcEndpoint for GetPageRequest {
    const PATH: &'static str = "/xrpc/sh.weaver.notebook.getPage";
    const METHOD: jacquard_common::xrpc::XrpcMethod = jacquard_common::xrpc::XrpcMethod::Query;
    type Request<'de> = GetPage<'de>;
    type Response = GetPageResponse;
}

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

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

impl<'a> GetPage<'a> {
    /// Create a new builder for this type
    pub fn new() -> GetPageBuilder<'a, get_page_state::Empty> {
        GetPageBuilder::new()
    }
}

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

impl<'a, S> GetPageBuilder<'a, S>
where
    S: get_page_state::State,
    S::Page: get_page_state::IsUnset,
{
    /// Set the `page` field (required)
    pub fn page(
        mut self,
        value: impl Into<AtUri<'a>>,
    ) -> GetPageBuilder<'a, get_page_state::SetPage<S>> {
        self._fields.0 = Option::Some(value.into());
        GetPageBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S> GetPageBuilder<'a, S>
where
    S: get_page_state::State,
    S::Page: get_page_state::IsSet,
{
    /// Build the final struct
    pub fn build(self) -> GetPage<'a> {
        GetPage {
            page: self._fields.0.unwrap(),
        }
    }
}