@@ -39,21 +39,27 @@ impl std::error::Error for IndexError {}
39
39
/// derived from the primitives defined here.
40
40
pub trait Primitives {
41
41
/// Type for a boolean.
42
- type Bool ;
42
+ type Bool : fmt :: Debug + From < bool > + Into < bool > ;
43
43
/// Type for an integer.
44
- type Integer ;
44
+ type Integer : fmt :: Debug + From < u64 > + From < i64 > + From < i128 > + Into < i128 > ;
45
45
/// Type for a float.
46
- type Float ;
46
+ type Float : fmt :: Debug + From < f64 > + Into < f64 > ;
47
47
/// 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 > ;
49
53
/// Type for bytes.
50
- type Bytes ;
54
+ type Bytes : fmt :: Debug + From < Vec < u8 > > + Into < Vec < u8 > > ;
51
55
/// 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 > ;
53
58
}
54
59
55
60
/// The default values for the primitive types.
56
- #[ derive( Clone ) ]
61
+ #[ derive( Clone , Debug ) ]
62
+ //pub struct DefaultPrimitives<'de>(&'de PhantomData<_>);
57
63
pub struct DefaultPrimitives ;
58
64
59
65
impl Primitives for DefaultPrimitives {
@@ -65,9 +71,30 @@ impl Primitives for DefaultPrimitives {
65
71
type Link = Cid ;
66
72
}
67
73
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
+
68
95
/// The generic version of the core IPLD type that allows using custom primitive types.
69
96
#[ derive( Clone ) ]
70
- pub enum IpldGeneric < P : Primitives = DefaultPrimitives > {
97
+ pub enum IpldGeneric < P : Primitives > {
71
98
/// Represents the absence of a value or the value undefined.
72
99
Null ,
73
100
/// Represents a boolean value.
@@ -89,9 +116,13 @@ pub enum IpldGeneric<P: Primitives = DefaultPrimitives> {
89
116
}
90
117
91
118
/// The core IPLD type.
119
+ //pub type Ipld<'de> = IpldGeneric<DefaultPrimitives<'de>>;
92
120
pub type Ipld = IpldGeneric < DefaultPrimitives > ;
93
121
94
- impl fmt:: Debug for Ipld {
122
+ impl < P > fmt:: Debug for IpldGeneric < P >
123
+ where
124
+ P : Primitives ,
125
+ {
95
126
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
96
127
if f. alternate ( ) {
97
128
match self {
0 commit comments