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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
use std::collections::HashMap;

use super::{OneBotBytes, TryAsMut, TryAsRef};
use crate::error::{WalleError, WalleResult};

/// 扩展字段 Map
pub type ValueMap = HashMap<String, Value>;

mod _macro;
mod _serde;

/// 扩展字段 MapValue
#[derive(Debug, Clone, PartialEq)]
pub enum Value {
    Str(String),
    F64(f64),
    Int(i64),
    Bool(bool),
    Map(ValueMap),
    List(Vec<Value>),
    Bytes(OneBotBytes),
    Null,
}

macro_rules! impl_from {
    ($inty: tt,$ty: ty) => {
        impl From<$ty> for Value {
            fn from(v: $ty) -> Self {
                Value::$inty(v)
            }
        }
    };
    ($inty: tt, $ty: ty, $ty0:ty) => {
        impl From<$ty> for Value {
            fn from(v: $ty) -> Self {
                Value::$inty(v as $ty0)
            }
        }
    };
}

impl_from!(Str, String);
impl_from!(Int, i64);
impl_from!(Int, i8, i64);
impl_from!(Int, i16, i64);
impl_from!(Int, i32, i64);
// impl_from!(Int, u8, i64);
impl_from!(Int, u16, i64);
impl_from!(Int, u32, i64);
impl_from!(F64, f64);
impl_from!(F64, f32, f64);
impl_from!(Bool, bool);
impl_from!(Bytes, OneBotBytes);

impl From<()> for Value {
    fn from(_: ()) -> Self {
        Value::Null
    }
}

impl From<Vec<u8>> for Value {
    fn from(v: Vec<u8>) -> Self {
        Value::Bytes(OneBotBytes(v))
    }
}

impl From<&[u8]> for Value {
    fn from(v: &[u8]) -> Self {
        Value::Bytes(OneBotBytes(v.to_vec()))
    }
}

impl From<&str> for Value {
    fn from(v: &str) -> Self {
        Value::Str(v.to_owned())
    }
}

impl<T> From<HashMap<String, T>> for Value
where
    T: Into<Value>,
{
    fn from(v: HashMap<String, T>) -> Self {
        let mut map = HashMap::new();
        for (k, v) in v {
            map.insert(k, v.into());
        }
        Value::Map(map)
    }
}

impl<T> From<Vec<T>> for Value
where
    T: Into<Value>,
{
    fn from(v: Vec<T>) -> Self {
        let mut list = Vec::new();
        for v in v {
            list.push(v.into());
        }
        Value::List(list)
    }
}

impl<T> From<Option<T>> for Value
where
    T: Into<Value>,
{
    fn from(v: Option<T>) -> Self {
        match v {
            Some(v) => v.into(),
            None => Value::Null,
        }
    }
}

macro_rules! impl_try_from {
    ($inty: tt, $ty: ty) => {
        impl TryFrom<Value> for $ty {
            type Error = WalleError;
            fn try_from(i: Value) -> Result<$ty, Self::Error> {
                match i {
                    Value::$inty(v) => Ok(v),
                    v => Err(WalleError::ValueTypeNotMatch(
                        std::any::type_name::<$ty>().to_string(),
                        format!("{:?}", v),
                    )),
                }
            }
        }
    };
    ($inty: tt as $ty: ty) => {
        impl TryFrom<Value> for $ty {
            type Error = WalleError;
            fn try_from(i: Value) -> Result<$ty, Self::Error> {
                match i {
                    Value::$inty(v) => Ok(v as $ty),
                    v => Err(WalleError::ValueTypeNotMatch(
                        std::any::type_name::<$ty>().to_string(),
                        format!("{:?}", v),
                    )),
                }
            }
        }
    };
}

impl_try_from!(Str, String);
impl_try_from!(Int, i64);
impl_try_from!(Int as i32);
impl_try_from!(Int as u32);
impl_try_from!(Int as i16);
impl_try_from!(Int as u16);
impl_try_from!(Int as i8);
impl_try_from!(Int as u8);
impl_try_from!(F64, f64);
impl_try_from!(F64 as f32);
impl_try_from!(Bool, bool);

impl<V> TryFrom<Value> for Vec<V>
where
    V: TryFrom<Value, Error = WalleError>,
{
    type Error = WalleError;
    fn try_from(value: Value) -> Result<Self, Self::Error> {
        match value {
            Value::List(l) => l.into_iter().map(|v| v.try_into()).collect(),
            v => Err(WalleError::ValueTypeNotMatch(
                format!("List<{}>", std::any::type_name::<V>()),
                format!("{:?}", v),
            )),
        }
    }
}

impl<V> TryFrom<Value> for HashMap<String, V>
where
    V: TryFrom<Value, Error = WalleError>,
{
    type Error = WalleError;
    fn try_from(value: Value) -> Result<Self, Self::Error> {
        match value {
            Value::Map(m) => m
                .into_iter()
                .map(|e| e.1.try_into().map(|v| (e.0, v)))
                .collect(),
            v => Err(WalleError::ValueTypeNotMatch(
                format!("Map<{}>", std::any::type_name::<V>()),
                format!("{:?}", v),
            )),
        }
    }
}

// impl<T> TryFrom<ExtendedValue> for Option<T>
// where
//     T: TryFrom<ExtendedValue, Error = WalleError>,
// {
//     type Error = WalleError;
//     fn try_from(value: ExtendedValue) -> Result<Self, Self::Error> {
//         match value {
//             ExtendedValue::Null => Ok(None),
//             v => Ok(Some(v.try_into()?)),
//         }
//     }
// }

/// json bytes could be String
impl TryFrom<Value> for OneBotBytes {
    type Error = WalleError;
    fn try_from(value: Value) -> Result<Self, Self::Error> {
        match value {
            Value::Bytes(v) => Ok(v),
            Value::Str(s) => Ok(OneBotBytes(
                base64::decode(&s).map_err(|_| WalleError::IllegalBase64(s))?,
            )),
            v => Err(WalleError::ValueTypeNotMatch(
                "bytes".to_string(),
                format!("{:?}", v),
            )),
        }
    }
}

impl TryFrom<Value> for () {
    type Error = WalleError;
    fn try_from(_: Value) -> Result<Self, Self::Error> {
        Ok(())
    }
}

macro_rules! downcast_fn {
    ($fname: ident, $ty: ty) => {
        pub fn $fname(self) -> WalleResult<$ty> {
            self.downcast()
        }
    };
}

#[allow(dead_code)]
impl Value {
    pub fn downcast<T>(self) -> WalleResult<T>
    where
        T: TryFrom<Value, Error = WalleError>,
    {
        self.try_into()
    }

    downcast_fn!(downcast_str, String);
    downcast_fn!(downcast_int, i64);
    downcast_fn!(downcast_f64, f64);
    downcast_fn!(downcast_bool, bool);
    downcast_fn!(downcast_bytes, OneBotBytes);

    pub fn downcast_map(self) -> WalleResult<HashMap<String, Value>> {
        if let Self::Map(m) = self {
            Ok(m)
        } else {
            Err(WalleError::ValueTypeNotMatch(
                "ExtendedMap".to_string(),
                format!("{:?}", self),
            ))
        }
    }

    pub fn downcast_list(self) -> WalleResult<Vec<Value>> {
        if let Self::List(l) = self {
            Ok(l)
        } else {
            Err(WalleError::ValueTypeNotMatch(
                "ExtendedList".to_string(),
                format!("{:?}", self),
            ))
        }
    }

    pub fn try_as_ref<'a, T>(&'a self) -> WalleResult<T>
    where
        Self: TryAsRef<'a, T>,
        T: 'a,
    {
        self._try_as_ref()
    }

    pub fn try_as_mut<'a, T>(&'a mut self) -> WalleResult<T>
    where
        Self: TryAsMut<'a, T>,
        T: 'a,
    {
        self._try_as_mut()
    }

    pub fn is_str(&self) -> bool {
        matches!(self, Self::Str(_))
    }
    pub fn is_f64(&self) -> bool {
        matches!(self, Self::F64(_))
    }
    pub fn is_int(&self) -> bool {
        matches!(self, Self::Int(_))
    }
    pub fn is_bool(&self) -> bool {
        matches!(self, Self::Bool(_))
    }
    pub fn is_bytes(&self) -> bool {
        matches!(self, Self::Bytes(_))
    }
    pub fn is_map(&self) -> bool {
        matches!(self, Self::Map(_))
    }
    pub fn is_list(&self) -> bool {
        matches!(self, Self::List(_))
    }
    pub fn is_null(&self) -> bool {
        matches!(self, Self::Null)
    }
}

pub trait PushToValueMap {
    fn push_to(self, map: &mut ValueMap);
}

impl PushToValueMap for () {
    fn push_to(self, _map: &mut ValueMap) {}
}

pub trait ValueMapExt {
    fn try_remove_downcast<T>(&mut self, key: &str) -> Result<Option<T>, WalleError>
    where
        T: TryFrom<Value, Error = WalleError>;
    fn remove_downcast<T>(&mut self, key: &str) -> Result<T, WalleError>
    where
        T: TryFrom<Value, Error = WalleError>;
    fn try_get_downcast<T>(&self, key: &str) -> Result<Option<T>, WalleError>
    where
        T: TryFrom<Value, Error = WalleError>;
    fn get_downcast<T>(&self, key: &str) -> Result<T, WalleError>
    where
        T: TryFrom<Value, Error = WalleError>;
    fn try_get_as_ref<'a, T>(&'a self, key: &str) -> Result<T, WalleError>
    where
        Value: TryAsRef<'a, T>,
        T: 'a;
    fn try_get_as_mut<'a, T>(&'a mut self, key: &str) -> Result<T, WalleError>
    where
        Value: TryAsMut<'a, T>,
        T: 'a;
    fn push<T>(&mut self, value: T)
    where
        T: PushToValueMap;
}

impl ValueMapExt for ValueMap {
    fn try_remove_downcast<T>(&mut self, key: &str) -> Result<Option<T>, WalleError>
    where
        T: TryFrom<Value, Error = WalleError>,
    {
        self.remove(key)
            .and_then(|v| match v {
                Value::Null => None,
                v => Some(v.try_into().map_err(|v| {
                    let msg = format!("{:?}", v);
                    WalleError::ValueTypeNotMatch(std::any::type_name::<T>().to_string(), msg)
                })),
            })
            .transpose()
    }
    fn remove_downcast<T>(&mut self, key: &str) -> Result<T, WalleError>
    where
        T: TryFrom<Value, Error = WalleError>,
    {
        self.try_remove_downcast(key)
            .and_then(|v| v.ok_or_else(|| WalleError::MapMissedKey(key.to_string())))
    }
    fn try_get_downcast<T>(&self, key: &str) -> Result<Option<T>, WalleError>
    where
        T: TryFrom<Value, Error = WalleError>,
    {
        self.get(key)
            .and_then(|v| match v {
                Value::Null => None,
                v => Some(v.clone().try_into().map_err(|v| {
                    let msg = format!("{:?}", v);
                    WalleError::ValueTypeNotMatch(std::any::type_name::<T>().to_string(), msg)
                })),
            })
            .transpose()
    }
    fn get_downcast<T>(&self, key: &str) -> Result<T, WalleError>
    where
        T: TryFrom<Value, Error = WalleError>,
    {
        self.try_get_downcast(key)
            .and_then(|v| v.ok_or_else(|| WalleError::MapMissedKey(key.to_string())))
    }
    fn try_get_as_ref<'a, T>(&'a self, key: &str) -> Result<T, WalleError>
    where
        Value: TryAsRef<'a, T>,
        T: 'a,
    {
        match self.get(key) {
            Some(v) => v._try_as_ref(),
            None => Err(WalleError::MapMissedKey(key.to_owned())),
        }
    }
    fn try_get_as_mut<'a, T>(&'a mut self, key: &str) -> Result<T, WalleError>
    where
        Value: TryAsMut<'a, T>,
        T: 'a,
    {
        match self.get_mut(key) {
            Some(v) => v._try_as_mut(),
            None => Err(WalleError::MapMissedKey(key.to_owned())),
        }
    }
    fn push<T>(&mut self, value: T)
    where
        T: PushToValueMap,
    {
        value.push_to(self)
    }
}

macro_rules! ref_impl {
    ($t: ty, $mt: ty, $subt: tt, $s: expr) => {
        impl<'a> TryAsRef<'a, $t> for Value {
            fn _try_as_ref(&'a self) -> WalleResult<$t> {
                match self {
                    Self::$subt(r) => Ok(r),
                    _ => Err(WalleError::ValueTypeNotMatch(
                        $s.to_owned(),
                        format!("{:?}", self),
                    )),
                }
            }
        }
        impl<'a> TryAsMut<'a, $mt> for Value {
            fn _try_as_mut(&'a mut self) -> WalleResult<$mt> {
                match self {
                    Self::$subt(r) => Ok(r),
                    _ => Err(WalleError::ValueTypeNotMatch(
                        $s.to_owned(),
                        format!("{:?}", self),
                    )),
                }
            }
        }
    };
}

impl<'a> TryAsRef<'a, &'a str> for Value {
    fn _try_as_ref(&'a self) -> WalleResult<&'a str> {
        match self {
            Self::Str(s) => Ok(s),
            _ => Err(WalleError::ValueTypeNotMatch(
                "str".to_string(),
                format!("{:?}", self),
            )),
        }
    }
}

impl<'a> TryAsMut<'a, &'a mut String> for Value {
    fn _try_as_mut(&'a mut self) -> WalleResult<&'a mut String> {
        match self {
            Self::Str(s) => Ok(s),
            _ => Err(WalleError::ValueTypeNotMatch(
                "str".to_string(),
                format!("{:?}", self),
            )),
        }
    }
}

ref_impl!(&'a i64, &'a mut i64, Int, "i64");
ref_impl!(&'a f64, &'a mut f64, F64, "f64");
ref_impl!(&'a bool, &'a mut bool, Bool, "bool");
ref_impl!(&'a ValueMap, &'a mut ValueMap, Map, "map");
ref_impl!(&'a Vec<Value>, &'a mut Vec<Value>, List, "list");
ref_impl!(&'a OneBotBytes, &'a mut OneBotBytes, Bytes, "bytes");