cfx_rpc_cfx_types/
subscriber_id.rs

1use cfx_types::H64;
2use std::str;
3
4#[derive(Debug, Clone, Hash, Eq, PartialEq)]
5pub struct SubId(H64);
6
7impl str::FromStr for SubId {
8    type Err = String;
9
10    fn from_str(s: &str) -> Result<Self, Self::Err> {
11        if s.starts_with("0x") {
12            Ok(SubId(s[2..].parse().map_err(|e| format!("{}", e))?))
13        } else {
14            Err("The id must start with 0x".into())
15        }
16    }
17}
18impl SubId {
19    pub fn new(data: H64) -> Self { SubId(data) }
20
21    /// Generate a new random subscription id.
22    pub fn next() -> Self { SubId(H64(rand::random::<[u8; 8]>())) }
23
24    // TODO: replace `format!` see [#10412](https://github.com/paritytech/parity-ethereum/issues/10412)
25    pub fn as_string(&self) -> String { format!("{:?}", self.0) }
26}