core_crypto/mls/client/
id.rs

1// Wire
2// Copyright (C) 2022 Wire Swiss GmbH
3
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see http://www.gnu.org/licenses/.
16
17use crate::CryptoError;
18
19/// A Client identifier
20///
21/// A unique identifier for clients. A client is an identifier for each App a user is using, such as desktop,
22/// mobile, etc. Users can have multiple clients.
23/// More information [here](https://messaginglayersecurity.rocks/mls-architecture/draft-ietf-mls-architecture.html#name-group-members-and-clients)
24#[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}