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
use crate::compat::rand::distributions::{Distribution, Standard};
use crate::compat::rand::Rng;
use crate::compat::string::{String, ToString};
use core::fmt;
use core::fmt::Formatter;
use minicbor::{Decode, Encode};
use serde::{Deserialize, Serialize};

/// Unique random identifier of a Flow Control
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize, Decode, Encode)]
#[rustfmt::skip]
#[cbor(map)]
pub struct FlowControlId {
    #[n(1)] id: String
}

impl FlowControlId {
    /// Constructor
    fn new(str: &str) -> Self {
        Self {
            id: str.to_string(),
        }
    }
}

impl fmt::Debug for FlowControlId {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(self, f)
    }
}

impl fmt::Display for FlowControlId {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        f.write_str(&self.id)
    }
}

impl Distribution<FlowControlId> for Standard {
    fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> FlowControlId {
        let data: [u8; 16] = rng.gen();
        FlowControlId::new(&hex::encode(data))
    }
}

impl From<String> for FlowControlId {
    fn from(value: String) -> Self {
        Self { id: value }
    }
}