Skip to content

Commit edc70ed

Browse files
committed
WIP: deserializer seems to compile
The serializer doesn't compile though.
1 parent 4ef5d73 commit edc70ed

File tree

4 files changed

+276
-142
lines changed

4 files changed

+276
-142
lines changed

src/ipld.rs

+40-9
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,27 @@ impl std::error::Error for IndexError {}
3939
/// derived from the primitives defined here.
4040
pub trait Primitives {
4141
/// Type for a boolean.
42-
type Bool;
42+
type Bool: fmt::Debug + From<bool> + Into<bool>;
4343
/// Type for an integer.
44-
type Integer;
44+
type Integer: fmt::Debug + From<u64> + From<i64> + From<i128> + Into<i128>;
4545
/// Type for a float.
46-
type Float;
46+
type Float: fmt::Debug + From<f64> + Into<f64>;
4747
/// Type for a String.
48-
type String;
48+
#[cfg(not(feature = "serde"))]
49+
type String: fmt::Debug + From<String> + Into<String> + Ord;
50+
// TODO vmx 2024-08-14: Check if the `for <'de>` is the right thing to do here.
51+
#[cfg(feature = "serde")]
52+
type String: fmt::Debug + From<String> + Into<String> + Ord + for<'de> serde::Deserialize<'de>;
4953
/// Type for bytes.
50-
type Bytes;
54+
type Bytes: fmt::Debug + From<Vec<u8>> + Into<Vec<u8>>;
5155
/// Type for a link.
52-
type Link;
56+
// TODO vmx 2024-08-14: This should be `CidGeneric` and not just `Cid`.
57+
type Link: fmt::Debug + fmt::Display + From<Cid> + Into<Cid>;
5358
}
5459

5560
/// The default values for the primitive types.
56-
#[derive(Clone)]
61+
#[derive(Clone, Debug)]
62+
//pub struct DefaultPrimitives<'de>(&'de PhantomData<_>);
5763
pub struct DefaultPrimitives;
5864

5965
impl Primitives for DefaultPrimitives {
@@ -65,9 +71,30 @@ impl Primitives for DefaultPrimitives {
6571
type Link = Cid;
6672
}
6773

74+
#[cfg(feature = "serde")]
75+
pub trait PrimitivesSerde<'de>: Primitives {
76+
fn visit_integer<V: serde::de::Visitor<'de>, E: serde::de::Error>(
77+
visitor: V,
78+
value: <Self as Primitives>::Integer,
79+
) -> Result<V::Value, E>;
80+
}
81+
82+
#[cfg(feature = "serde")]
83+
impl<'de> PrimitivesSerde<'de> for DefaultPrimitives {
84+
//fn visit_integer<V>(visitor: V) -> Result<V::Value, Self::Error>
85+
fn visit_integer<V, E>(visitor: V, value: Self::Integer) -> Result<V::Value, E>
86+
where
87+
V: serde::de::Visitor<'de>,
88+
E: serde::de::Error,
89+
{
90+
visitor.visit_i128(value)
91+
//println!("vmx: visit_integer")
92+
}
93+
}
94+
6895
/// The generic version of the core IPLD type that allows using custom primitive types.
6996
#[derive(Clone)]
70-
pub enum IpldGeneric<P: Primitives = DefaultPrimitives> {
97+
pub enum IpldGeneric<P: Primitives> {
7198
/// Represents the absence of a value or the value undefined.
7299
Null,
73100
/// Represents a boolean value.
@@ -89,9 +116,13 @@ pub enum IpldGeneric<P: Primitives = DefaultPrimitives> {
89116
}
90117

91118
/// The core IPLD type.
119+
//pub type Ipld<'de> = IpldGeneric<DefaultPrimitives<'de>>;
92120
pub type Ipld = IpldGeneric<DefaultPrimitives>;
93121

94-
impl fmt::Debug for Ipld {
122+
impl<P> fmt::Debug for IpldGeneric<P>
123+
where
124+
P: Primitives,
125+
{
95126
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
96127
if f.alternate() {
97128
match self {

src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![allow(clippy::needless_doctest_main)]
22
#![doc = include_str!("../README.md")]
3-
#![deny(missing_docs)]
4-
#![deny(warnings)]
3+
//#![deny(missing_docs)]
4+
//#![deny(warnings)]
55
#![cfg_attr(not(feature = "std"), no_std)]
66

77
extern crate alloc;

0 commit comments

Comments
 (0)