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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
//! User-defined class implementation.

use async_hash::Hash;
use std::fmt;
use std::ops::Deref;

use async_trait::async_trait;
use destream::{de, en};
use safecast::{CastFrom, TryCastFrom};
use sha2::digest::Output;
use sha2::Digest;

use tc_value::{Link, Value};
use tcgeneric::{path_label, Id, Map, NativeClass, PathLabel, TCPathBuf};

use crate::scalar::*;
use crate::state::StateType;

use super::ObjectType;

const PATH: PathLabel = path_label(&["state", "class"]);

/// A user-defined class.
#[derive(Clone, Default, Eq, PartialEq)]
pub struct InstanceClass {
    extends: Option<Link>,
    link: Option<Link>,
    proto: Map<Scalar>,
}

impl InstanceClass {
    /// Construct a new class.
    pub fn new(link: Option<Link>, proto: Map<Scalar>) -> Self {
        Self {
            extends: None,
            link,
            proto,
        }
    }

    /// Extend an existing class.
    pub fn extend(extends: Link, link: Option<Link>, proto: Map<Scalar>) -> Self {
        Self {
            extends: Some(extends),
            link,
            proto,
        }
    }

    /// Construct a new anonymous class.
    pub fn anonymous(extends: Option<Link>, proto: Map<Scalar>) -> Self {
        Self {
            extends,
            link: None,
            proto,
        }
    }

    /// Return the link to this class, if any.
    pub fn link(&self) -> Link {
        if let Some(link) = &self.link {
            link.clone()
        } else {
            TCPathBuf::from(PATH).into()
        }
    }

    /// Consume this class and return its data.
    pub fn into_inner(self) -> (Option<Link>, Map<Scalar>) {
        (self.extends, self.proto)
    }

    /// Return `false` if this class has a self-`Link`
    pub fn is_anonymous(&self) -> bool {
        self.link.is_none()
    }

    /// Return the instance data of this class.
    pub fn proto(&'_ self) -> &'_ Map<Scalar> {
        &self.proto
    }
}

impl<D: Digest> Hash<D> for InstanceClass {
    fn hash(self) -> Output<D> {
        Hash::<D>::hash(&self)
    }
}

impl<'a, D: Digest> Hash<D> for &'a InstanceClass {
    fn hash(self) -> Output<D> {
        if let Some(link) = &self.extends {
            Hash::<D>::hash((link, self.proto.deref()))
        } else {
            Hash::<D>::hash(self.proto.deref())
        }
    }
}

impl tcgeneric::Class for InstanceClass {}

impl tcgeneric::Instance for InstanceClass {
    type Class = ObjectType;

    fn class(&self) -> ObjectType {
        ObjectType::Class
    }
}

impl From<StateType> for InstanceClass {
    fn from(st: StateType) -> Self {
        Self::extend(st.path().into(), None, Map::default())
    }
}

impl TryCastFrom<InstanceClass> for StateType {
    fn can_cast_from(class: &InstanceClass) -> bool {
        if class.proto.is_empty() {
            if let Some(classpath) = &class.extends {
                if classpath.host().is_none() {
                    return StateType::from_path(classpath.path()).is_some();
                }
            }
        }

        false
    }

    fn opt_cast_from(class: InstanceClass) -> Option<Self> {
        if class.proto.is_empty() {
            if let Some(classpath) = &class.extends {
                if classpath.host().is_none() {
                    return StateType::from_path(classpath.path());
                }
            }
        }

        None
    }
}

impl CastFrom<InstanceClass> for Scalar {
    fn cast_from(class: InstanceClass) -> Self {
        Self::Map(class.proto)
    }
}

impl CastFrom<InstanceClass> for Value {
    fn cast_from(class: InstanceClass) -> Self {
        class.extends.into()
    }
}

#[async_trait]
impl de::FromStream for InstanceClass {
    type Context = ();

    async fn from_stream<D: de::Decoder>(_: (), decoder: &mut D) -> Result<Self, D::Error> {
        decoder.decode_map(InstanceClassVisitor).await
    }
}

impl<'en> en::IntoStream<'en> for InstanceClass {
    fn into_stream<E: en::Encoder<'en>>(self, encoder: E) -> Result<E::Ok, E::Error> {
        if let Some(class) = self.extends {
            use en::EncodeMap;

            let mut map = encoder.encode_map(Some(1))?;
            map.encode_entry(class.to_string(), self.proto.into_inner())?;
            map.end()
        } else {
            self.proto.into_inner().into_stream(encoder)
        }
    }
}

impl From<InstanceClass> for Link {
    fn from(ic: InstanceClass) -> Link {
        if let Some(link) = ic.extends {
            link
        } else {
            TCPathBuf::from(PATH).into()
        }
    }
}

impl fmt::Debug for InstanceClass {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        fmt::Display::fmt(self, f)
    }
}

impl fmt::Display for InstanceClass {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        if let Some(link) = &self.link {
            write!(f, "class {}", link)
        } else if let Some(link) = &self.extends {
            write!(f, "anonymous subclass of {}", link)
        } else {
            f.write_str("Object")
        }
    }
}

struct InstanceClassVisitor;

#[async_trait]
impl de::Visitor for InstanceClassVisitor {
    type Value = InstanceClass;

    fn expecting() -> &'static str {
        "a user-defined Class"
    }

    async fn visit_map<A: de::MapAccess>(self, mut access: A) -> Result<InstanceClass, A::Error> {
        if let Some(key) = access.next_key::<String>(()).await? {
            if let Ok(extends) = key.parse() {
                log::debug!("Class extends {}", extends);
                let proto = access.next_value(()).await?;
                log::debug!("prototype is {}", proto);
                return Ok(InstanceClass::extend(extends, None, proto));
            }

            let mut proto = Map::new();
            let id: Id = key.parse().map_err(de::Error::custom)?;
            proto.insert(id, access.next_value(()).await?);

            while let Some(id) = access.next_key(()).await? {
                let value = access.next_value(()).await?;
                proto.insert(id, value);
            }

            Ok(InstanceClass::anonymous(None, proto.into()))
        } else {
            Ok(InstanceClass::anonymous(None, Map::default()))
        }
    }
}