Atproto, serde deserialize & move strong_ref
This adds Serde deserialization. It also needs to add an error for handling the failure on these deserializations. I broke strong_ref into its own file because it was starting to grow a lot.
This commit is contained in:
parent
34719e7d01
commit
5bc903b2fa
11 changed files with 194 additions and 54 deletions
|
|
@ -20,6 +20,8 @@ pub enum FormatError {
|
|||
pub enum ParseError {
|
||||
#[error("Time Parse Error: {0}")]
|
||||
Datetime(#[from] time::error::Parse),
|
||||
#[error("Json Parse Error: {0}")]
|
||||
Serde(#[from] serde_json::error::Error),
|
||||
#[error("Length of parsed object too long, max: {max:?}, got: {got:?}.")]
|
||||
Length { max: usize, got: usize },
|
||||
#[error("Currently Did is enforced, cannot use handle, {handle:?}")]
|
||||
|
|
|
|||
|
|
@ -1,5 +1,20 @@
|
|||
use crate::error::{Error, ParseError};
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! basic_deserializer {
|
||||
($name:ident) => {
|
||||
impl<'de> serde::de::Deserialize<'de> for $name {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: serde::de::Deserializer<'de>,
|
||||
{
|
||||
let value: String = serde::de::Deserialize::deserialize(deserializer)?;
|
||||
value.parse::<$name>().map_err(<D::Error as serde::de::Error>::custom)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! basic_string_type {
|
||||
($name:ident, $regex:literal, $max_len:literal) => {
|
||||
pub struct $name { value: String, }
|
||||
|
|
@ -35,20 +50,24 @@ macro_rules! basic_string_type {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
basic_deserializer!($name);
|
||||
}
|
||||
}
|
||||
|
||||
mod did;
|
||||
pub use did::Did;
|
||||
mod cid;
|
||||
pub use cid::Cid;
|
||||
mod authority;
|
||||
pub use authority::Authority;
|
||||
mod cid;
|
||||
mod datetime;
|
||||
pub use datetime::Datetime;
|
||||
mod did;
|
||||
mod record_key;
|
||||
pub use record_key::RecordKey;
|
||||
mod strong_ref;
|
||||
mod uri;
|
||||
pub use authority::Authority;
|
||||
pub use cid::Cid;
|
||||
pub use datetime::Datetime;
|
||||
pub use did::Did;
|
||||
pub use record_key::RecordKey;
|
||||
pub use strong_ref::StrongRef;
|
||||
pub use uri::Uri;
|
||||
|
||||
basic_string_type!(Handle,
|
||||
|
|
@ -63,22 +82,3 @@ basic_string_type!(Tid,
|
|||
r"^[234567abcdefghij][234567abcdefghijklmnopqrstuvwxyz]{12}$",
|
||||
13
|
||||
);
|
||||
|
||||
pub struct StrongRef<T> {
|
||||
content: T,
|
||||
cid: Cid,
|
||||
}
|
||||
|
||||
impl<T> StrongRef<T> {
|
||||
pub fn get_content(&self) -> &T {
|
||||
&self.content
|
||||
}
|
||||
|
||||
pub fn extract_content(self) -> (T, Cid) {
|
||||
(self.content, self.cid)
|
||||
}
|
||||
|
||||
pub fn get_cid(&self) -> &Cid {
|
||||
&self.cid
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,12 +1,16 @@
|
|||
use crate::{
|
||||
types::{Did, Handle},
|
||||
types::{
|
||||
Did, Handle
|
||||
},
|
||||
error::{Error, ParseError},
|
||||
};
|
||||
use serde::Deserialize;
|
||||
use std::{
|
||||
fmt::{Display, Formatter, Result as FmtResult},
|
||||
str::FromStr,
|
||||
};
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub enum Authority {
|
||||
Did(Did),
|
||||
Handle(Handle),
|
||||
|
|
|
|||
|
|
@ -1,4 +1,7 @@
|
|||
use crate::error::Error;
|
||||
use crate::{
|
||||
basic_deserializer,
|
||||
error::Error
|
||||
};
|
||||
|
||||
pub struct Cid { value: String, }
|
||||
|
||||
|
|
@ -14,3 +17,5 @@ impl std::str::FromStr for Cid {
|
|||
Ok(Self { value: s.to_string() })
|
||||
}
|
||||
}
|
||||
|
||||
basic_deserializer!(Cid);
|
||||
|
|
|
|||
|
|
@ -1,4 +1,7 @@
|
|||
use crate::error::{Error, ParseError, FormatError};
|
||||
use crate::{
|
||||
basic_deserializer,
|
||||
error::{Error, ParseError, FormatError},
|
||||
};
|
||||
use time::{
|
||||
UtcDateTime,
|
||||
format_description::well_known::{Rfc3339, Iso8601},
|
||||
|
|
@ -61,3 +64,5 @@ impl FromStr for Datetime {
|
|||
Ok(Datetime { time: datetime, derived_string: s.to_string() })
|
||||
}
|
||||
}
|
||||
|
||||
basic_deserializer!(Datetime);
|
||||
|
|
|
|||
|
|
@ -1,4 +1,7 @@
|
|||
use crate::error::{Error, ParseError};
|
||||
use crate::{
|
||||
basic_deserializer,
|
||||
error::{Error, ParseError},
|
||||
};
|
||||
use std::{
|
||||
fmt::{Display, Formatter, Result as FmtResult},
|
||||
str::FromStr,
|
||||
|
|
@ -31,6 +34,8 @@ impl FromStr for DidMethod {
|
|||
}
|
||||
}
|
||||
|
||||
basic_deserializer!(DidMethod);
|
||||
|
||||
pub struct Did {
|
||||
method: DidMethod,
|
||||
identifier: String,
|
||||
|
|
@ -70,3 +75,5 @@ impl FromStr for Did {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
basic_deserializer!(Did);
|
||||
|
|
|
|||
|
|
@ -61,3 +61,5 @@ impl FromStr for RecordKey {
|
|||
Ok(RecordKey::Any(s.to_string()))
|
||||
}
|
||||
}
|
||||
|
||||
basic_deserializer!(RecordKey);
|
||||
|
|
|
|||
46
atproto/src/types/strong_ref.rs
Normal file
46
atproto/src/types/strong_ref.rs
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
use crate::{
|
||||
basic_deserializer,
|
||||
types::{Cid, Uri},
|
||||
error::{Error, ParseError},
|
||||
};
|
||||
|
||||
pub struct StrongRef<T> {
|
||||
content: T,
|
||||
cid: Cid,
|
||||
}
|
||||
|
||||
impl StrongRef<Uri> {
|
||||
pub fn from_atrium_api(strong_ref: atrium_api::com::atproto::repo::strong_ref::MainData) -> Result<Self, Error> {
|
||||
let str_cid = serde_json::to_string(&strong_ref.cid).map_err(|e| {
|
||||
Error::Parse { err: ParseError::Serde(e), object: "Uri".to_string() }
|
||||
})?;
|
||||
Ok(Self {
|
||||
content: strong_ref.uri.parse::<Uri>()?,
|
||||
cid: str_cid.parse::<Cid>()?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> StrongRef<T> {
|
||||
pub fn map_content<U, F>(self, f: F) -> StrongRef<U>
|
||||
where
|
||||
F: FnOnce(T) -> U,
|
||||
{
|
||||
StrongRef {
|
||||
content: f(self.content),
|
||||
cid: self.cid,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_content(&self) -> &T {
|
||||
&self.content
|
||||
}
|
||||
|
||||
pub fn extract_content(self) -> (T, Cid) {
|
||||
(self.content, self.cid)
|
||||
}
|
||||
|
||||
pub fn get_cid(&self) -> &Cid {
|
||||
&self.cid
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
use crate::{
|
||||
basic_deserializer,
|
||||
types::{Did, Authority, Nsid, RecordKey},
|
||||
error::{Error, ParseError},
|
||||
};
|
||||
|
|
@ -33,47 +34,78 @@ impl Display for Uri {
|
|||
impl FromStr for Uri {
|
||||
type Err = Error;
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
if s.len() > 8000 {
|
||||
return Err(Error::Parse {
|
||||
err: ParseError::Length { max: 8000, got: s.len() },
|
||||
object: "Did".to_string(),
|
||||
});
|
||||
}
|
||||
Self::check_length(s)?;
|
||||
|
||||
let Some((
|
||||
_whole, unchecked_authority, unchecked_collection, unchecked_rkey
|
||||
)) = regex_captures!(
|
||||
)): Option<(&str, &str, &str, &str)> = regex_captures!(
|
||||
r"/^at:\/\/([\w\.\-_~:]+)(?:\/([\w\.\-_~:]+)(?:)\/([\w\.\-_~:]+))?$/i",
|
||||
s,
|
||||
) else {
|
||||
) else {
|
||||
return Err(Error::Parse {
|
||||
err: ParseError::Format,
|
||||
object: "Uri".to_string(),
|
||||
});
|
||||
};
|
||||
|
||||
let did = match Authority::from_str(unchecked_authority)? {
|
||||
Authority::Handle(h) =>
|
||||
return Err(Error::Parse {
|
||||
err: ParseError::ForceDid { handle: h.to_string() },
|
||||
object: "Uri".to_string(),
|
||||
}),
|
||||
Authority::Did(d) => d,
|
||||
};
|
||||
let did = Self::check_authority(unchecked_authority.to_string())?;
|
||||
|
||||
let collection = if unchecked_collection.is_empty() { None }
|
||||
else { Some(unchecked_collection.parse::<Nsid>()?) };
|
||||
else { Some(Self::check_collection(unchecked_collection.to_string())?) };
|
||||
|
||||
let rkey = if unchecked_rkey.is_empty() { None }
|
||||
else { Some(unchecked_rkey.parse::<RecordKey>()?) };
|
||||
else { Some(Self::check_rkey(unchecked_rkey.to_string())?) };
|
||||
|
||||
Ok(Uri { authority: did, collection, rkey })
|
||||
}
|
||||
}
|
||||
|
||||
impl Uri {
|
||||
pub fn from_components(
|
||||
authority_str: String, collection_str: Option<String>,
|
||||
rkey_str: Option<String>
|
||||
) -> Result<Self, Error> {
|
||||
let authority = Self::check_authority(authority_str)?;
|
||||
let collection = collection_str.map(Self::check_collection).transpose()?;
|
||||
let rkey = rkey_str.map(Self::check_rkey).transpose()?;
|
||||
let uri = Uri { authority, collection, rkey };
|
||||
Self::check_length(&uri.to_string())?;
|
||||
|
||||
Ok(uri)
|
||||
}
|
||||
|
||||
fn check_length(s: &str) -> Result<(), Error> {
|
||||
if s.len() > 8000 {
|
||||
return Err(Error::Parse {
|
||||
err: ParseError::Length { max: 8000, got: s.len() },
|
||||
object: "Did".to_string(),
|
||||
});
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn check_authority(authority: String) -> Result<Did, Error> {
|
||||
Ok(match Authority::from_str(&authority)? {
|
||||
Authority::Handle(h) =>
|
||||
return Err(Error::Parse {
|
||||
err: ParseError::ForceDid { handle: h.to_string() },
|
||||
object: "Uri".to_string(),
|
||||
}),
|
||||
Authority::Did(d) => d,
|
||||
})
|
||||
}
|
||||
|
||||
fn check_collection(collection: String) -> Result<Nsid, Error> {
|
||||
Ok(collection.parse::<Nsid>()?)
|
||||
}
|
||||
|
||||
fn check_rkey(rkey: String) -> Result<RecordKey, Error> {
|
||||
Ok(rkey.parse::<RecordKey>()?)
|
||||
}
|
||||
|
||||
pub fn authority_as_did(&self) -> &Did {
|
||||
&self.authority
|
||||
}
|
||||
}
|
||||
|
||||
basic_deserializer!(Uri);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue