abstract_std/objects/
namespace.rs

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
use std::fmt::Display;

use cosmwasm_std::StdResult;
use cw_storage_plus::{Key, KeyDeserialize, Prefixer, PrimaryKey};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use super::module::validate_name;
use crate::{AbstractError, AbstractResult};

pub const ABSTRACT_NAMESPACE: &str = "abstract";

/// Represents an Abstract namespace for modules
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct Namespace(String);

impl Namespace {
    pub fn new(namespace: &str) -> AbstractResult<Self> {
        validate_name(namespace)?;
        Ok(Self(namespace.to_owned()))
    }
    /// Create an instance without validating. Not for use in production code.
    pub fn unchecked(namespace: impl ToString) -> Self {
        Self(namespace.to_string())
    }
    pub fn as_str(&self) -> &str {
        &self.0
    }
    /// Check eq the namespace is valid
    pub fn validate(&self) -> AbstractResult<()> {
        validate_name(&self.0)?;
        Ok(())
    }
    /// Get the namespace from a module's ID
    /// Formatted as `namespace:module`
    pub fn from_id(module_id: &str) -> AbstractResult<Self> {
        let parts: Vec<&str> = module_id.split(':').collect();
        if parts.len() != 2 {
            return Err(AbstractError::FormattingError {
                object: "module_id".to_string(),
                expected: "namespace:module".to_string(),
                actual: module_id.to_string(),
            });
        }
        Self::new(parts[0])
    }
}

impl TryFrom<&str> for Namespace {
    type Error = AbstractError;

    fn try_from(namespace: &str) -> AbstractResult<Self> {
        Self::new(namespace)
    }
}

impl TryFrom<String> for Namespace {
    type Error = AbstractError;

    fn try_from(namespace: String) -> AbstractResult<Self> {
        Self::try_from(&namespace)
    }
}

impl TryFrom<&String> for Namespace {
    type Error = AbstractError;

    fn try_from(namespace: &String) -> AbstractResult<Self> {
        Self::new(namespace)
    }
}

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

impl KeyDeserialize for &Namespace {
    type Output = Namespace;
    const KEY_ELEMS: u16 = 1;

    #[inline(always)]
    fn from_vec(value: Vec<u8>) -> StdResult<Self::Output> {
        Ok(Namespace(String::from_vec(value)?))
    }
}

impl PrimaryKey<'_> for Namespace {
    type Prefix = ();

    type SubPrefix = ();

    type Suffix = Self;

    type SuperSuffix = Self;

    fn key(&self) -> Vec<cw_storage_plus::Key> {
        self.0.key()
    }
}

impl Prefixer<'_> for Namespace {
    fn prefix(&self) -> Vec<Key> {
        self.0.prefix()
    }
}

impl KeyDeserialize for Namespace {
    type Output = Namespace;
    const KEY_ELEMS: u16 = 1;

    #[inline(always)]
    fn from_vec(value: Vec<u8>) -> StdResult<Self::Output> {
        Ok(Namespace(String::from_vec(value)?))
    }
}

#[cfg(test)]
mod test {
    #![allow(clippy::needless_borrows_for_generic_args)]

    use super::*;

    #[coverage_helper::test]
    fn test_namespace() {
        let namespace = Namespace::new("test").unwrap();
        assert_eq!(namespace.as_str(), "test");
    }

    #[coverage_helper::test]
    fn test_from_string() {
        let namespace = Namespace::try_from("test".to_string()).unwrap();
        assert_eq!(namespace.as_str(), "test");
    }

    #[coverage_helper::test]
    fn test_from_str() {
        let namespace = Namespace::try_from("test").unwrap();
        assert_eq!(namespace.as_str(), "test");
    }

    #[coverage_helper::test]
    fn test_from_ref_string() {
        let namespace = Namespace::try_from(&"test".to_string()).unwrap();
        assert_eq!(namespace.as_str(), "test");
    }

    #[coverage_helper::test]
    fn test_to_string() {
        let namespace = Namespace::new("test").unwrap();
        assert_eq!(namespace.to_string(), "test".to_string());
    }

    #[coverage_helper::test]
    fn string_key_works() {
        let k = &Namespace::new("test").unwrap();
        let path = k.key();
        assert_eq!(1, path.len());
        assert_eq!(b"test", path[0].as_ref());

        let joined = k.joined_key();
        assert_eq!(joined, b"test")
    }
}