#[allow(unused_imports)]
use alloc::collections::BTreeMap;
#[allow(unused_imports)]
use core::marker::PhantomData;
use jacquard_common::{BosStr, CowStr, DefaultStr, FromStaticStr};
#[allow(unused_imports)]
use jacquard_common::deps::codegen::unicode_segmentation::UnicodeSegmentation;
use jacquard_common::deps::smol_str::SmolStr;
use jacquard_common::types::collection::{Collection, RecordError};
use jacquard_common::types::string::{AtUri, Cid};
use jacquard_common::types::uri::{RecordUri, UriError};
use jacquard_common::types::value::Data;
use jacquard_common::xrpc::XrpcResp;
use jacquard_derive::{IntoStatic, lexicon};
use jacquard_lexicon::lexicon::LexiconDoc;
use jacquard_lexicon::schema::LexiconSchema;
#[allow(unused_imports)]
use jacquard_lexicon::validation::{ConstraintError, ValidationPath};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(
rename_all = "camelCase",
rename = "coop.hypha.spores.site.section",
tag = "$type",
bound(deserialize = "S: Deserialize<'de> + BosStr")
)]
pub struct Section<S: BosStr = DefaultStr> {
#[serde(skip_serializing_if = "Option::is_none")]
pub collection: Option<S>,
#[serde(skip_serializing_if = "Option::is_none")]
pub content: Option<S>,
#[serde(skip_serializing_if = "Option::is_none")]
pub format: Option<SectionFormat<S>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub hide_header: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub layout: Option<SectionLayout<S>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub limit: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub records: Option<Vec<AtUri<S>>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub r#ref: Option<AtUri<S>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub rkey: Option<S>,
#[serde(skip_serializing_if = "Option::is_none")]
pub title: Option<S>,
pub r#type: SectionType<S>,
#[serde(flatten, default, skip_serializing_if = "Option::is_none")]
pub extra_data: Option<BTreeMap<SmolStr, Data<S>>>,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum SectionFormat<S: BosStr = DefaultStr> {
Markdown,
Html,
Text,
Other(S),
}
impl<S: BosStr> SectionFormat<S> {
pub fn as_str(&self) -> &str {
match self {
Self::Markdown => "markdown",
Self::Html => "html",
Self::Text => "text",
Self::Other(s) => s.as_ref(),
}
}
pub fn from_value(s: S) -> Self {
match s.as_ref() {
"markdown" => Self::Markdown,
"html" => Self::Html,
"text" => Self::Text,
_ => Self::Other(s),
}
}
}
impl<S: BosStr> core::fmt::Display for SectionFormat<S> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", self.as_str())
}
}
impl<S: BosStr> AsRef<str> for SectionFormat<S> {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl<S: BosStr> Serialize for SectionFormat<S> {
fn serialize<Ser>(&self, serializer: Ser) -> Result<Ser::Ok, Ser::Error>
where
Ser: serde::Serializer,
{
serializer.serialize_str(self.as_str())
}
}
impl<'de, S: Deserialize<'de> + BosStr> Deserialize<'de> for SectionFormat<S> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let s = S::deserialize(deserializer)?;
Ok(Self::from_value(s))
}
}
impl<S: BosStr + Default> Default for SectionFormat<S> {
fn default() -> Self {
Self::Other(Default::default())
}
}
impl<S: BosStr> jacquard_common::IntoStatic for SectionFormat<S>
where
S: BosStr + jacquard_common::IntoStatic,
S::Output: BosStr,
{
type Output = SectionFormat<S::Output>;
fn into_static(self) -> Self::Output {
match self {
SectionFormat::Markdown => SectionFormat::Markdown,
SectionFormat::Html => SectionFormat::Html,
SectionFormat::Text => SectionFormat::Text,
SectionFormat::Other(v) => SectionFormat::Other(v.into_static()),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum SectionLayout<S: BosStr = DefaultStr> {
Post,
Card,
Image,
Link,
Links,
List,
Profile,
Raw,
Other(S),
}
impl<S: BosStr> SectionLayout<S> {
pub fn as_str(&self) -> &str {
match self {
Self::Post => "post",
Self::Card => "card",
Self::Image => "image",
Self::Link => "link",
Self::Links => "links",
Self::List => "list",
Self::Profile => "profile",
Self::Raw => "raw",
Self::Other(s) => s.as_ref(),
}
}
pub fn from_value(s: S) -> Self {
match s.as_ref() {
"post" => Self::Post,
"card" => Self::Card,
"image" => Self::Image,
"link" => Self::Link,
"links" => Self::Links,
"list" => Self::List,
"profile" => Self::Profile,
"raw" => Self::Raw,
_ => Self::Other(s),
}
}
}
impl<S: BosStr> core::fmt::Display for SectionLayout<S> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", self.as_str())
}
}
impl<S: BosStr> AsRef<str> for SectionLayout<S> {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl<S: BosStr> Serialize for SectionLayout<S> {
fn serialize<Ser>(&self, serializer: Ser) -> Result<Ser::Ok, Ser::Error>
where
Ser: serde::Serializer,
{
serializer.serialize_str(self.as_str())
}
}
impl<'de, S: Deserialize<'de> + BosStr> Deserialize<'de> for SectionLayout<S> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let s = S::deserialize(deserializer)?;
Ok(Self::from_value(s))
}
}
impl<S: BosStr + Default> Default for SectionLayout<S> {
fn default() -> Self {
Self::Other(Default::default())
}
}
impl<S: BosStr> jacquard_common::IntoStatic for SectionLayout<S>
where
S: BosStr + jacquard_common::IntoStatic,
S::Output: BosStr,
{
type Output = SectionLayout<S::Output>;
fn into_static(self) -> Self::Output {
match self {
SectionLayout::Post => SectionLayout::Post,
SectionLayout::Card => SectionLayout::Card,
SectionLayout::Image => SectionLayout::Image,
SectionLayout::Link => SectionLayout::Link,
SectionLayout::Links => SectionLayout::Links,
SectionLayout::List => SectionLayout::List,
SectionLayout::Profile => SectionLayout::Profile,
SectionLayout::Raw => SectionLayout::Raw,
SectionLayout::Other(v) => SectionLayout::Other(v.into_static()),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum SectionType<S: BosStr = DefaultStr> {
Collection,
Records,
Content,
Block,
Profile,
ShareToBluesky,
CollectedFlowers,
Other(S),
}
impl<S: BosStr> SectionType<S> {
pub fn as_str(&self) -> &str {
match self {
Self::Collection => "collection",
Self::Records => "records",
Self::Content => "content",
Self::Block => "block",
Self::Profile => "profile",
Self::ShareToBluesky => "share-to-bluesky",
Self::CollectedFlowers => "collected-flowers",
Self::Other(s) => s.as_ref(),
}
}
pub fn from_value(s: S) -> Self {
match s.as_ref() {
"collection" => Self::Collection,
"records" => Self::Records,
"content" => Self::Content,
"block" => Self::Block,
"profile" => Self::Profile,
"share-to-bluesky" => Self::ShareToBluesky,
"collected-flowers" => Self::CollectedFlowers,
_ => Self::Other(s),
}
}
}
impl<S: BosStr> core::fmt::Display for SectionType<S> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", self.as_str())
}
}
impl<S: BosStr> AsRef<str> for SectionType<S> {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl<S: BosStr> Serialize for SectionType<S> {
fn serialize<Ser>(&self, serializer: Ser) -> Result<Ser::Ok, Ser::Error>
where
Ser: serde::Serializer,
{
serializer.serialize_str(self.as_str())
}
}
impl<'de, S: Deserialize<'de> + BosStr> Deserialize<'de> for SectionType<S> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let s = S::deserialize(deserializer)?;
Ok(Self::from_value(s))
}
}
impl<S: BosStr + Default> Default for SectionType<S> {
fn default() -> Self {
Self::Other(Default::default())
}
}
impl<S: BosStr> jacquard_common::IntoStatic for SectionType<S>
where
S: BosStr + jacquard_common::IntoStatic,
S::Output: BosStr,
{
type Output = SectionType<S::Output>;
fn into_static(self) -> Self::Output {
match self {
SectionType::Collection => SectionType::Collection,
SectionType::Records => SectionType::Records,
SectionType::Content => SectionType::Content,
SectionType::Block => SectionType::Block,
SectionType::Profile => SectionType::Profile,
SectionType::ShareToBluesky => SectionType::ShareToBluesky,
SectionType::CollectedFlowers => SectionType::CollectedFlowers,
SectionType::Other(v) => SectionType::Other(v.into_static()),
}
}
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase")]
pub struct SectionGetRecordOutput<S: BosStr = DefaultStr> {
#[serde(skip_serializing_if = "Option::is_none")]
pub cid: Option<Cid<S>>,
pub uri: AtUri<S>,
pub value: Section<S>,
}
impl<S: BosStr> Section<S> {
pub fn uri(uri: S) -> Result<RecordUri<S, SectionRecord>, UriError> {
RecordUri::try_from_uri(AtUri::new(uri)?)
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct SectionRecord;
impl XrpcResp for SectionRecord {
const NSID: &'static str = "coop.hypha.spores.site.section";
const ENCODING: &'static str = "application/json";
type Output<S: BosStr> = SectionGetRecordOutput<S>;
type Err = RecordError;
}
impl<S: BosStr> From<SectionGetRecordOutput<S>> for Section<S> {
fn from(output: SectionGetRecordOutput<S>) -> Self {
output.value
}
}
impl<S: BosStr> Collection for Section<S> {
const NSID: &'static str = "coop.hypha.spores.site.section";
type Record = SectionRecord;
}
impl Collection for SectionRecord {
const NSID: &'static str = "coop.hypha.spores.site.section";
type Record = SectionRecord;
}
impl<S: BosStr> LexiconSchema for Section<S> {
fn nsid() -> &'static str {
"coop.hypha.spores.site.section"
}
fn def_name() -> &'static str {
"main"
}
fn lexicon_doc() -> LexiconDoc<'static> {
lexicon_doc_coop_hypha_spores_site_section()
}
fn validate(&self) -> Result<(), ConstraintError> {
if let Some(ref value) = self.content {
#[allow(unused_comparisons)]
if <str>::len(value.as_ref()) > 500000usize {
return Err(ConstraintError::MaxLength {
path: ValidationPath::from_field("content"),
max: 500000usize,
actual: <str>::len(value.as_ref()),
});
}
}
if let Some(ref value) = self.content {
{
let count = UnicodeSegmentation::graphemes(value.as_ref(), true).count();
if count > 50000usize {
return Err(ConstraintError::MaxGraphemes {
path: ValidationPath::from_field("content"),
max: 50000usize,
actual: count,
});
}
}
}
if let Some(ref value) = self.limit {
if *value > 100i64 {
return Err(ConstraintError::Maximum {
path: ValidationPath::from_field("limit"),
max: 100i64,
actual: *value,
});
}
}
if let Some(ref value) = self.limit {
if *value < 1i64 {
return Err(ConstraintError::Minimum {
path: ValidationPath::from_field("limit"),
min: 1i64,
actual: *value,
});
}
}
if let Some(ref value) = self.title {
#[allow(unused_comparisons)]
if <str>::len(value.as_ref()) > 2000usize {
return Err(ConstraintError::MaxLength {
path: ValidationPath::from_field("title"),
max: 2000usize,
actual: <str>::len(value.as_ref()),
});
}
}
if let Some(ref value) = self.title {
{
let count = UnicodeSegmentation::graphemes(value.as_ref(), true).count();
if count > 200usize {
return Err(ConstraintError::MaxGraphemes {
path: ValidationPath::from_field("title"),
max: 200usize,
actual: count,
});
}
}
}
Ok(())
}
}
pub mod section_state {
pub use crate::builder_types::{IsSet, IsUnset, Set, Unset};
#[allow(unused)]
use ::core::marker::PhantomData;
mod sealed {
pub trait Sealed {}
}
pub trait State: sealed::Sealed {
type Type;
}
pub struct Empty(());
impl sealed::Sealed for Empty {}
impl State for Empty {
type Type = Unset;
}
pub struct SetType<St: State = Empty>(PhantomData<fn() -> St>);
impl<St: State> sealed::Sealed for SetType<St> {}
impl<St: State> State for SetType<St> {
type Type = Set<members::r#type>;
}
#[allow(non_camel_case_types)]
pub mod members {
pub struct r#type(());
}
}
pub struct SectionBuilder<S: BosStr, St: section_state::State> {
_state: PhantomData<fn() -> St>,
_fields: (
Option<S>,
Option<S>,
Option<SectionFormat<S>>,
Option<bool>,
Option<SectionLayout<S>>,
Option<i64>,
Option<Vec<AtUri<S>>>,
Option<AtUri<S>>,
Option<S>,
Option<S>,
Option<SectionType<S>>,
),
_type: PhantomData<fn() -> S>,
}
impl<S: BosStr> Section<S> {
pub fn new() -> SectionBuilder<S, section_state::Empty> {
SectionBuilder::new()
}
}
impl<S: BosStr> SectionBuilder<S, section_state::Empty> {
pub fn new() -> Self {
SectionBuilder {
_state: PhantomData,
_fields: (
None, None, None, None, None, None, None, None, None, None, None,
),
_type: PhantomData,
}
}
}
impl<S: BosStr, St: section_state::State> SectionBuilder<S, St> {
pub fn collection(mut self, value: impl Into<Option<S>>) -> Self {
self._fields.0 = value.into();
self
}
pub fn maybe_collection(mut self, value: Option<S>) -> Self {
self._fields.0 = value;
self
}
}
impl<S: BosStr, St: section_state::State> SectionBuilder<S, St> {
pub fn content(mut self, value: impl Into<Option<S>>) -> Self {
self._fields.1 = value.into();
self
}
pub fn maybe_content(mut self, value: Option<S>) -> Self {
self._fields.1 = value;
self
}
}
impl<S: BosStr, St: section_state::State> SectionBuilder<S, St> {
pub fn format(mut self, value: impl Into<Option<SectionFormat<S>>>) -> Self {
self._fields.2 = value.into();
self
}
pub fn maybe_format(mut self, value: Option<SectionFormat<S>>) -> Self {
self._fields.2 = value;
self
}
}
impl<S: BosStr, St: section_state::State> SectionBuilder<S, St> {
pub fn hide_header(mut self, value: impl Into<Option<bool>>) -> Self {
self._fields.3 = value.into();
self
}
pub fn maybe_hide_header(mut self, value: Option<bool>) -> Self {
self._fields.3 = value;
self
}
}
impl<S: BosStr, St: section_state::State> SectionBuilder<S, St> {
pub fn layout(mut self, value: impl Into<Option<SectionLayout<S>>>) -> Self {
self._fields.4 = value.into();
self
}
pub fn maybe_layout(mut self, value: Option<SectionLayout<S>>) -> Self {
self._fields.4 = value;
self
}
}
impl<S: BosStr, St: section_state::State> SectionBuilder<S, St> {
pub fn limit(mut self, value: impl Into<Option<i64>>) -> Self {
self._fields.5 = value.into();
self
}
pub fn maybe_limit(mut self, value: Option<i64>) -> Self {
self._fields.5 = value;
self
}
}
impl<S: BosStr, St: section_state::State> SectionBuilder<S, St> {
pub fn records(mut self, value: impl Into<Option<Vec<AtUri<S>>>>) -> Self {
self._fields.6 = value.into();
self
}
pub fn maybe_records(mut self, value: Option<Vec<AtUri<S>>>) -> Self {
self._fields.6 = value;
self
}
}
impl<S: BosStr, St: section_state::State> SectionBuilder<S, St> {
pub fn r#ref(mut self, value: impl Into<Option<AtUri<S>>>) -> Self {
self._fields.7 = value.into();
self
}
pub fn maybe_ref(mut self, value: Option<AtUri<S>>) -> Self {
self._fields.7 = value;
self
}
}
impl<S: BosStr, St: section_state::State> SectionBuilder<S, St> {
pub fn rkey(mut self, value: impl Into<Option<S>>) -> Self {
self._fields.8 = value.into();
self
}
pub fn maybe_rkey(mut self, value: Option<S>) -> Self {
self._fields.8 = value;
self
}
}
impl<S: BosStr, St: section_state::State> SectionBuilder<S, St> {
pub fn title(mut self, value: impl Into<Option<S>>) -> Self {
self._fields.9 = value.into();
self
}
pub fn maybe_title(mut self, value: Option<S>) -> Self {
self._fields.9 = value;
self
}
}
impl<S: BosStr, St> SectionBuilder<S, St>
where
St: section_state::State,
St::Type: section_state::IsUnset,
{
pub fn r#type(
mut self,
value: impl Into<SectionType<S>>,
) -> SectionBuilder<S, section_state::SetType<St>> {
self._fields.10 = Option::Some(value.into());
SectionBuilder {
_state: PhantomData,
_fields: self._fields,
_type: PhantomData,
}
}
}
impl<S: BosStr, St> SectionBuilder<S, St>
where
St: section_state::State,
St::Type: section_state::IsSet,
{
pub fn build(self) -> Section<S> {
Section {
collection: self._fields.0,
content: self._fields.1,
format: self._fields.2,
hide_header: self._fields.3,
layout: self._fields.4,
limit: self._fields.5,
records: self._fields.6,
r#ref: self._fields.7,
rkey: self._fields.8,
title: self._fields.9,
r#type: self._fields.10.unwrap(),
extra_data: Default::default(),
}
}
pub fn build_with_data(self, extra_data: BTreeMap<SmolStr, Data<S>>) -> Section<S> {
Section {
collection: self._fields.0,
content: self._fields.1,
format: self._fields.2,
hide_header: self._fields.3,
layout: self._fields.4,
limit: self._fields.5,
records: self._fields.6,
r#ref: self._fields.7,
rkey: self._fields.8,
title: self._fields.9,
r#type: self._fields.10.unwrap(),
extra_data: Some(extra_data),
}
}
}
fn lexicon_doc_coop_hypha_spores_site_section() -> LexiconDoc<'static> {
use alloc::collections::BTreeMap;
#[allow(unused_imports)]
use jacquard_common::{CowStr, deps::smol_str::SmolStr, types::blob::MimeType};
use jacquard_lexicon::lexicon::*;
LexiconDoc {
lexicon: Lexicon::Lexicon1,
id: CowStr::new_static("coop.hypha.spores.site.section"),
defs: {
let mut map = BTreeMap::new();
map.insert(
SmolStr::new_static("main"),
LexUserType::Record(LexRecord {
description: Some(
CowStr::new_static(
"A single site section for spores.garden. Each section is a record in a collection.",
),
),
key: Some(CowStr::new_static("tid")),
record: LexRecordRecord::Object(LexObject {
required: Some(vec![SmolStr::new_static("type")]),
properties: {
#[allow(unused_mut)]
let mut map = BTreeMap::new();
map.insert(
SmolStr::new_static("collection"),
LexObjectProperty::String(LexString {
description: Some(
CowStr::new_static(
"Collection NSID to display (for type=collection)",
),
),
..Default::default()
}),
);
map.insert(
SmolStr::new_static("content"),
LexObjectProperty::String(LexString {
description: Some(
CowStr::new_static("Content for block sections"),
),
max_length: Some(500000usize),
max_graphemes: Some(50000usize),
..Default::default()
}),
);
map.insert(
SmolStr::new_static("format"),
LexObjectProperty::String(LexString {
description: Some(CowStr::new_static("Content format")),
..Default::default()
}),
);
map.insert(
SmolStr::new_static("hideHeader"),
LexObjectProperty::Boolean(LexBoolean {
..Default::default()
}),
);
map.insert(
SmolStr::new_static("layout"),
LexObjectProperty::String(LexString {
description: Some(
CowStr::new_static("Layout to use for rendering"),
),
..Default::default()
}),
);
map.insert(
SmolStr::new_static("limit"),
LexObjectProperty::Integer(LexInteger {
minimum: Some(1i64),
maximum: Some(100i64),
..Default::default()
}),
);
map.insert(
SmolStr::new_static("records"),
LexObjectProperty::Array(LexArray {
description: Some(
CowStr::new_static(
"Specific record URIs to display (for type=records)",
),
),
items: LexArrayItem::String(LexString {
format: Some(LexStringFormat::AtUri),
..Default::default()
}),
..Default::default()
}),
);
map.insert(
SmolStr::new_static("ref"),
LexObjectProperty::String(LexString {
description: Some(
CowStr::new_static(
"AT-URI of the referenced record (for content/profile sections)",
),
),
format: Some(LexStringFormat::AtUri),
..Default::default()
}),
);
map.insert(
SmolStr::new_static("rkey"),
LexObjectProperty::String(LexString {
description: Some(
CowStr::new_static(
"Record key within the collection to display (e.g. 'self' for profile)",
),
),
..Default::default()
}),
);
map.insert(
SmolStr::new_static("title"),
LexObjectProperty::String(LexString {
description: Some(CowStr::new_static("Section title")),
max_length: Some(2000usize),
max_graphemes: Some(200usize),
..Default::default()
}),
);
map.insert(
SmolStr::new_static("type"),
LexObjectProperty::String(LexString {
description: Some(CowStr::new_static("Section type")),
..Default::default()
}),
);
map
},
..Default::default()
}),
..Default::default()
}),
);
map
},
..Default::default()
}
}