nostr 0.45.0

Rust implementation of the Nostr protocol.
Documentation
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
use core::cmp::Ordering;
use core::fmt;
use core::str::FromStr;

use serde::{Deserialize, Deserializer, Serialize, Serializer};

use crate::error::{Error, ErrorKind};

#[inline]
const fn invalid_len() -> Error {
    Error::with_static_message(
        ErrorKind::Invalid,
        "expected exactly one ASCII alphabetic character",
    )
}

#[inline]
const fn non_ascii_alphabetic_char() -> Error {
    Error::with_static_message(
        ErrorKind::Invalid,
        "expected an ASCII alphabetic character (`a-z` or `A-Z`)",
    )
}

/// A validated single-letter ASCII tag.
///
/// A `SingleLetterTag` can contain exactly one of the following bytes:
///
/// - `b'a'..=b'z'`
/// - `b'A'..=b'Z'`
///
/// The type stores the original character case.
///
/// # Representation
///
/// The value is stored internally as a single [`u8`]. Because the type is
/// `#[repr(transparent)]`, it has the same layout and alignment as `u8`.
///
/// # Invariants
///
/// The inner byte is always an ASCII alphabetic character. This invariant is
/// maintained by keeping the field private and validating every public
/// constructor.
///
/// # Ordering
///
/// ```text
/// a < A < b < B < ... < z < Z
/// ```
///
/// In other words, values are ordered alphabetically without regard to case,
/// with the lowercase variant before the uppercase variant of the same letter.
#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct SingleLetterTag(u8);

impl SingleLetterTag {
    /// The lowercase letter `a`.
    pub const LOWERCASE_A: Self = Self(b'a');

    /// The lowercase letter `b`.
    pub const LOWERCASE_B: Self = Self(b'b');

    /// The lowercase letter `c`.
    pub const LOWERCASE_C: Self = Self(b'c');

    /// The lowercase letter `d`.
    pub const LOWERCASE_D: Self = Self(b'd');

    /// The lowercase letter `e`.
    pub const LOWERCASE_E: Self = Self(b'e');

    /// The lowercase letter `f`.
    pub const LOWERCASE_F: Self = Self(b'f');

    /// The lowercase letter `g`.
    pub const LOWERCASE_G: Self = Self(b'g');

    /// The lowercase letter `h`.
    pub const LOWERCASE_H: Self = Self(b'h');

    /// The lowercase letter `i`.
    pub const LOWERCASE_I: Self = Self(b'i');

    /// The lowercase letter `j`.
    pub const LOWERCASE_J: Self = Self(b'j');

    /// The lowercase letter `k`.
    pub const LOWERCASE_K: Self = Self(b'k');

    /// The lowercase letter `l`.
    pub const LOWERCASE_L: Self = Self(b'l');

    /// The lowercase letter `m`.
    pub const LOWERCASE_M: Self = Self(b'm');

    /// The lowercase letter `n`.
    pub const LOWERCASE_N: Self = Self(b'n');

    /// The lowercase letter `o`.
    pub const LOWERCASE_O: Self = Self(b'o');

    /// The lowercase letter `p`.
    pub const LOWERCASE_P: Self = Self(b'p');

    /// The lowercase letter `q`.
    pub const LOWERCASE_Q: Self = Self(b'q');

    /// The lowercase letter `r`.
    pub const LOWERCASE_R: Self = Self(b'r');

    /// The lowercase letter `s`.
    pub const LOWERCASE_S: Self = Self(b's');

    /// The lowercase letter `t`.
    pub const LOWERCASE_T: Self = Self(b't');

    /// The lowercase letter `u`.
    pub const LOWERCASE_U: Self = Self(b'u');

    /// The lowercase letter `v`.
    pub const LOWERCASE_V: Self = Self(b'v');

    /// The lowercase letter `w`.
    pub const LOWERCASE_W: Self = Self(b'w');

    /// The lowercase letter `x`.
    pub const LOWERCASE_X: Self = Self(b'x');

    /// The lowercase letter `y`.
    pub const LOWERCASE_Y: Self = Self(b'y');

    /// The lowercase letter `z`.
    pub const LOWERCASE_Z: Self = Self(b'z');

    /// The uppercase letter `A`.
    pub const UPPERCASE_A: Self = Self(b'A');

    /// The uppercase letter `B`.
    pub const UPPERCASE_B: Self = Self(b'B');

    /// The uppercase letter `C`.
    pub const UPPERCASE_C: Self = Self(b'C');

    /// The uppercase letter `D`.
    pub const UPPERCASE_D: Self = Self(b'D');

    /// The uppercase letter `E`.
    pub const UPPERCASE_E: Self = Self(b'E');

    /// The uppercase letter `F`.
    pub const UPPERCASE_F: Self = Self(b'F');

    /// The uppercase letter `G`.
    pub const UPPERCASE_G: Self = Self(b'G');

    /// The uppercase letter `H`.
    pub const UPPERCASE_H: Self = Self(b'H');

    /// The uppercase letter `I`.
    pub const UPPERCASE_I: Self = Self(b'I');

    /// The uppercase letter `J`.
    pub const UPPERCASE_J: Self = Self(b'J');

    /// The uppercase letter `K`.
    pub const UPPERCASE_K: Self = Self(b'K');

    /// The uppercase letter `L`.
    pub const UPPERCASE_L: Self = Self(b'L');

    /// The uppercase letter `M`.
    pub const UPPERCASE_M: Self = Self(b'M');

    /// The uppercase letter `N`.
    pub const UPPERCASE_N: Self = Self(b'N');

    /// The uppercase letter `O`.
    pub const UPPERCASE_O: Self = Self(b'O');

    /// The uppercase letter `P`.
    pub const UPPERCASE_P: Self = Self(b'P');

    /// The uppercase letter `Q`.
    pub const UPPERCASE_Q: Self = Self(b'Q');

    /// The uppercase letter `R`.
    pub const UPPERCASE_R: Self = Self(b'R');

    /// The uppercase letter `S`.
    pub const UPPERCASE_S: Self = Self(b'S');

    /// The uppercase letter `T`.
    pub const UPPERCASE_T: Self = Self(b'T');

    /// The uppercase letter `U`.
    pub const UPPERCASE_U: Self = Self(b'U');

    /// The uppercase letter `V`.
    pub const UPPERCASE_V: Self = Self(b'V');

    /// The uppercase letter `W`.
    pub const UPPERCASE_W: Self = Self(b'W');

    /// The uppercase letter `X`.
    pub const UPPERCASE_X: Self = Self(b'X');

    /// The uppercase letter `Y`.
    pub const UPPERCASE_Y: Self = Self(b'Y');

    /// The uppercase letter `Z`.
    pub const UPPERCASE_Z: Self = Self(b'Z');

    /// Creates a tag from an ASCII alphabetic `char`.
    ///
    /// The original character case is preserved.
    ///
    /// # Errors
    ///
    /// Returns an error when `character` is not in `a-zA-Z`.
    ///
    /// # Examples
    ///
    /// ```
    /// # use nostr::filter::SingleLetterTag;
    /// assert_eq!(
    ///     SingleLetterTag::from_char('a'),
    ///     Ok(SingleLetterTag::LOWERCASE_A),
    /// );
    /// assert_eq!(
    ///     SingleLetterTag::from_char('A'),
    ///     Ok(SingleLetterTag::UPPERCASE_A),
    /// );
    /// assert!(SingleLetterTag::from_char('1').is_err());
    /// ```
    #[inline]
    pub const fn from_char(character: char) -> Result<Self, Error> {
        Self::from_byte(character as u8)
    }

    /// Creates a tag from an ASCII alphabetic byte.
    ///
    /// The original character case is preserved.
    ///
    /// # Errors
    ///
    /// Returns an error when `byte`
    /// is not in `b'a'..=b'z'` or `b'A'..=b'Z'`.
    #[inline]
    pub const fn from_byte(byte: u8) -> Result<Self, Error> {
        if byte.is_ascii_alphabetic() {
            Ok(Self(byte))
        } else {
            Err(non_ascii_alphabetic_char())
        }
    }

    /// Returns the underlying ASCII byte.
    #[inline]
    pub const fn as_byte(self) -> u8 {
        self.0
    }

    /// Returns the tag as a `char`.
    #[inline]
    pub const fn as_char(self) -> char {
        self.0 as char
    }

    /// Returns the tag as a one-byte string slice.
    ///
    /// This method does not allocate.
    #[inline]
    pub const fn as_str(&self) -> &str {
        // SAFETY:
        //
        // The private field is initialized only by:
        //
        // - the associated constants, all of which contain ASCII letters;
        // - `from_char`, which validates `is_ascii_alphabetic`;
        // - `from_byte`, which validates `is_ascii_alphabetic`;
        // - the case-normalizing constructors, which validate their input.
        //
        // Every ASCII byte is valid UTF-8.
        unsafe { core::str::from_utf8_unchecked(core::slice::from_ref(&self.0)) }
    }

    /// Returns `true` when the tag contains a lowercase ASCII letter.
    #[inline]
    pub const fn is_lowercase(self) -> bool {
        self.0.is_ascii_lowercase()
    }

    /// Returns `true` when the tag contains an uppercase ASCII letter.
    #[inline]
    pub const fn is_uppercase(self) -> bool {
        self.0.is_ascii_uppercase()
    }

    /// Returns the lowercase form of this tag.
    ///
    /// This method does not modify `self`.
    #[inline]
    #[must_use]
    pub const fn to_lowercase(self) -> Self {
        Self(self.0.to_ascii_lowercase())
    }

    /// Returns the uppercase form of this tag.
    ///
    /// This method does not modify `self`.
    #[inline]
    #[must_use]
    pub const fn to_uppercase(self) -> Self {
        Self(self.0.to_ascii_uppercase())
    }

    /// Compares two tags without considering ASCII letter case.
    #[inline]
    pub const fn eq_ignore_case(self, other: Self) -> bool {
        self.0.eq_ignore_ascii_case(&other.0)
    }

    /// Produces the key used by the [`Ord`] implementation.
    ///
    /// The first element represents the case-insensitive letter. The second
    /// element is `false` for lowercase and `true` for uppercase.
    #[inline]
    const fn ordering_key(self) -> (u8, bool) {
        (self.0.to_ascii_lowercase(), self.0.is_ascii_uppercase())
    }
}

impl fmt::Debug for SingleLetterTag {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_tuple("SingleLetterTag")
            .field(&self.as_char())
            .finish()
    }
}

impl fmt::Display for SingleLetterTag {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(self.as_str())
    }
}

impl PartialOrd for SingleLetterTag {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for SingleLetterTag {
    fn cmp(&self, other: &Self) -> Ordering {
        self.ordering_key().cmp(&other.ordering_key())
    }
}

impl FromStr for SingleLetterTag {
    type Err = Error;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        match value.as_bytes() {
            &[byte] => Self::from_byte(byte),
            _ => Err(invalid_len()),
        }
    }
}

impl TryFrom<char> for SingleLetterTag {
    type Error = Error;

    #[inline]
    fn try_from(character: char) -> Result<Self, Self::Error> {
        Self::from_char(character)
    }
}

impl TryFrom<u8> for SingleLetterTag {
    type Error = Error;

    #[inline]
    fn try_from(byte: u8) -> Result<Self, Self::Error> {
        Self::from_byte(byte)
    }
}

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

    #[inline]
    fn try_from(value: &str) -> Result<Self, Self::Error> {
        value.parse()
    }
}

impl From<SingleLetterTag> for char {
    #[inline]
    fn from(tag: SingleLetterTag) -> Self {
        tag.as_char()
    }
}

impl From<SingleLetterTag> for u8 {
    #[inline]
    fn from(tag: SingleLetterTag) -> Self {
        tag.as_byte()
    }
}

impl AsRef<str> for SingleLetterTag {
    #[inline]
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

impl Serialize for SingleLetterTag {
    #[inline]
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_char(self.as_char())
    }
}

impl<'de> Deserialize<'de> for SingleLetterTag {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let character: char = char::deserialize(deserializer)?;
        Self::from_char(character).map_err(serde::de::Error::custom)
    }
}

#[cfg(test)]
mod tests {
    use alloc::string::ToString;

    use super::*;

    #[test]
    fn has_the_same_layout_as_u8() {
        assert_eq!(size_of::<SingleLetterTag>(), size_of::<u8>());
        assert_eq!(align_of::<SingleLetterTag>(), align_of::<u8>());
    }

    #[test]
    fn parses_valid_characters() {
        assert_eq!(
            SingleLetterTag::from_char('a'),
            Ok(SingleLetterTag::LOWERCASE_A),
        );
        assert_eq!(
            SingleLetterTag::from_char('Z'),
            Ok(SingleLetterTag::UPPERCASE_Z),
        );
    }

    #[test]
    fn rejects_invalid_characters() {
        assert_eq!(
            SingleLetterTag::from_char('1'),
            Err(non_ascii_alphabetic_char()),
        );
        assert_eq!(
            SingleLetterTag::from_char('é'),
            Err(non_ascii_alphabetic_char()),
        );
    }

    #[test]
    fn parses_valid_bytes() {
        assert_eq!(
            SingleLetterTag::from_byte(b'p'),
            Ok(SingleLetterTag::LOWERCASE_P),
        );
        assert_eq!(
            SingleLetterTag::from_byte(b'P'),
            Ok(SingleLetterTag::UPPERCASE_P),
        );
    }

    #[test]
    fn parses_valid_strings() {
        assert_eq!(
            "a".parse::<SingleLetterTag>(),
            Ok(SingleLetterTag::LOWERCASE_A),
        );
        assert_eq!(
            "Z".parse::<SingleLetterTag>(),
            Ok(SingleLetterTag::UPPERCASE_Z),
        );
    }

    #[test]
    fn rejects_invalid_strings() {
        assert_eq!("".parse::<SingleLetterTag>(), Err(invalid_len()));
        assert_eq!("ab".parse::<SingleLetterTag>(), Err(invalid_len()));
        assert_eq!("é".parse::<SingleLetterTag>(), Err(invalid_len()));
        assert_eq!(
            "1".parse::<SingleLetterTag>(),
            Err(non_ascii_alphabetic_char()),
        );
    }

    #[test]
    fn exposes_character_representations() {
        let tag = SingleLetterTag::LOWERCASE_P;

        assert_eq!(tag.as_byte(), b'p');
        assert_eq!(tag.as_char(), 'p');
        assert_eq!(tag.as_str(), "p");
        assert_eq!(tag.to_string(), "p");
    }

    #[test]
    fn normalizes_character_case() {
        assert_eq!(
            SingleLetterTag::UPPERCASE_P.to_lowercase(),
            SingleLetterTag::LOWERCASE_P,
        );
        assert_eq!(
            SingleLetterTag::LOWERCASE_P.to_uppercase(),
            SingleLetterTag::UPPERCASE_P,
        );
    }

    #[test]
    fn detects_character_case() {
        assert!(SingleLetterTag::LOWERCASE_A.is_lowercase());
        assert!(!SingleLetterTag::LOWERCASE_A.is_uppercase());

        assert!(SingleLetterTag::UPPERCASE_A.is_uppercase());
        assert!(!SingleLetterTag::UPPERCASE_A.is_lowercase());
    }

    #[test]
    fn compares_case_insensitively() {
        assert!(SingleLetterTag::LOWERCASE_A.eq_ignore_case(SingleLetterTag::UPPERCASE_A));
        assert!(!SingleLetterTag::LOWERCASE_A.eq_ignore_case(SingleLetterTag::UPPERCASE_B));
    }

    #[test]
    fn preserves_the_previous_ordering_semantics() {
        assert!(SingleLetterTag::LOWERCASE_A < SingleLetterTag::UPPERCASE_A);
        assert!(SingleLetterTag::UPPERCASE_A < SingleLetterTag::LOWERCASE_B);
    }

    #[test]
    fn debug_output_contains_the_character() {
        assert_eq!(
            format!("{:?}", SingleLetterTag::LOWERCASE_P),
            "SingleLetterTag('p')",
        );
    }
}