#[allow(unused_imports)]
use alloc::collections::BTreeMap;
#[allow(unused_imports)]
use core::marker::PhantomData;
use jacquard_common::{CowStr, BosStr, DefaultStr, FromStaticStr};
use jacquard_common::deps::bytes::Bytes;
#[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::{Did, 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;
#[allow(unused_imports)]
use jacquard_lexicon::validation::{ConstraintError, ValidationPath};
use serde::{Serialize, Deserialize};
use crate::org_passingreads::AspectRatio;
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(
rename_all = "camelCase",
rename = "org.passingreads.book.registration",
tag = "$type",
bound(deserialize = "S: Deserialize<'de> + BosStr")
)]
pub struct Registration<S: BosStr = DefaultStr> {
#[serde(skip_serializing_if = "Option::is_none")]
pub aspect_ratio: Option<AspectRatio<S>>,
pub authors: Vec<S>,
pub book_id: S,
#[serde(with = "jacquard_common::serde_bytes_helper")]
pub book_pub: Bytes,
#[serde(with = "jacquard_common::serde_bytes_helper")]
pub book_sig: Bytes,
#[serde(skip_serializing_if = "Option::is_none")]
pub cover: Option<BlobRef<S>>,
pub did: Did<S>,
pub occurred_at: Datetime,
pub publication_id: S,
pub title: 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")]
pub struct RegistrationGetRecordOutput<S: BosStr = DefaultStr> {
#[serde(skip_serializing_if = "Option::is_none")]
pub cid: Option<Cid<S>>,
pub uri: AtUri<S>,
pub value: Registration<S>,
}
impl<S: BosStr> Registration<S> {
pub fn uri(uri: S) -> Result<RecordUri<S, RegistrationRecord>, UriError> {
RecordUri::try_from_uri(AtUri::new(uri)?)
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct RegistrationRecord;
impl XrpcResp for RegistrationRecord {
const NSID: &'static str = "org.passingreads.book.registration";
const ENCODING: &'static str = "application/json";
type Output<S: BosStr> = RegistrationGetRecordOutput<S>;
type Err = RecordError;
}
impl<S: BosStr> From<RegistrationGetRecordOutput<S>> for Registration<S> {
fn from(output: RegistrationGetRecordOutput<S>) -> Self {
output.value
}
}
impl<S: BosStr> Collection for Registration<S> {
const NSID: &'static str = "org.passingreads.book.registration";
type Record = RegistrationRecord;
}
impl Collection for RegistrationRecord {
const NSID: &'static str = "org.passingreads.book.registration";
type Record = RegistrationRecord;
}
impl<S: BosStr> LexiconSchema for Registration<S> {
fn nsid() -> &'static str {
"org.passingreads.book.registration"
}
fn def_name() -> &'static str {
"main"
}
fn lexicon_doc() -> LexiconDoc<'static> {
lexicon_doc_org_passingreads_book_registration()
}
fn validate(&self) -> Result<(), ConstraintError> {
if let Some(ref value) = self.cover {
{
let size = value.blob().size;
if size > 1000000usize {
return Err(ConstraintError::BlobTooLarge {
path: ValidationPath::from_field("cover"),
max: 1000000usize,
actual: size,
});
}
}
}
if let Some(ref value) = self.cover {
{
let mime = value.blob().mime_type.as_str();
let accepted: &[&str] = &["image/png", "image/jpeg"];
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("cover"),
accepted: vec![
"image/png".to_string(), "image/jpeg".to_string()
],
actual: mime.to_string(),
});
}
}
}
{
let value = &self.title;
#[allow(unused_comparisons)]
if <str>::len(value.as_ref()) > 512usize {
return Err(ConstraintError::MaxLength {
path: ValidationPath::from_field("title"),
max: 512usize,
actual: <str>::len(value.as_ref()),
});
}
}
{
let value = &self.title;
#[allow(unused_comparisons)]
if <str>::len(value.as_ref()) < 1usize {
return Err(ConstraintError::MinLength {
path: ValidationPath::from_field("title"),
min: 1usize,
actual: <str>::len(value.as_ref()),
});
}
}
Ok(())
}
}
pub mod registration_state {
pub use crate::builder_types::{Set, Unset, IsSet, IsUnset};
#[allow(unused)]
use ::core::marker::PhantomData;
mod sealed {
pub trait Sealed {}
}
pub trait State: sealed::Sealed {
type BookId;
type BookSig;
type OccurredAt;
type Title;
type Authors;
type BookPub;
type PublicationId;
type Did;
}
pub struct Empty(());
impl sealed::Sealed for Empty {}
impl State for Empty {
type BookId = Unset;
type BookSig = Unset;
type OccurredAt = Unset;
type Title = Unset;
type Authors = Unset;
type BookPub = Unset;
type PublicationId = Unset;
type Did = Unset;
}
pub struct SetBookId<St: State = Empty>(PhantomData<fn() -> St>);
impl<St: State> sealed::Sealed for SetBookId<St> {}
impl<St: State> State for SetBookId<St> {
type BookId = Set<members::book_id>;
type BookSig = St::BookSig;
type OccurredAt = St::OccurredAt;
type Title = St::Title;
type Authors = St::Authors;
type BookPub = St::BookPub;
type PublicationId = St::PublicationId;
type Did = St::Did;
}
pub struct SetBookSig<St: State = Empty>(PhantomData<fn() -> St>);
impl<St: State> sealed::Sealed for SetBookSig<St> {}
impl<St: State> State for SetBookSig<St> {
type BookId = St::BookId;
type BookSig = Set<members::book_sig>;
type OccurredAt = St::OccurredAt;
type Title = St::Title;
type Authors = St::Authors;
type BookPub = St::BookPub;
type PublicationId = St::PublicationId;
type Did = St::Did;
}
pub struct SetOccurredAt<St: State = Empty>(PhantomData<fn() -> St>);
impl<St: State> sealed::Sealed for SetOccurredAt<St> {}
impl<St: State> State for SetOccurredAt<St> {
type BookId = St::BookId;
type BookSig = St::BookSig;
type OccurredAt = Set<members::occurred_at>;
type Title = St::Title;
type Authors = St::Authors;
type BookPub = St::BookPub;
type PublicationId = St::PublicationId;
type Did = St::Did;
}
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 BookId = St::BookId;
type BookSig = St::BookSig;
type OccurredAt = St::OccurredAt;
type Title = Set<members::title>;
type Authors = St::Authors;
type BookPub = St::BookPub;
type PublicationId = St::PublicationId;
type Did = St::Did;
}
pub struct SetAuthors<St: State = Empty>(PhantomData<fn() -> St>);
impl<St: State> sealed::Sealed for SetAuthors<St> {}
impl<St: State> State for SetAuthors<St> {
type BookId = St::BookId;
type BookSig = St::BookSig;
type OccurredAt = St::OccurredAt;
type Title = St::Title;
type Authors = Set<members::authors>;
type BookPub = St::BookPub;
type PublicationId = St::PublicationId;
type Did = St::Did;
}
pub struct SetBookPub<St: State = Empty>(PhantomData<fn() -> St>);
impl<St: State> sealed::Sealed for SetBookPub<St> {}
impl<St: State> State for SetBookPub<St> {
type BookId = St::BookId;
type BookSig = St::BookSig;
type OccurredAt = St::OccurredAt;
type Title = St::Title;
type Authors = St::Authors;
type BookPub = Set<members::book_pub>;
type PublicationId = St::PublicationId;
type Did = St::Did;
}
pub struct SetPublicationId<St: State = Empty>(PhantomData<fn() -> St>);
impl<St: State> sealed::Sealed for SetPublicationId<St> {}
impl<St: State> State for SetPublicationId<St> {
type BookId = St::BookId;
type BookSig = St::BookSig;
type OccurredAt = St::OccurredAt;
type Title = St::Title;
type Authors = St::Authors;
type BookPub = St::BookPub;
type PublicationId = Set<members::publication_id>;
type Did = St::Did;
}
pub struct SetDid<St: State = Empty>(PhantomData<fn() -> St>);
impl<St: State> sealed::Sealed for SetDid<St> {}
impl<St: State> State for SetDid<St> {
type BookId = St::BookId;
type BookSig = St::BookSig;
type OccurredAt = St::OccurredAt;
type Title = St::Title;
type Authors = St::Authors;
type BookPub = St::BookPub;
type PublicationId = St::PublicationId;
type Did = Set<members::did>;
}
#[allow(non_camel_case_types)]
pub mod members {
pub struct book_id(());
pub struct book_sig(());
pub struct occurred_at(());
pub struct title(());
pub struct authors(());
pub struct book_pub(());
pub struct publication_id(());
pub struct did(());
}
}
pub struct RegistrationBuilder<S: BosStr, St: registration_state::State> {
_state: PhantomData<fn() -> St>,
_fields: (
Option<AspectRatio<S>>,
Option<Vec<S>>,
Option<S>,
Option<Bytes>,
Option<Bytes>,
Option<BlobRef<S>>,
Option<Did<S>>,
Option<Datetime>,
Option<S>,
Option<S>,
),
_type: PhantomData<fn() -> S>,
}
impl<S: BosStr> Registration<S> {
pub fn new() -> RegistrationBuilder<S, registration_state::Empty> {
RegistrationBuilder::new()
}
}
impl<S: BosStr> RegistrationBuilder<S, registration_state::Empty> {
pub fn new() -> Self {
RegistrationBuilder {
_state: PhantomData,
_fields: (None, None, None, None, None, None, None, None, None, None),
_type: PhantomData,
}
}
}
impl<S: BosStr, St: registration_state::State> RegistrationBuilder<S, St> {
pub fn aspect_ratio(mut self, value: impl Into<Option<AspectRatio<S>>>) -> Self {
self._fields.0 = value.into();
self
}
pub fn maybe_aspect_ratio(mut self, value: Option<AspectRatio<S>>) -> Self {
self._fields.0 = value;
self
}
}
impl<S: BosStr, St> RegistrationBuilder<S, St>
where
St: registration_state::State,
St::Authors: registration_state::IsUnset,
{
pub fn authors(
mut self,
value: impl Into<Vec<S>>,
) -> RegistrationBuilder<S, registration_state::SetAuthors<St>> {
self._fields.1 = Option::Some(value.into());
RegistrationBuilder {
_state: PhantomData,
_fields: self._fields,
_type: PhantomData,
}
}
}
impl<S: BosStr, St> RegistrationBuilder<S, St>
where
St: registration_state::State,
St::BookId: registration_state::IsUnset,
{
pub fn book_id(
mut self,
value: impl Into<S>,
) -> RegistrationBuilder<S, registration_state::SetBookId<St>> {
self._fields.2 = Option::Some(value.into());
RegistrationBuilder {
_state: PhantomData,
_fields: self._fields,
_type: PhantomData,
}
}
}
impl<S: BosStr, St> RegistrationBuilder<S, St>
where
St: registration_state::State,
St::BookPub: registration_state::IsUnset,
{
pub fn book_pub(
mut self,
value: impl Into<Bytes>,
) -> RegistrationBuilder<S, registration_state::SetBookPub<St>> {
self._fields.3 = Option::Some(value.into());
RegistrationBuilder {
_state: PhantomData,
_fields: self._fields,
_type: PhantomData,
}
}
}
impl<S: BosStr, St> RegistrationBuilder<S, St>
where
St: registration_state::State,
St::BookSig: registration_state::IsUnset,
{
pub fn book_sig(
mut self,
value: impl Into<Bytes>,
) -> RegistrationBuilder<S, registration_state::SetBookSig<St>> {
self._fields.4 = Option::Some(value.into());
RegistrationBuilder {
_state: PhantomData,
_fields: self._fields,
_type: PhantomData,
}
}
}
impl<S: BosStr, St: registration_state::State> RegistrationBuilder<S, St> {
pub fn cover(mut self, value: impl Into<Option<BlobRef<S>>>) -> Self {
self._fields.5 = value.into();
self
}
pub fn maybe_cover(mut self, value: Option<BlobRef<S>>) -> Self {
self._fields.5 = value;
self
}
}
impl<S: BosStr, St> RegistrationBuilder<S, St>
where
St: registration_state::State,
St::Did: registration_state::IsUnset,
{
pub fn did(
mut self,
value: impl Into<Did<S>>,
) -> RegistrationBuilder<S, registration_state::SetDid<St>> {
self._fields.6 = Option::Some(value.into());
RegistrationBuilder {
_state: PhantomData,
_fields: self._fields,
_type: PhantomData,
}
}
}
impl<S: BosStr, St> RegistrationBuilder<S, St>
where
St: registration_state::State,
St::OccurredAt: registration_state::IsUnset,
{
pub fn occurred_at(
mut self,
value: impl Into<Datetime>,
) -> RegistrationBuilder<S, registration_state::SetOccurredAt<St>> {
self._fields.7 = Option::Some(value.into());
RegistrationBuilder {
_state: PhantomData,
_fields: self._fields,
_type: PhantomData,
}
}
}
impl<S: BosStr, St> RegistrationBuilder<S, St>
where
St: registration_state::State,
St::PublicationId: registration_state::IsUnset,
{
pub fn publication_id(
mut self,
value: impl Into<S>,
) -> RegistrationBuilder<S, registration_state::SetPublicationId<St>> {
self._fields.8 = Option::Some(value.into());
RegistrationBuilder {
_state: PhantomData,
_fields: self._fields,
_type: PhantomData,
}
}
}
impl<S: BosStr, St> RegistrationBuilder<S, St>
where
St: registration_state::State,
St::Title: registration_state::IsUnset,
{
pub fn title(
mut self,
value: impl Into<S>,
) -> RegistrationBuilder<S, registration_state::SetTitle<St>> {
self._fields.9 = Option::Some(value.into());
RegistrationBuilder {
_state: PhantomData,
_fields: self._fields,
_type: PhantomData,
}
}
}
impl<S: BosStr, St> RegistrationBuilder<S, St>
where
St: registration_state::State,
St::BookId: registration_state::IsSet,
St::BookSig: registration_state::IsSet,
St::OccurredAt: registration_state::IsSet,
St::Title: registration_state::IsSet,
St::Authors: registration_state::IsSet,
St::BookPub: registration_state::IsSet,
St::PublicationId: registration_state::IsSet,
St::Did: registration_state::IsSet,
{
pub fn build(self) -> Registration<S> {
Registration {
aspect_ratio: self._fields.0,
authors: self._fields.1.unwrap(),
book_id: self._fields.2.unwrap(),
book_pub: self._fields.3.unwrap(),
book_sig: self._fields.4.unwrap(),
cover: self._fields.5,
did: self._fields.6.unwrap(),
occurred_at: self._fields.7.unwrap(),
publication_id: self._fields.8.unwrap(),
title: self._fields.9.unwrap(),
extra_data: Default::default(),
}
}
pub fn build_with_data(
self,
extra_data: BTreeMap<SmolStr, Data<S>>,
) -> Registration<S> {
Registration {
aspect_ratio: self._fields.0,
authors: self._fields.1.unwrap(),
book_id: self._fields.2.unwrap(),
book_pub: self._fields.3.unwrap(),
book_sig: self._fields.4.unwrap(),
cover: self._fields.5,
did: self._fields.6.unwrap(),
occurred_at: self._fields.7.unwrap(),
publication_id: self._fields.8.unwrap(),
title: self._fields.9.unwrap(),
extra_data: Some(extra_data),
}
}
}
fn lexicon_doc_org_passingreads_book_registration() -> LexiconDoc<'static> {
#[allow(unused_imports)]
use jacquard_common::{CowStr, deps::smol_str::SmolStr, types::blob::MimeType};
use jacquard_lexicon::lexicon::*;
use alloc::collections::BTreeMap;
LexiconDoc {
lexicon: Lexicon::Lexicon1,
id: CowStr::new_static("org.passingreads.book.registration"),
defs: {
let mut map = BTreeMap::new();
map.insert(
SmolStr::new_static("main"),
LexUserType::Record(LexRecord {
description: Some(
CowStr::new_static(
"A book that has been registered on PassingReads",
),
),
key: Some(CowStr::new_static("any")),
record: LexRecordRecord::Object(LexObject {
required: Some(
vec![
SmolStr::new_static("did"), SmolStr::new_static("bookId"),
SmolStr::new_static("title"),
SmolStr::new_static("authors"),
SmolStr::new_static("publicationId"),
SmolStr::new_static("occurredAt"),
SmolStr::new_static("bookPub"),
SmolStr::new_static("bookSig")
],
),
properties: {
#[allow(unused_mut)]
let mut map = BTreeMap::new();
map.insert(
SmolStr::new_static("aspectRatio"),
LexObjectProperty::Ref(LexRef {
r#ref: CowStr::new_static(
"org.passingreads.defs#aspectRatio",
),
..Default::default()
}),
);
map.insert(
SmolStr::new_static("authors"),
LexObjectProperty::Array(LexArray {
description: Some(
CowStr::new_static(
"Authors of this book, in order of credit. An empty array implies a book of anonymous or unknown authorship.",
),
),
items: LexArrayItem::String(LexString {
min_length: Some(1usize),
max_length: Some(512usize),
..Default::default()
}),
..Default::default()
}),
);
map.insert(
SmolStr::new_static("bookId"),
LexObjectProperty::String(LexString {
description: Some(
CowStr::new_static(
"The book's ID (as defined on its QR Code)",
),
),
..Default::default()
}),
);
map.insert(
SmolStr::new_static("bookPub"),
LexObjectProperty::Bytes(LexBytes { ..Default::default() }),
);
map.insert(
SmolStr::new_static("bookSig"),
LexObjectProperty::Bytes(LexBytes { ..Default::default() }),
);
map.insert(
SmolStr::new_static("cover"),
LexObjectProperty::Blob(LexBlob { ..Default::default() }),
);
map.insert(
SmolStr::new_static("did"),
LexObjectProperty::String(LexString {
description: Some(
CowStr::new_static(
"The DID of the person who registered the book. Included here, so it's verifiable with the bookSig.",
),
),
format: Some(LexStringFormat::Did),
..Default::default()
}),
);
map.insert(
SmolStr::new_static("occurredAt"),
LexObjectProperty::String(LexString {
format: Some(LexStringFormat::Datetime),
..Default::default()
}),
);
map.insert(
SmolStr::new_static("publicationId"),
LexObjectProperty::String(LexString {
description: Some(
CowStr::new_static("The book's Open Library Edition ID"),
),
..Default::default()
}),
);
map.insert(
SmolStr::new_static("title"),
LexObjectProperty::String(LexString {
description: Some(
CowStr::new_static("The title of the book"),
),
min_length: Some(1usize),
max_length: Some(512usize),
..Default::default()
}),
);
map
},
..Default::default()
}),
..Default::default()
}),
);
map
},
..Default::default()
}
}