core_crypto/mls/client/
id.rs1use crate::CryptoError;
18
19#[derive(Debug, Clone, PartialEq, Eq, Hash, derive_more::Deref)]
25pub struct ClientId(pub(crate) Vec<u8>);
26
27impl From<&[u8]> for ClientId {
28 fn from(value: &[u8]) -> Self {
29 Self(value.into())
30 }
31}
32
33impl From<Vec<u8>> for ClientId {
34 fn from(value: Vec<u8>) -> Self {
35 Self(value)
36 }
37}
38
39impl From<Box<[u8]>> for ClientId {
40 fn from(value: Box<[u8]>) -> Self {
41 Self(value.into())
42 }
43}
44
45impl From<ClientId> for Box<[u8]> {
46 fn from(value: ClientId) -> Self {
47 value.0.into_boxed_slice()
48 }
49}
50
51#[cfg(test)]
52impl From<&str> for ClientId {
53 fn from(value: &str) -> Self {
54 Self(value.as_bytes().into())
55 }
56}
57
58#[allow(clippy::from_over_into)]
59impl Into<Vec<u8>> for ClientId {
60 fn into(self) -> Vec<u8> {
61 self.0
62 }
63}
64
65impl std::fmt::Display for ClientId {
66 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
67 write!(f, "{}", hex::encode(self.0.as_slice()))
68 }
69}
70
71impl std::str::FromStr for ClientId {
72 type Err = CryptoError;
73
74 fn from_str(s: &str) -> Result<Self, Self::Err> {
75 Ok(Self(
76 hex::decode(s).map_or_else(|_| s.as_bytes().to_vec(), std::convert::identity),
77 ))
78 }
79}