Skip to content

Commit

Permalink
rename Unknown -> Any
Browse files Browse the repository at this point in the history
  • Loading branch information
aDotInTheVoid committed Sep 2, 2023
1 parent 4cf1dee commit 3eb90e8
Show file tree
Hide file tree
Showing 17 changed files with 91 additions and 84 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ It's currently very WIP.

The code in the [`lexgen/lexicons/`](lexgen/lexicons/) directory is from the atproto repo, and is subject to the [MIT license](lexgen/lexicons/LICENSE)

Some of the `Any` serialization/deserialization/encoding/decoding code is coppied from libipld and serde_json.

Everything else is subject to the terms of the [Mozilla Public License, v. 2.0.](LICENSE)

TODO: Use reuse5 to make sense of this, it's a bit of a mess.
13 changes: 8 additions & 5 deletions lexgen/src/compiller/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use proc_macro2::{Ident, TokenStream};
use quote::{format_ident, quote, ToTokens};

use crate::lexicon::{
ArrayItem, Boolean, Bytes, CidLink, ObjectProperty, ParameterProperty, StringFormat,
ArrayItem, Boolean, Bytes, CidLink, ObjectProperty, ParameterProperty, StringFormat, Unknown,
XrpcParameters,
};

Expand Down Expand Up @@ -121,7 +121,7 @@ pub(super) enum FieldType {
I64,
U64,
Blob,
Unknown,
Any,
Bool,
CidLink,
Bytes,
Expand All @@ -136,6 +136,7 @@ impl FieldType {
ObjectProperty::Integer(i) => Self::integer(i),
ObjectProperty::Bytes(b) => Self::bytes(b),
ObjectProperty::CidLink(c) => Self::cid_link(c),
ObjectProperty::Unknown(u) => Self::unknown(u),

ObjectProperty::Array(a) => Self::array(a, doc_id),

Expand All @@ -144,7 +145,6 @@ impl FieldType {

// TODO: Implement.
ObjectProperty::Union(u) => (FieldType::Unit, &u.description),
ObjectProperty::Unknown(u) => (FieldType::Unknown, &u.description),
}
}

Expand All @@ -153,7 +153,7 @@ impl FieldType {
ParameterProperty::Boolean(b) => Self::bool(b),
ParameterProperty::Integer(i) => Self::integer(i),
ParameterProperty::String(s) => Self::string(s),
ParameterProperty::Unknown(_) => todo!(),
ParameterProperty::Unknown(u) => Self::unknown(u),
ParameterProperty::Array(_) => todo!(),
}
}
Expand Down Expand Up @@ -210,6 +210,9 @@ impl FieldType {
fn cid_link(c: &CidLink) -> (Self, &Option<String>) {
(Self::CidLink, &c.description)
}
fn unknown(u: &Unknown) -> (Self, &Option<String>) {
(Self::Any, &u.description)
}

fn bytes(b: &Bytes) -> (Self, &Option<String>) {
// TODO: min and max lenght
Expand Down Expand Up @@ -258,7 +261,7 @@ impl ToTokens for FieldType {

FieldType::Blob => quote!(_lex::_rt::Blob).to_tokens(tokens),
FieldType::Bytes => quote!(_lex::_rt::Bytes).to_tokens(tokens),
FieldType::Unknown => quote!(_lex::_rt::Unknown).to_tokens(tokens),
FieldType::Any => quote!(_lex::_rt::Any).to_tokens(tokens),
FieldType::CidLink => quote!(_lex::_rt::CidLink).to_tokens(tokens),

FieldType::StdString => quote!(::std::string::String).to_tokens(tokens),
Expand Down
4 changes: 2 additions & 2 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl Client {
self,
&create_record::Args {
collection: R::NSID,
record: triphosphate_vocab::to_unknown(record)?, // PERF: Avoid this
record: triphosphate_vocab::to_any(record)?, // PERF: Avoid this
repo,
// TODO: Make configurable
rkey: None,
Expand Down Expand Up @@ -130,7 +130,7 @@ impl Client {
.await
.context("failed to make XRPC call")?;

let value = triphosphate_vocab::from_unknown(resp.value)
let value = triphosphate_vocab::from_any(resp.value)
.context("failed to convert responce to expected type")?;

Ok(GetRecord {
Expand Down
2 changes: 1 addition & 1 deletion src/lex/app/bsky/embed/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub struct ViewRecord {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub labels: Option<Vec<_lex::com::atproto::label::defs::Label>>,
pub uri: _lex::_rt::AtUri,
pub value: _lex::_rt::Unknown,
pub value: _lex::_rt::Any,
}
impl _lex::_rt::LexItem for ViewRecord {
const URI: &'static str = "app.bsky.embed.record#viewRecord";
Expand Down
2 changes: 1 addition & 1 deletion src/lex/app/bsky/feed/defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ pub struct PostView {
#[serde(rename = "likeCount")]
#[serde(default, skip_serializing_if = "Option::is_none")]
pub like_count: Option<i64>,
pub record: _lex::_rt::Unknown,
pub record: _lex::_rt::Any,
#[serde(rename = "replyCount")]
#[serde(default, skip_serializing_if = "Option::is_none")]
pub reply_count: Option<i64>,
Expand Down
2 changes: 1 addition & 1 deletion src/lex/com/atproto/repo/create_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct Args {
///The NSID of the record collection.
pub collection: _lex::_rt::Nsid,
///The record to create.
pub record: _lex::_rt::Unknown,
pub record: _lex::_rt::Any,
///The handle or DID of the repo.
pub repo: _lex::_rt::AtIdentifier,
#[serde(default, skip_serializing_if = "Option::is_none")]
Expand Down
2 changes: 1 addition & 1 deletion src/lex/com/atproto/repo/get_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,5 @@ pub struct Responce {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub cid: Option<_lex::_rt::Cid>,
pub uri: _lex::_rt::AtUri,
pub value: _lex::_rt::Unknown,
pub value: _lex::_rt::Any,
}
4 changes: 2 additions & 2 deletions tests/it/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ fn check_json_str_roundtrip<T: Serialize + DeserializeOwned + PartialEq + Debug>
}

fn check_unknown_roundtrip<T: Serialize + DeserializeOwned + PartialEq + Debug>(item: &T) {
let unknown = triphosphate_vocab::to_unknown(item).unwrap();
let new_item: T = triphosphate_vocab::from_unknown(unknown).unwrap();
let unknown = triphosphate_vocab::to_any(item).unwrap();
let new_item: T = triphosphate_vocab::from_any(unknown).unwrap();

assert_eq!(item, &new_item);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ use serde::{de::DeserializeOwned, Serialize};

/// An unholy fussion of [serde_json::Value] and [libipld::Ipld]
#[derive(Debug, Clone, PartialEq)]
pub enum Unknown {
pub enum Any {
Null,
Bool(bool),
// ATProto data model doesn't have floats, or ints larget than 51bits.
Integer(i64),
String(String),
Bytes(crate::Bytes),
List(Vec<Unknown>),
Map(BTreeMap<String, Unknown>),
List(Vec<Any>),
Map(BTreeMap<String, Any>),
Link(crate::CidLink),
}

Expand All @@ -23,14 +23,14 @@ mod ser;
#[cfg(test)]
mod tests;

pub fn to_unknown<T>(value: T) -> Result<Unknown, serde_json::Error>
pub fn to_any<T>(value: T) -> Result<Any, serde_json::Error>
where
T: Serialize,
{
value.serialize(ser::Serializer)
}

pub fn from_unknown<T>(u: Unknown) -> Result<T, serde_json::Error>
pub fn from_any<T>(u: Any) -> Result<T, serde_json::Error>
where
T: DeserializeOwned,
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ use std::collections::BTreeMap as Map;

use serde_json::Error;

use crate::Any as Value;
use crate::Bytes;
use crate::CidLink;
use crate::Unknown as Value;

macro_rules! tri {
($e:expr $(,)?) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use libipld::cbor::error::{UnexpectedCode, UnknownTag};
use libipld::cbor::DagCborCodec as DagCbor;
use libipld::codec::Decode;

impl Decode<DagCbor> for super::Unknown {
impl Decode<DagCbor> for super::Any {
fn decode<R: Read + Seek>(_: DagCbor, r: &mut R) -> libipld::Result<Self> {
let major = read_major(r)?;
let ipld = match major.kind() {
Expand Down
22 changes: 22 additions & 0 deletions triphosphate-vocab/src/any/encode.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// https://github.com/ipld/libipld/blob/8478d6d66576636b9970cb3b00a232be7a88ea42/dag-cbor/src/encode.rs#L274-L288

use libipld::cbor::encode::write_null;
use libipld::cbor::DagCborCodec;
use libipld::codec::Encode;

use crate::Any;

impl Encode<DagCborCodec> for Any {
fn encode<W: std::io::Write>(&self, c: DagCborCodec, w: &mut W) -> libipld::Result<()> {
match self {
Any::Null => write_null(w),
Any::Bool(b) => b.encode(c, w),
Any::Integer(i) => i.encode(c, w),
Any::Bytes(b) => b.encode(c, w),
Any::String(s) => s.encode(c, w),
Any::List(l) => l.encode(c, w),
Any::Map(m) => m.encode(c, w),
Any::Link(l) => l.encode(c, w),
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use std::vec::Vec;

use serde_json::{Error, Result};

use super::to_unknown as to_value;
use super::Unknown as Value;
use super::to_any as to_value;
use super::Any as Value;

macro_rules! tri {
($e:expr $(,)?) => {
Expand Down
38 changes: 38 additions & 0 deletions triphosphate-vocab/src/any/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use libipld::{cbor::DagCborCodec, codec::assert_roundtrip, ipld, Ipld};

use crate::{Bytes, CidLink};

use super::*;

fn cbor_roundtrip(unknown: Any, ipld: Ipld) {
assert_roundtrip(DagCborCodec, &unknown, &ipld);
}

#[test]
fn primitives_cbor() {
cbor_roundtrip(Any::Integer(101), ipld!(101));
cbor_roundtrip(Any::Integer(-101), ipld!(-101));
cbor_roundtrip(Any::Integer(i64::MIN), ipld!(i64::MIN));
cbor_roundtrip(Any::Integer(i64::MAX), ipld!(i64::MAX));

cbor_roundtrip(Any::Bool(true), ipld!(true));
cbor_roundtrip(Any::Bool(false), ipld!(false));

cbor_roundtrip(
Any::Bytes(Bytes::new(vec![1, 3, 3, 7])),
Ipld::Bytes(vec![1, 3, 3, 7]),
);
cbor_roundtrip(Any::Bytes(Bytes::new(vec![])), Ipld::Bytes(vec![]));

cbor_roundtrip(Any::String(String::new()), ipld!(""));
cbor_roundtrip(Any::String("__test__".to_owned()), ipld!("__test__"));

cbor_roundtrip(Any::List(vec![]), ipld!([]));
cbor_roundtrip(Any::Null, ipld!(null));

let cid_link =
CidLink::from_str("bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a").unwrap();
let cid =
cid::Cid::try_from("bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a").unwrap();
cbor_roundtrip(Any::Link(cid_link), Ipld::Link(cid));
}
4 changes: 2 additions & 2 deletions triphosphate-vocab/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ impl fmt::Display for ParseError {
}
impl std::error::Error for ParseError {}

mod any;
mod at_identifer;
mod at_uri;
mod bytes;
Expand All @@ -41,9 +42,9 @@ mod handle;
mod language;
mod nsid;
mod parsing;
mod unknown;

pub use self::cid::Cid;
pub use any::{from_any, to_any, Any};
pub use at_identifer::AtIdentifier;
pub use at_uri::AtUri;
pub use bytes::Bytes;
Expand All @@ -53,7 +54,6 @@ pub use did::Did;
pub use handle::Handle;
pub use language::Language;
pub use nsid::Nsid;
pub use unknown::{from_unknown, to_unknown, Unknown};

macro_rules! serde_impls {
($($name:path)*) => {$(
Expand Down
22 changes: 0 additions & 22 deletions triphosphate-vocab/src/unknown/encode.rs

This file was deleted.

38 changes: 0 additions & 38 deletions triphosphate-vocab/src/unknown/tests.rs

This file was deleted.

0 comments on commit 3eb90e8

Please sign in to comment.