#[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};
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 = "sh.weaver.publish.blob",
tag = "$type",
bound(deserialize = "S: Deserialize<'de> + BosStr")
)]
pub struct Blob<S: BosStr = DefaultStr> {
pub path: S,
pub upload: 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")]
pub struct BlobGetRecordOutput<S: BosStr = DefaultStr> {
#[serde(skip_serializing_if = "Option::is_none")]
pub cid: Option<Cid<S>>,
pub uri: AtUri<S>,
pub value: Blob<S>,
}
impl<S: BosStr> Blob<S> {
pub fn uri(uri: S) -> Result<RecordUri<S, BlobRecord>, UriError> {
RecordUri::try_from_uri(AtUri::new(uri)?)
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct BlobRecord;
impl XrpcResp for BlobRecord {
const NSID: &'static str = "sh.weaver.publish.blob";
const ENCODING: &'static str = "application/json";
type Output<S: BosStr> = BlobGetRecordOutput<S>;
type Err = RecordError;
}
impl<S: BosStr> From<BlobGetRecordOutput<S>> for Blob<S> {
fn from(output: BlobGetRecordOutput<S>) -> Self {
output.value
}
}
impl<S: BosStr> Collection for Blob<S> {
const NSID: &'static str = "sh.weaver.publish.blob";
type Record = BlobRecord;
}
impl Collection for BlobRecord {
const NSID: &'static str = "sh.weaver.publish.blob";
type Record = BlobRecord;
}
impl<S: BosStr> LexiconSchema for Blob<S> {
fn nsid() -> &'static str {
"sh.weaver.publish.blob"
}
fn def_name() -> &'static str {
"main"
}
fn lexicon_doc() -> LexiconDoc<'static> {
lexicon_doc_sh_weaver_publish_blob()
}
fn validate(&self) -> Result<(), ConstraintError> {
{
let value = &self.path;
#[allow(unused_comparisons)]
if <str>::len(value.as_ref()) < 1usize {
return Err(ConstraintError::MinLength {
path: ValidationPath::from_field("path"),
min: 1usize,
actual: <str>::len(value.as_ref()),
});
}
}
{
let value = &self.upload;
{
let size = value.blob().size;
if size > 10000000usize {
return Err(ConstraintError::BlobTooLarge {
path: ValidationPath::from_field("upload"),
max: 10000000usize,
actual: size,
});
}
}
}
{
let value = &self.upload;
{
let mime = value.blob().mime_type.as_str();
let accepted: &[&str] = &["*/*"];
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("upload"),
accepted: vec!["*/*".to_string()],
actual: mime.to_string(),
});
}
}
}
Ok(())
}
}
pub mod blob_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 Upload;
type Path;
}
pub struct Empty(());
impl sealed::Sealed for Empty {}
impl State for Empty {
type Upload = Unset;
type Path = Unset;
}
pub struct SetUpload<St: State = Empty>(PhantomData<fn() -> St>);
impl<St: State> sealed::Sealed for SetUpload<St> {}
impl<St: State> State for SetUpload<St> {
type Upload = Set<members::upload>;
type Path = St::Path;
}
pub struct SetPath<St: State = Empty>(PhantomData<fn() -> St>);
impl<St: State> sealed::Sealed for SetPath<St> {}
impl<St: State> State for SetPath<St> {
type Upload = St::Upload;
type Path = Set<members::path>;
}
#[allow(non_camel_case_types)]
pub mod members {
pub struct upload(());
pub struct path(());
}
}
pub struct BlobBuilder<S: BosStr, St: blob_state::State> {
_state: PhantomData<fn() -> St>,
_fields: (Option<S>, Option<BlobRef<S>>),
_type: PhantomData<fn() -> S>,
}
impl<S: BosStr> Blob<S> {
pub fn new() -> BlobBuilder<S, blob_state::Empty> {
BlobBuilder::new()
}
}
impl<S: BosStr> BlobBuilder<S, blob_state::Empty> {
pub fn new() -> Self {
BlobBuilder {
_state: PhantomData,
_fields: (None, None),
_type: PhantomData,
}
}
}
impl<S: BosStr, St> BlobBuilder<S, St>
where
St: blob_state::State,
St::Path: blob_state::IsUnset,
{
pub fn path(mut self, value: impl Into<S>) -> BlobBuilder<S, blob_state::SetPath<St>> {
self._fields.0 = Option::Some(value.into());
BlobBuilder {
_state: PhantomData,
_fields: self._fields,
_type: PhantomData,
}
}
}
impl<S: BosStr, St> BlobBuilder<S, St>
where
St: blob_state::State,
St::Upload: blob_state::IsUnset,
{
pub fn upload(
mut self,
value: impl Into<BlobRef<S>>,
) -> BlobBuilder<S, blob_state::SetUpload<St>> {
self._fields.1 = Option::Some(value.into());
BlobBuilder {
_state: PhantomData,
_fields: self._fields,
_type: PhantomData,
}
}
}
impl<S: BosStr, St> BlobBuilder<S, St>
where
St: blob_state::State,
St::Upload: blob_state::IsSet,
St::Path: blob_state::IsSet,
{
pub fn build(self) -> Blob<S> {
Blob {
path: self._fields.0.unwrap(),
upload: self._fields.1.unwrap(),
extra_data: Default::default(),
}
}
pub fn build_with_data(self, extra_data: BTreeMap<SmolStr, Data<S>>) -> Blob<S> {
Blob {
path: self._fields.0.unwrap(),
upload: self._fields.1.unwrap(),
extra_data: Some(extra_data),
}
}
}
fn lexicon_doc_sh_weaver_publish_blob() -> 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("sh.weaver.publish.blob"),
defs: {
let mut map = BTreeMap::new();
map.insert(
SmolStr::new_static("main"),
LexUserType::Record(LexRecord {
description: Some(CowStr::new_static(
"A simple record referencing a file hosted on a PDS",
)),
key: Some(CowStr::new_static("tid")),
record: LexRecordRecord::Object(LexObject {
required: Some(vec![
SmolStr::new_static("upload"),
SmolStr::new_static("path"),
]),
properties: {
#[allow(unused_mut)]
let mut map = BTreeMap::new();
map.insert(
SmolStr::new_static("path"),
LexObjectProperty::String(LexString {
description: Some(CowStr::new_static(
"relative path to the blob",
)),
min_length: Some(1usize),
..Default::default()
}),
);
map.insert(
SmolStr::new_static("upload"),
LexObjectProperty::Blob(LexBlob {
..Default::default()
}),
);
map
},
..Default::default()
}),
..Default::default()
}),
);
map
},
..Default::default()
}
}