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
//! Wrapper type for serializing Move module IDs as strings.

use move_core_types::{
    language_storage::{ModuleId, StructTag},
    parser::parse_struct_tag,
};
use serde::{Deserialize, Serialize, Serializer};
use std::fmt::Display;

/// Wrapper around [ModuleId] which can be serialized to JSON.
#[derive(Debug, PartialEq, Hash, Eq, Clone, PartialOrd, Ord)]
pub struct ModuleIdData(ModuleId);

impl Display for ModuleIdData {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.0.fmt(f)
    }
}

impl Serialize for ModuleIdData {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(&self.0.short_str_lossless())
    }
}

impl From<&ModuleId> for ModuleIdData {
    fn from(id: &ModuleId) -> Self {
        Self(id.clone())
    }
}

impl From<ModuleId> for ModuleIdData {
    fn from(id: ModuleId) -> Self {
        Self(id)
    }
}

impl From<&ModuleIdData> for ModuleId {
    fn from(id: &ModuleIdData) -> Self {
        id.0.clone()
    }
}

impl From<ModuleIdData> for ModuleId {
    fn from(id: ModuleIdData) -> Self {
        id.0
    }
}

impl<'de> Deserialize<'de> for ModuleIdData {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let s = String::deserialize(deserializer)?;
        let tt: StructTag =
            parse_struct_tag(&format!("{}::Dummy", &s)).map_err(serde::de::Error::custom)?;
        Ok(ModuleIdData(tt.module_id()))
    }
}

#[cfg(test)]
mod tests {
    use move_core_types::parser::parse_struct_tag;

    use crate::ModuleIdData;

    #[test]
    fn test_serde() {
        let my_module_id = ModuleIdData(parse_struct_tag("0x1::A::B").unwrap().module_id());

        let ser = serde_json::to_string(&my_module_id).unwrap();
        let des: ModuleIdData = serde_json::from_str(&ser).unwrap();

        println!("{}", &des);
    }
}