Skip to content

Commit

Permalink
Serde sometimes, I tell ya
Browse files Browse the repository at this point in the history
  • Loading branch information
expede committed Feb 1, 2024
1 parent fdaae6c commit 8ff556c
Show file tree
Hide file tree
Showing 31 changed files with 255 additions and 197 deletions.
7 changes: 5 additions & 2 deletions src/ability/dynamic.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//! This module is for dynamic abilities, especially for FFI and Wasm support

use super::{arguments::Arguments, command::ToCommand};
use crate::{delegate::Delegatable, invoke::Resolvable, promise::Promise};
use crate::{delegation::Delegatable, invocation::Resolvable, promise::Promise};
use serde_derive::{Deserialize, Serialize};
use std::fmt::Debug;

// FIXME move module?
// FIXME move commented-out module?
// use js_sys;
// use wasm_bindgen::prelude::*;
// type JsDynamic = Dynamic<&'a js_sys::Function>;
Expand All @@ -19,6 +19,9 @@ use std::fmt::Debug;
// #[serde(skip_serializing)]
// pub serialize_nonce: DefaultTrue,

// NOTE the lack of checking functions!
// This is meant to be embedded inside of structs that have e.g. FFI bindings to
// a validation function, such as a &js_sys::Function, Ruby magnus::function!, etc etc
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct Generic<Args> {
pub cmd: String,
Expand Down
4 changes: 2 additions & 2 deletions src/ability/msg/send.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
ability::{arguments::Arguments, command::Command},
delegate::Delegatable,
invoke::Resolvable,
delegation::Delegatable,
invocation::Resolvable,
promise::Promise,
proof::{checkable::Checkable, parentful::Parentful, parents::CheckParents, same::CheckSame},
};
Expand Down
4 changes: 2 additions & 2 deletions src/ability/ucan.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use super::arguments::Arguments;
use crate::{ability::command::Command, delegate::Delegatable, promise::Promise};
use crate::{ability::command::Command, delegation::Delegatable, promise::Promise};
use libipld_core::ipld::Ipld;
use serde::{Deserialize, Serialize};
use std::fmt::Debug;

// NOTE This one is primarily for enabling delegated recipets
// NOTE This one is primarily for enabling delegationd recipets

// FIXME
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
Expand Down
9 changes: 5 additions & 4 deletions src/delegate.rs → src/delegation.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// FIXME rename delegate?

mod condition;
mod delegatable;
mod payload;

pub use condition::traits::Condition;
pub use condition::*;

pub use delegatable::Delegatable;
pub use payload::Payload;

Expand All @@ -16,4 +15,6 @@ use crate::signature;
///
/// # Examples
/// FIXME
pub type Delegation<B, C> = signature::Envelope<Payload<B, C>>;
pub type Delegation<T, C> = signature::Envelope<Payload<T, C>>;

// FIXME add a store with delegation indexing
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 5 additions & 5 deletions src/delegate/payload.rs → src/delegation/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use crate::{
ability::{arguments::Arguments, command::Command, dynamic},
capsule::Capsule,
did::Did,
invoke,
invoke::Resolvable,
invocation,
invocation::Resolvable,
nonce::Nonce,
proof::{
checkable::Checkable,
Expand Down Expand Up @@ -90,14 +90,14 @@ impl<T: Delegatable, C: Condition> From<Payload<T, C>> for Ipld {

impl<'a, T: Delegatable + Resolvable + Checkable + Clone, C: Condition> Payload<T, C> {
pub fn check<U: Delegatable + Clone>(
invoked: &'a invoke::Payload<T>, // FIXME promisory version
invoked: &'a invocation::Payload<T>, // FIXME promisory version
proofs: Vec<Payload<U, C>>,
now: SystemTime,
) -> Result<(), ()>
where
invoke::Payload<T>: Clone,
invocation::Payload<T>: Clone,
U::Builder: Clone + Into<T::Hierarchy>,
T::Hierarchy: From<invoke::Payload<T>>,
T::Hierarchy: From<invocation::Payload<T>>,
{
let start: Acc<'a, T> = Acc {
issuer: &invoked.issuer,
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/invoke.rs → src/invocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod payload;
mod resolvable;
mod serializer;

pub use payload::Payload;
pub use payload::{Payload, Unresolved};
pub use resolvable::Resolvable;

use crate::signature;
Expand Down
2 changes: 1 addition & 1 deletion src/invoke/payload.rs → src/invocation/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl<T> From<Payload<T>> for Ipld {

#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub(super) struct InternalSerializer {
struct InternalSerializer {
#[serde(rename = "iss")]
issuer: Did,
#[serde(rename = "sub")]
Expand Down
File renamed without changes.
File renamed without changes.
197 changes: 74 additions & 123 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,38 @@

//! ucan

use std::str::FromStr;

use cid::{multihash, Cid};
use serde::{de, Deserialize, Deserializer, Serialize};

pub mod builder;
pub mod capability;
pub mod crypto;
pub mod did;
pub mod did_verifier;
pub mod error;
pub mod plugins;
pub mod semantics;
pub mod store;
pub mod time;
pub mod ucan;

#[cfg(target_arch = "wasm32")]
mod wasm;

#[cfg(target_arch = "wasm32")]
pub use wasm::*;

#[doc(hidden)]
#[cfg(not(target_arch = "wasm32"))]
pub use linkme;

/// The default multihash algorithm used for UCANs
pub const DEFAULT_MULTIHASH: multihash::Code = multihash::Code::Sha2_256;

/// A decentralized identifier.
pub type Did = String;

use std::fmt::Debug;
// use std::str::FromStr;
//
// use cid::{multihash, Cid};
// use serde::{de, Deserialize, Deserializer, Serialize};
//
// pub mod builder;
// pub mod capability;
// pub mod crypto;
// pub mod did_verifier;
// pub mod error;
// pub mod plugins;
// pub mod semantics;
// pub mod store;
// pub mod ucan;
//
// #[cfg(target_arch = "wasm32")]
// mod wasm;
//
// #[cfg(target_arch = "wasm32")]
// pub use wasm::*;
//
// #[doc(hidden)]
// #[cfg(not(target_arch = "wasm32"))]
// pub use linkme;
//
// /// The default multihash algorithm used for UCANs
// pub const DEFAULT_MULTIHASH: multihash::Code = multihash::Code::Sha2_256;
//
// /// A decentralized identifier.
// pub type Did = String;
//
// use std::fmt::Debug;

// FIXME concrete abilitiy types in addition to promised version

Expand All @@ -62,103 +60,56 @@ use std::fmt::Debug;
// }
// }

// FIXME Remove
// pub trait Prove<T> {
// type Witness;
// // FIXME make sure that passing the top-level item through and not checking each
// // item in the chain against the next one is correct in the 1.0 semantics
// fn prove<'a>(&'a self, proof: &'a T) -> &Self::Witness;
// }
pub mod ability;
pub mod capsule;
pub mod delegation;
pub mod did;
pub mod invocation;
pub mod ipld;
pub mod nonce;
pub mod number;
pub mod promise;
pub mod proof;
pub mod receipt;
pub mod signature;
pub mod task;
pub mod time;

// impl<T> Prove<DelegateAny> for T {
// type Witness = T;
// FIXME consider a fact-system
// /// The empty fact
// #[derive(Debug, Clone, Default, Serialize, Deserialize)]
// pub struct EmptyFact {}
//
// fn prove<'a>(&'a self, proof: &'a DelegateAny) -> &Self::Witness {
// self
// }
// }
// /// The default fact
// pub type DefaultFact = EmptyFact;
//
// impl<T: Prove<T>> TryProve<T> for T {
// type Error = Void;
// type Proven = T;
// /// A newtype around Cid that (de)serializes as a string
// #[derive(Debug, Clone)]
// pub struct CidString(pub(crate) Cid);
//
// fn try_prove<'a>(&'a self, proof: &'a T) -> Result<&'a T, Void> {
// Ok(proof)
// impl Serialize for CidString {
// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
// where
// S: serde::Serializer,
// {
// serializer.serialize_str(self.0.to_string().as_str())
// }
// }

// FIXME lives etirely in bindgen
// https://rustwasm.github.io/docs/wasm-bindgen/contributing/design/importing-js-struct.html
// pub struct DynamicJs {
// pub command: Box<str>,
// pub args: BTreeMap<Box<str>, Ipld>,
// }
//
// impl TryProve<DynamicJs> for DynamicJs {
// type Error = ();
// type Proven = DynamicJs;
//
// fn try_prove<'a>(&'a self, proof: &'a DynamicJs) -> Result<&'a DynamicJs, ()> {
// impl<'de> Deserialize<'de> for CidString {
// fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
// where
// D: Deserializer<'de>,
// {
// let s = String::deserialize(deserializer)?;
//
// }
// }

// impl ProveDelegaton<DynamicJs> for DynamicJs {
// type Error = anyhow::Error;
//
// fn prove(&self, proof: &DynamicJs) -> Result<Self, anyhow::Error> {
// todo!()
// Cid::from_str(&s)
// .map(CidString)
// .map_err(|e| de::Error::custom(format!("invalid CID: {}", e)))
// }
// }
//

pub mod ability;
pub mod capsule;
pub mod delegate;
pub mod invoke;
pub mod ipld;
pub mod nonce;
pub mod number;
pub mod promise;
pub mod proof;
pub mod respond;
pub mod signature;
pub mod task;

/// The empty fact
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct EmptyFact {}

/// The default fact
pub type DefaultFact = EmptyFact;

/// A newtype around Cid that (de)serializes as a string
#[derive(Debug, Clone)]
pub struct CidString(pub(crate) Cid);

impl Serialize for CidString {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(self.0.to_string().as_str())
}
}

impl<'de> Deserialize<'de> for CidString {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;

Cid::from_str(&s)
.map(CidString)
.map_err(|e| de::Error::custom(format!("invalid CID: {}", e)))
}
}

/// Test utilities.
#[cfg(any(test, feature = "test_utils"))]
#[cfg_attr(docsrs, doc(cfg(feature = "test_utils")))]
pub mod test_utils;
// /// Test utilities.
// #[cfg(any(test, feature = "test_utils"))]
// #[cfg_attr(docsrs, doc(cfg(feature = "test_utils")))]
// pub mod test_utils;
2 changes: 1 addition & 1 deletion src/promise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use libipld_core::{ipld::Ipld, serde as ipld_serde};
use serde_derive::{Deserialize, Serialize};
use std::fmt::Debug;

// FIXME move under invoke?
// FIXME move under invocation?

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(untagged)]
Expand Down
8 changes: 4 additions & 4 deletions src/respond.rs → src/receipt.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
mod payload;
mod receipt;
mod responds;
mod store;

pub use payload::Payload;
pub use receipt::Receipt;
pub use responds::Responds;

use crate::signature;

pub type Receipt<T> = signature::Envelope<Payload<T>>;
pub use store::Store;
Loading

0 comments on commit 8ff556c

Please sign in to comment.