1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
use rapira::{Rapira, RapiraError};
use serde::Serialize;
#[cfg(feature = "ts-types")]
use specta::{DataType, DefOpts, Type};
#[cfg(feature = "ts-types")]
use typescript_type_def::{
    type_expr::{DefinedTypeInfo, Ident, TypeDefinition, TypeExpr, TypeInfo, TypeIntersection},
    TypeDef,
};

use crate::{Cid, Record};

#[derive(Clone, Debug, Serialize)]
pub struct Entry<Val>
where
    Val: Record,
{
    #[serde(flatten)]
    pub key: Val::SelfId,
    #[serde(flatten)]
    pub val: Val,
}

impl<Val> Entry<Val>
where
    Val: Record,
{
    pub fn new(key: Val::SelfId, val: Val) -> Self {
        Entry { key, val }
    }

    pub fn get_id(self) -> Val::SelfId {
        self.key
    }
    pub fn get_val(&self) -> &Val {
        &self.val
    }
    pub fn split(self) -> (Val::SelfId, Val) {
        let Entry { key, val } = self;
        (key, val)
    }
}

impl<Val> From<(Val::SelfId, Val)> for Entry<Val>
where
    Val: Record,
{
    fn from(tuple: (Val::SelfId, Val)) -> Self {
        Entry::new(tuple.0, tuple.1)
    }
}

const fn a_b_size(a: Option<usize>, b: Option<usize>) -> Option<usize> {
    match a {
        Some(s) => match b {
            Some(ss) => Some(s + ss),
            None => None,
        },
        None => None,
    }
}

impl<Val> Rapira for Entry<Val>
where
    Val: Record,
    <Val::SelfId as Cid>::B: Rapira,
{
    const STATIC_SIZE: Option<usize> =
        a_b_size(Val::STATIC_SIZE, <Val::SelfId as Cid>::B::STATIC_SIZE);

    fn size(&self) -> usize {
        match Self::STATIC_SIZE {
            Some(s) => s,
            // None => self.key.bytes().as_ref().len(),
            None => self.key.encode().size() + self.val.size(),
        }
    }

    fn check_bytes(slice: &mut &[u8]) -> rapira::Result<()> {
        <Val::SelfId as Cid>::B::check_bytes(slice)?;
        Val::check_bytes(slice)?;
        Ok(())
    }

    fn from_slice_unchecked(slice: &mut &[u8]) -> rapira::Result<Self>
    where
        Self: Sized,
    {
        let key = <Val::SelfId as Cid>::B::from_slice_unchecked(slice)?;
        let key = <Val::SelfId>::from_bytes(key.as_ref())
            .map_err(|_| RapiraError::OtherError("Cid::from_bytes error"))?;
        let val = Val::from_slice_unchecked(slice)?;
        Ok(Entry::new(key, val))
    }

    fn from_slice(slice: &mut &[u8]) -> rapira::Result<Self>
    where
        Self: Sized,
    {
        let key = <Val::SelfId as Cid>::B::from_slice(slice)?;
        let key = <Val::SelfId>::from_bytes(key.as_ref())
            .map_err(|_| RapiraError::OtherError("Entry Cid error"))?;
        let val = Val::from_slice(slice)?;
        Ok(Entry::new(key, val))
    }

    unsafe fn from_slice_unsafe(slice: &mut &[u8]) -> rapira::Result<Self>
    where
        Self: Sized,
    {
        let key = <Val::SelfId as Cid>::B::from_slice_unsafe(slice)?;
        let key = <Val::SelfId>::from_bytes_unsafe(key.as_ref());
        let val = Val::from_slice_unsafe(slice)?;
        Ok(Entry::new(key, val))
    }

    fn convert_to_bytes(&self, slice: &mut [u8], cursor: &mut usize) {
        self.key.encode().convert_to_bytes(slice, cursor);
        self.val.convert_to_bytes(slice, cursor);
    }

    fn try_convert_to_bytes(&self, slice: &mut [u8], cursor: &mut usize) -> rapira::Result<()> {
        self.key.encode().try_convert_to_bytes(slice, cursor)?;
        self.val.try_convert_to_bytes(slice, cursor)?;
        Ok(())
    }
}

// #[cfg(feature = "ts-types")]
// impl<Val> Type for Entry<Val>
// where
//     Val: Record ,
// {
//     const NAME: &'static str = "Entry";
//     fn inline(_: DefOpts, _: &[DataType]) -> DataType {}
// }

#[cfg(feature = "ts-types")]
impl<Val> TypeDef for Entry<Val>
where
    Self: 'static,
    Val: Record + TypeDef,
    Val::SelfId: TypeDef,
{
    const INFO: TypeInfo = TypeInfo::Defined(DefinedTypeInfo {
        def: TypeDefinition {
            docs: None,
            path: &[],
            name: Ident("Entry"),
            generic_vars: &[Ident("T"), Ident("Id")],
            def: TypeExpr::Intersection(TypeIntersection {
                docs: None,
                members: &[TypeExpr::Ref(&Val::SelfId::INFO), TypeExpr::Ref(&Val::INFO)],
            }),
        },
        generic_args: &[],
    });
}