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
use std::{borrow::Cow, fmt, ops::Deref, str::FromStr};

use crate::{AccountIdRef, ParseAccountError};

/// NEAR Account Identifier.
///
/// This is a unique, syntactically valid, human-readable account identifier on the NEAR network.
///
/// [See the crate-level docs for information about validation.](index.html#account-id-rules)
///
/// Also see [Error kind precedence](AccountId#error-kind-precedence).
///
/// ## Examples
///
/// ```
/// use near_account_id::AccountId;
///
/// let alice: AccountId = "alice.near".parse().unwrap();
///
/// assert!("ƒelicia.near".parse::<AccountId>().is_err()); // (ƒ is not f)
/// ```
#[derive(Eq, Ord, Hash, Clone, Debug, PartialEq, PartialOrd)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "abi", derive(borsh::BorshSchema))]
pub struct AccountId(pub(crate) Box<str>);

impl AccountId {
    /// Shortest valid length for a NEAR Account ID.
    pub const MIN_LEN: usize = crate::validation::MIN_LEN;
    /// Longest valid length for a NEAR Account ID.
    pub const MAX_LEN: usize = crate::validation::MAX_LEN;

    /// Creates an `AccountId` without any validation checks.
    ///
    /// Please note that this is restrictively for internal use only. Plus, being behind a feature flag,
    /// this could be removed later in the future.
    ///
    /// ## Safety
    ///
    /// Since this skips validation and constructs an `AccountId` regardless,
    /// the caller bears the responsibility of ensuring that the Account ID is valid.
    /// You can use the [`AccountId::validate`] function sometime after its creation but before it's use.
    ///
    /// ## Examples
    ///
    /// ```
    /// use near_account_id::AccountId;
    ///
    /// let alice = AccountId::new_unvalidated("alice.near".to_string());
    /// assert!(AccountId::validate(alice.as_str()).is_ok());
    /// ```
    #[doc(hidden)]
    #[cfg(feature = "internal_unstable")]
    #[deprecated = "AccountId construction without validation is illegal since nearcore#4440"]
    pub fn new_unvalidated(account_id: String) -> Self {
        Self(account_id.into_boxed_str())
    }

    /// Validates a string as a well-structured NEAR Account ID.
    ///
    /// Checks Account ID validity without constructing an `AccountId` instance.
    ///
    /// ## Examples
    ///
    /// ```
    /// use near_account_id::{AccountId, ParseErrorKind};
    ///
    /// assert!(AccountId::validate("alice.near").is_ok());
    ///
    /// assert!(
    ///   matches!(
    ///     AccountId::validate("ƒelicia.near"), // fancy ƒ!
    ///     Err(err) if err.kind() == &ParseErrorKind::InvalidChar
    ///   )
    /// );
    /// ```
    ///
    /// ## Error kind precedence
    ///
    /// If an Account ID has multiple format violations, the first one would be reported.
    ///
    /// ### Examples
    ///
    /// ```
    /// use near_account_id::{AccountId, ParseErrorKind};
    ///
    /// assert!(
    ///   matches!(
    ///     AccountId::validate("A__ƒƒluent."),
    ///     Err(err) if err.kind() == &ParseErrorKind::InvalidChar
    ///   )
    /// );
    ///
    /// assert!(
    ///   matches!(
    ///     AccountId::validate("a__ƒƒluent."),
    ///     Err(err) if err.kind() == &ParseErrorKind::RedundantSeparator
    ///   )
    /// );
    ///
    /// assert!(
    ///   matches!(
    ///     AccountId::validate("aƒƒluent."),
    ///     Err(err) if err.kind() == &ParseErrorKind::InvalidChar
    ///   )
    /// );
    ///
    /// assert!(
    ///   matches!(
    ///     AccountId::validate("affluent."),
    ///     Err(err) if err.kind() == &ParseErrorKind::RedundantSeparator
    ///   )
    /// );
    /// ```
    pub fn validate(account_id: &str) -> Result<(), ParseAccountError> {
        crate::validation::validate(account_id)
    }
}

impl AsRef<str> for AccountId {
    fn as_ref(&self) -> &str {
        &self.0
    }
}

impl AsRef<AccountIdRef> for AccountId {
    fn as_ref(&self) -> &AccountIdRef {
        self
    }
}

impl Deref for AccountId {
    type Target = AccountIdRef;

    fn deref(&self) -> &Self::Target {
        AccountIdRef::new_unvalidated(&self.0)
    }
}

impl std::borrow::Borrow<AccountIdRef> for AccountId {
    fn borrow(&self) -> &AccountIdRef {
        AccountIdRef::new_unvalidated(self)
    }
}

impl FromStr for AccountId {
    type Err = ParseAccountError;

    fn from_str(account_id: &str) -> Result<Self, Self::Err> {
        crate::validation::validate(account_id)?;
        Ok(Self(account_id.into()))
    }
}

impl TryFrom<Box<str>> for AccountId {
    type Error = ParseAccountError;

    fn try_from(account_id: Box<str>) -> Result<Self, Self::Error> {
        crate::validation::validate(&account_id)?;
        Ok(Self(account_id))
    }
}

impl TryFrom<String> for AccountId {
    type Error = ParseAccountError;

    fn try_from(account_id: String) -> Result<Self, Self::Error> {
        crate::validation::validate(&account_id)?;
        Ok(Self(account_id.into_boxed_str()))
    }
}

impl fmt::Display for AccountId {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        fmt::Display::fmt(&self.0, f)
    }
}

impl From<AccountId> for String {
    fn from(account_id: AccountId) -> Self {
        account_id.0.into_string()
    }
}

impl From<AccountId> for Box<str> {
    fn from(value: AccountId) -> Box<str> {
        value.0
    }
}

impl PartialEq<AccountId> for AccountIdRef {
    fn eq(&self, other: &AccountId) -> bool {
        &self.0 == other.as_str()
    }
}

impl PartialEq<AccountIdRef> for AccountId {
    fn eq(&self, other: &AccountIdRef) -> bool {
        self.as_str() == &other.0
    }
}

impl<'a> PartialEq<AccountId> for &'a AccountIdRef {
    fn eq(&self, other: &AccountId) -> bool {
        &self.0 == other.as_str()
    }
}

impl<'a> PartialEq<&'a AccountIdRef> for AccountId {
    fn eq(&self, other: &&'a AccountIdRef) -> bool {
        self.as_str() == &other.0
    }
}

impl PartialEq<AccountId> for String {
    fn eq(&self, other: &AccountId) -> bool {
        self == other.as_str()
    }
}

impl PartialEq<String> for AccountId {
    fn eq(&self, other: &String) -> bool {
        self.as_str() == other
    }
}

impl PartialEq<AccountId> for str {
    fn eq(&self, other: &AccountId) -> bool {
        self == other.as_str()
    }
}

impl PartialEq<str> for AccountId {
    fn eq(&self, other: &str) -> bool {
        self.as_str() == other
    }
}

impl<'a> PartialEq<AccountId> for &'a str {
    fn eq(&self, other: &AccountId) -> bool {
        *self == other.as_str()
    }
}

impl<'a> PartialEq<&'a str> for AccountId {
    fn eq(&self, other: &&'a str) -> bool {
        self.as_str() == *other
    }
}

impl PartialOrd<AccountId> for AccountIdRef {
    fn partial_cmp(&self, other: &AccountId) -> Option<std::cmp::Ordering> {
        self.0.partial_cmp(other.as_str())
    }
}

impl PartialOrd<AccountIdRef> for AccountId {
    fn partial_cmp(&self, other: &AccountIdRef) -> Option<std::cmp::Ordering> {
        self.as_str().partial_cmp(&other.0)
    }
}

impl<'a> PartialOrd<AccountId> for &'a AccountIdRef {
    fn partial_cmp(&self, other: &AccountId) -> Option<std::cmp::Ordering> {
        self.0.partial_cmp(other.as_str())
    }
}

impl<'a> PartialOrd<&'a AccountIdRef> for AccountId {
    fn partial_cmp(&self, other: &&'a AccountIdRef) -> Option<std::cmp::Ordering> {
        self.as_str().partial_cmp(&other.0)
    }
}

impl PartialOrd<AccountId> for String {
    fn partial_cmp(&self, other: &AccountId) -> Option<std::cmp::Ordering> {
        self.as_str().partial_cmp(other.as_str())
    }
}

impl PartialOrd<String> for AccountId {
    fn partial_cmp(&self, other: &String) -> Option<std::cmp::Ordering> {
        self.as_str().partial_cmp(other.as_str())
    }
}

impl PartialOrd<AccountId> for str {
    fn partial_cmp(&self, other: &AccountId) -> Option<std::cmp::Ordering> {
        self.partial_cmp(other.as_str())
    }
}

impl PartialOrd<str> for AccountId {
    fn partial_cmp(&self, other: &str) -> Option<std::cmp::Ordering> {
        self.as_str().partial_cmp(other)
    }
}

impl<'a> PartialOrd<AccountId> for &'a str {
    fn partial_cmp(&self, other: &AccountId) -> Option<std::cmp::Ordering> {
        self.partial_cmp(&other.as_str())
    }
}

impl<'a> PartialOrd<&'a str> for AccountId {
    fn partial_cmp(&self, other: &&'a str) -> Option<std::cmp::Ordering> {
        self.as_str().partial_cmp(*other)
    }
}

impl<'a> From<AccountId> for Cow<'a, AccountIdRef> {
    fn from(value: AccountId) -> Self {
        Cow::Owned(value)
    }
}

impl<'a> From<&'a AccountId> for Cow<'a, AccountIdRef> {
    fn from(value: &'a AccountId) -> Self {
        Cow::Borrowed(value)
    }
}

impl<'a> From<Cow<'a, AccountIdRef>> for AccountId {
    fn from(value: Cow<'a, AccountIdRef>) -> Self {
        value.into_owned()
    }
}

#[cfg(feature = "arbitrary")]
impl<'a> arbitrary::Arbitrary<'a> for AccountId {
    fn size_hint(depth: usize) -> (usize, Option<usize>) {
        <&AccountIdRef as arbitrary::Arbitrary>::size_hint(depth)
    }

    fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
        Ok(u.arbitrary::<&AccountIdRef>()?.into())
    }

    fn arbitrary_take_rest(u: arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
        Ok(<&AccountIdRef as arbitrary::Arbitrary>::arbitrary_take_rest(u)?.into())
    }
}

#[cfg(test)]
mod tests {
    #[allow(unused_imports)]
    use super::*;

    #[test]
    #[cfg(feature = "arbitrary")]
    fn test_arbitrary() {
        let corpus = [
            ("a|bcd", None),
            ("ab|cde", Some("ab")),
            ("a_-b", None),
            ("ab_-c", Some("ab")),
            ("a", None),
            ("miraclx.near", Some("miraclx.near")),
            (
                "01234567890123456789012345678901234567890123456789012345678901234",
                None,
            ),
        ];

        for (input, expected_output) in corpus {
            assert!(input.len() <= u8::MAX as usize);
            let data = [input.as_bytes(), &[input.len() as _]].concat();
            let mut u = arbitrary::Unstructured::new(&data);

            assert_eq!(
                u.arbitrary::<AccountId>().map(Into::<String>::into).ok(),
                expected_output.map(Into::<String>::into)
            );
        }
    }
    #[test]
    #[cfg(feature = "schemars")]
    fn test_schemars() {
        let schema = schemars::schema_for!(AccountId);
        let json_schema = serde_json::to_value(&schema).unwrap();
        dbg!(&json_schema);
        assert_eq!(
            json_schema,
            serde_json::json!({
                    "$schema": "http://json-schema.org/draft-07/schema#",
                    "description": "NEAR Account Identifier.\n\nThis is a unique, syntactically valid, human-readable account identifier on the NEAR network.\n\n[See the crate-level docs for information about validation.](index.html#account-id-rules)\n\nAlso see [Error kind precedence](AccountId#error-kind-precedence).\n\n## Examples\n\n``` use near_account_id::AccountId;\n\nlet alice: AccountId = \"alice.near\".parse().unwrap();\n\nassert!(\"ƒelicia.near\".parse::<AccountId>().is_err()); // (ƒ is not f) ```",
                    "title": "AccountId",
                    "type": "string"
                }
            )
        );
    }
}