#[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::blob::BlobRef;
use jacquard_common::types::collection::{Collection, RecordError};
use jacquard_common::types::string::{AtUri, Cid, Datetime};
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;
use crate::space_litenote::note;
#[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",
bound(deserialize = "S: Deserialize<'de> + BosStr")
)]
pub struct Image<S: BosStr = DefaultStr> {
#[serde(skip_serializing_if = "Option::is_none")]
pub alt: Option<S>,
pub image: BlobRef<S>,
#[serde(flatten, default, skip_serializing_if = "Option::is_none")]
pub extra_data: Option<BTreeMap<SmolStr, Data<S>>>,
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(
rename_all = "camelCase",
rename = "space.litenote.note",
tag = "$type",
bound(deserialize = "S: Deserialize<'de> + BosStr")
)]
pub struct Note<S: BosStr = DefaultStr> {
pub content: S,
#[serde(skip_serializing_if = "Option::is_none")]
pub created_at: Option<Datetime>,
#[serde(skip_serializing_if = "Option::is_none")]
pub font_family: Option<S>,
#[serde(skip_serializing_if = "Option::is_none")]
pub font_size: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub images: Option<Vec<note::Image<S>>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub published_at: Option<Datetime>,
#[serde(skip_serializing_if = "Option::is_none")]
pub theme: Option<NoteTheme<S>>,
pub title: 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 NoteTheme<S: BosStr = DefaultStr> {
Light,
Dark,
Other(S),
}
impl<S: BosStr> NoteTheme<S> {
pub fn as_str(&self) -> &str {
match self {
Self::Light => "light",
Self::Dark => "dark",
Self::Other(s) => s.as_ref(),
}
}
pub fn from_value(s: S) -> Self {
match s.as_ref() {
"light" => Self::Light,
"dark" => Self::Dark,
_ => Self::Other(s),
}
}
}
impl<S: BosStr> core::fmt::Display for NoteTheme<S> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", self.as_str())
}
}
impl<S: BosStr> AsRef<str> for NoteTheme<S> {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl<S: BosStr> Serialize for NoteTheme<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 NoteTheme<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 NoteTheme<S> {
fn default() -> Self {
Self::Other(Default::default())
}
}
impl<S: BosStr> jacquard_common::IntoStatic for NoteTheme<S>
where
S: BosStr + jacquard_common::IntoStatic,
S::Output: BosStr,
{
type Output = NoteTheme<S::Output>;
fn into_static(self) -> Self::Output {
match self {
NoteTheme::Light => NoteTheme::Light,
NoteTheme::Dark => NoteTheme::Dark,
NoteTheme::Other(v) => NoteTheme::Other(v.into_static()),
}
}
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase")]
pub struct NoteGetRecordOutput<S: BosStr = DefaultStr> {
#[serde(skip_serializing_if = "Option::is_none")]
pub cid: Option<Cid<S>>,
pub uri: AtUri<S>,
pub value: Note<S>,
}
impl<S: BosStr> Note<S> {
pub fn uri(uri: S) -> Result<RecordUri<S, NoteRecord>, UriError> {
RecordUri::try_from_uri(AtUri::new(uri)?)
}
}
impl<S: BosStr> LexiconSchema for Image<S> {
fn nsid() -> &'static str {
"space.litenote.note"
}
fn def_name() -> &'static str {
"image"
}
fn lexicon_doc() -> LexiconDoc<'static> {
lexicon_doc_space_litenote_note()
}
fn validate(&self) -> Result<(), ConstraintError> {
if let Some(ref value) = self.alt {
#[allow(unused_comparisons)]
if <str>::len(value.as_ref()) > 2000usize {
return Err(ConstraintError::MaxLength {
path: ValidationPath::from_field("alt"),
max: 2000usize,
actual: <str>::len(value.as_ref()),
});
}
}
{
let value = &self.image;
{
let size = value.blob().size;
if size > 2000000usize {
return Err(ConstraintError::BlobTooLarge {
path: ValidationPath::from_field("image"),
max: 2000000usize,
actual: size,
});
}
}
}
{
let value = &self.image;
{
let mime = value.blob().mime_type.as_str();
let accepted: &[&str] = &["image/*"];
let matched = accepted.iter().any(|pattern| {
if *pattern == "*/*" {
true
} else if pattern.ends_with("/*") {
let prefix = &pattern[..pattern.len() - 2];
mime.starts_with(prefix) && mime.as_bytes().get(prefix.len()) == Some(&b'/')
} else {
mime == *pattern
}
});
if !matched {
return Err(ConstraintError::BlobMimeTypeNotAccepted {
path: ValidationPath::from_field("image"),
accepted: vec!["image/*".to_string()],
actual: mime.to_string(),
});
}
}
}
Ok(())
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct NoteRecord;
impl XrpcResp for NoteRecord {
const NSID: &'static str = "space.litenote.note";
const ENCODING: &'static str = "application/json";
type Output<S: BosStr> = NoteGetRecordOutput<S>;
type Err = RecordError;
}
impl<S: BosStr> From<NoteGetRecordOutput<S>> for Note<S> {
fn from(output: NoteGetRecordOutput<S>) -> Self {
output.value
}
}
impl<S: BosStr> Collection for Note<S> {
const NSID: &'static str = "space.litenote.note";
type Record = NoteRecord;
}
impl Collection for NoteRecord {
const NSID: &'static str = "space.litenote.note";
type Record = NoteRecord;
}
impl<S: BosStr> LexiconSchema for Note<S> {
fn nsid() -> &'static str {
"space.litenote.note"
}
fn def_name() -> &'static str {
"main"
}
fn lexicon_doc() -> LexiconDoc<'static> {
lexicon_doc_space_litenote_note()
}
fn validate(&self) -> Result<(), ConstraintError> {
{
let value = &self.content;
#[allow(unused_comparisons)]
if <str>::len(value.as_ref()) > 10000usize {
return Err(ConstraintError::MaxLength {
path: ValidationPath::from_field("content"),
max: 10000usize,
actual: <str>::len(value.as_ref()),
});
}
}
if let Some(ref value) = self.font_family {
#[allow(unused_comparisons)]
if <str>::len(value.as_ref()) > 200usize {
return Err(ConstraintError::MaxLength {
path: ValidationPath::from_field("font_family"),
max: 200usize,
actual: <str>::len(value.as_ref()),
});
}
}
if let Some(ref value) = self.images {
#[allow(unused_comparisons)]
if value.len() > 20usize {
return Err(ConstraintError::MaxLength {
path: ValidationPath::from_field("images"),
max: 20usize,
actual: value.len(),
});
}
}
{
let value = &self.title;
#[allow(unused_comparisons)]
if <str>::len(value.as_ref()) > 1000usize {
return Err(ConstraintError::MaxLength {
path: ValidationPath::from_field("title"),
max: 1000usize,
actual: <str>::len(value.as_ref()),
});
}
}
Ok(())
}
}
pub mod image_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 Image;
}
pub struct Empty(());
impl sealed::Sealed for Empty {}
impl State for Empty {
type Image = Unset;
}
pub struct SetImage<St: State = Empty>(PhantomData<fn() -> St>);
impl<St: State> sealed::Sealed for SetImage<St> {}
impl<St: State> State for SetImage<St> {
type Image = Set<members::image>;
}
#[allow(non_camel_case_types)]
pub mod members {
pub struct image(());
}
}
pub struct ImageBuilder<S: BosStr, St: image_state::State> {
_state: PhantomData<fn() -> St>,
_fields: (Option<S>, Option<BlobRef<S>>),
_type: PhantomData<fn() -> S>,
}
impl<S: BosStr> Image<S> {
pub fn new() -> ImageBuilder<S, image_state::Empty> {
ImageBuilder::new()
}
}
impl<S: BosStr> ImageBuilder<S, image_state::Empty> {
pub fn new() -> Self {
ImageBuilder {
_state: PhantomData,
_fields: (None, None),
_type: PhantomData,
}
}
}
impl<S: BosStr, St: image_state::State> ImageBuilder<S, St> {
pub fn alt(mut self, value: impl Into<Option<S>>) -> Self {
self._fields.0 = value.into();
self
}
pub fn maybe_alt(mut self, value: Option<S>) -> Self {
self._fields.0 = value;
self
}
}
impl<S: BosStr, St> ImageBuilder<S, St>
where
St: image_state::State,
St::Image: image_state::IsUnset,
{
pub fn image(
mut self,
value: impl Into<BlobRef<S>>,
) -> ImageBuilder<S, image_state::SetImage<St>> {
self._fields.1 = Option::Some(value.into());
ImageBuilder {
_state: PhantomData,
_fields: self._fields,
_type: PhantomData,
}
}
}
impl<S: BosStr, St> ImageBuilder<S, St>
where
St: image_state::State,
St::Image: image_state::IsSet,
{
pub fn build(self) -> Image<S> {
Image {
alt: self._fields.0,
image: self._fields.1.unwrap(),
extra_data: Default::default(),
}
}
pub fn build_with_data(self, extra_data: BTreeMap<SmolStr, Data<S>>) -> Image<S> {
Image {
alt: self._fields.0,
image: self._fields.1.unwrap(),
extra_data: Some(extra_data),
}
}
}
fn lexicon_doc_space_litenote_note() -> 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("space.litenote.note"),
defs: {
let mut map = BTreeMap::new();
map.insert(
SmolStr::new_static("image"),
LexUserType::Object(LexObject {
required: Some(vec![SmolStr::new_static("image")]),
properties: {
#[allow(unused_mut)]
let mut map = BTreeMap::new();
map.insert(
SmolStr::new_static("alt"),
LexObjectProperty::String(LexString {
description: Some(CowStr::new_static("Alt text for the image.")),
max_length: Some(2000usize),
..Default::default()
}),
);
map.insert(
SmolStr::new_static("image"),
LexObjectProperty::Blob(LexBlob {
..Default::default()
}),
);
map
},
..Default::default()
}),
);
map.insert(
SmolStr::new_static("main"),
LexUserType::Record(LexRecord {
description: Some(
CowStr::new_static(
"A markdown blog post with LaTeX, GitHub notes, Mermaid, YouTube and Bluesky extensions.",
),
),
key: Some(CowStr::new_static("tid")),
record: LexRecordRecord::Object(LexObject {
required: Some(
vec![
SmolStr::new_static("title"), SmolStr::new_static("content")
],
),
properties: {
#[allow(unused_mut)]
let mut map = BTreeMap::new();
map.insert(
SmolStr::new_static("content"),
LexObjectProperty::String(LexString {
description: Some(
CowStr::new_static(
"Markdown content. Local image paths are replaced with blob CIDs at publish time.",
),
),
max_length: Some(10000usize),
..Default::default()
}),
);
map.insert(
SmolStr::new_static("createdAt"),
LexObjectProperty::String(LexString {
format: Some(LexStringFormat::Datetime),
..Default::default()
}),
);
map.insert(
SmolStr::new_static("fontFamily"),
LexObjectProperty::String(LexString {
description: Some(
CowStr::new_static(
"Font family name available from Coollabs.",
),
),
max_length: Some(200usize),
..Default::default()
}),
);
map.insert(
SmolStr::new_static("fontSize"),
LexObjectProperty::Integer(LexInteger {
..Default::default()
}),
);
map.insert(
SmolStr::new_static("images"),
LexObjectProperty::Array(LexArray {
description: Some(
CowStr::new_static(
"Blob references for images embedded in the markdown content.",
),
),
items: LexArrayItem::Ref(LexRef {
r#ref: CowStr::new_static("#image"),
..Default::default()
}),
max_length: Some(20usize),
..Default::default()
}),
);
map.insert(
SmolStr::new_static("publishedAt"),
LexObjectProperty::String(LexString {
format: Some(LexStringFormat::Datetime),
..Default::default()
}),
);
map.insert(
SmolStr::new_static("theme"),
LexObjectProperty::String(LexString {
description: Some(
CowStr::new_static("Display theme for the note."),
),
..Default::default()
}),
);
map.insert(
SmolStr::new_static("title"),
LexObjectProperty::String(LexString {
max_length: Some(1000usize),
..Default::default()
}),
);
map
},
..Default::default()
}),
..Default::default()
}),
);
map
},
..Default::default()
}
}
pub mod note_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 Content;
type Title;
}
pub struct Empty(());
impl sealed::Sealed for Empty {}
impl State for Empty {
type Content = Unset;
type Title = Unset;
}
pub struct SetContent<St: State = Empty>(PhantomData<fn() -> St>);
impl<St: State> sealed::Sealed for SetContent<St> {}
impl<St: State> State for SetContent<St> {
type Content = Set<members::content>;
type Title = St::Title;
}
pub struct SetTitle<St: State = Empty>(PhantomData<fn() -> St>);
impl<St: State> sealed::Sealed for SetTitle<St> {}
impl<St: State> State for SetTitle<St> {
type Content = St::Content;
type Title = Set<members::title>;
}
#[allow(non_camel_case_types)]
pub mod members {
pub struct content(());
pub struct title(());
}
}
pub struct NoteBuilder<S: BosStr, St: note_state::State> {
_state: PhantomData<fn() -> St>,
_fields: (
Option<S>,
Option<Datetime>,
Option<S>,
Option<i64>,
Option<Vec<note::Image<S>>>,
Option<Datetime>,
Option<NoteTheme<S>>,
Option<S>,
),
_type: PhantomData<fn() -> S>,
}
impl<S: BosStr> Note<S> {
pub fn new() -> NoteBuilder<S, note_state::Empty> {
NoteBuilder::new()
}
}
impl<S: BosStr> NoteBuilder<S, note_state::Empty> {
pub fn new() -> Self {
NoteBuilder {
_state: PhantomData,
_fields: (None, None, None, None, None, None, None, None),
_type: PhantomData,
}
}
}
impl<S: BosStr, St> NoteBuilder<S, St>
where
St: note_state::State,
St::Content: note_state::IsUnset,
{
pub fn content(mut self, value: impl Into<S>) -> NoteBuilder<S, note_state::SetContent<St>> {
self._fields.0 = Option::Some(value.into());
NoteBuilder {
_state: PhantomData,
_fields: self._fields,
_type: PhantomData,
}
}
}
impl<S: BosStr, St: note_state::State> NoteBuilder<S, St> {
pub fn created_at(mut self, value: impl Into<Option<Datetime>>) -> Self {
self._fields.1 = value.into();
self
}
pub fn maybe_created_at(mut self, value: Option<Datetime>) -> Self {
self._fields.1 = value;
self
}
}
impl<S: BosStr, St: note_state::State> NoteBuilder<S, St> {
pub fn font_family(mut self, value: impl Into<Option<S>>) -> Self {
self._fields.2 = value.into();
self
}
pub fn maybe_font_family(mut self, value: Option<S>) -> Self {
self._fields.2 = value;
self
}
}
impl<S: BosStr, St: note_state::State> NoteBuilder<S, St> {
pub fn font_size(mut self, value: impl Into<Option<i64>>) -> Self {
self._fields.3 = value.into();
self
}
pub fn maybe_font_size(mut self, value: Option<i64>) -> Self {
self._fields.3 = value;
self
}
}
impl<S: BosStr, St: note_state::State> NoteBuilder<S, St> {
pub fn images(mut self, value: impl Into<Option<Vec<note::Image<S>>>>) -> Self {
self._fields.4 = value.into();
self
}
pub fn maybe_images(mut self, value: Option<Vec<note::Image<S>>>) -> Self {
self._fields.4 = value;
self
}
}
impl<S: BosStr, St: note_state::State> NoteBuilder<S, St> {
pub fn published_at(mut self, value: impl Into<Option<Datetime>>) -> Self {
self._fields.5 = value.into();
self
}
pub fn maybe_published_at(mut self, value: Option<Datetime>) -> Self {
self._fields.5 = value;
self
}
}
impl<S: BosStr, St: note_state::State> NoteBuilder<S, St> {
pub fn theme(mut self, value: impl Into<Option<NoteTheme<S>>>) -> Self {
self._fields.6 = value.into();
self
}
pub fn maybe_theme(mut self, value: Option<NoteTheme<S>>) -> Self {
self._fields.6 = value;
self
}
}
impl<S: BosStr, St> NoteBuilder<S, St>
where
St: note_state::State,
St::Title: note_state::IsUnset,
{
pub fn title(mut self, value: impl Into<S>) -> NoteBuilder<S, note_state::SetTitle<St>> {
self._fields.7 = Option::Some(value.into());
NoteBuilder {
_state: PhantomData,
_fields: self._fields,
_type: PhantomData,
}
}
}
impl<S: BosStr, St> NoteBuilder<S, St>
where
St: note_state::State,
St::Content: note_state::IsSet,
St::Title: note_state::IsSet,
{
pub fn build(self) -> Note<S> {
Note {
content: self._fields.0.unwrap(),
created_at: self._fields.1,
font_family: self._fields.2,
font_size: self._fields.3,
images: self._fields.4,
published_at: self._fields.5,
theme: self._fields.6,
title: self._fields.7.unwrap(),
extra_data: Default::default(),
}
}
pub fn build_with_data(self, extra_data: BTreeMap<SmolStr, Data<S>>) -> Note<S> {
Note {
content: self._fields.0.unwrap(),
created_at: self._fields.1,
font_family: self._fields.2,
font_size: self._fields.3,
images: self._fields.4,
published_at: self._fields.5,
theme: self._fields.6,
title: self._fields.7.unwrap(),
extra_data: Some(extra_data),
}
}
}