citadel_types/user/
mod.rs

1use serde::{Deserialize, Serialize};
2use std::hash::Hasher;
3use uuid::Uuid;
4
5/// This is to replace a tuple for greater organization
6#[derive(Serialize, Deserialize, Clone, Debug)]
7pub struct MutualPeer {
8    /// The interserver cid to which `cid` belongs to
9    pub parent_icid: u64,
10    /// the client to which belongs within `parent_icid`
11    pub cid: u64,
12    /// The username of this peer
13    pub username: Option<String>,
14}
15
16/// Contains info about a peer, used for giving the user access to usernames and names of peers
17#[derive(Serialize, Deserialize, Clone, Debug)]
18pub struct PeerInfo {
19    /// the client to which belongs within `parent_icid`
20    pub cid: u64,
21    /// The username of this peer
22    pub username: String,
23    /// The full name of this peer
24    pub full_name: String,
25}
26
27impl PartialEq for MutualPeer {
28    fn eq(&self, other: &Self) -> bool {
29        self.parent_icid == other.parent_icid
30            && self.cid == other.cid
31            && self.username.as_ref() == other.username.as_ref()
32    }
33}
34
35/// Generates a CID given a username
36pub fn username_to_cid(username: &str) -> u64 {
37    let mut hasher = twox_hash::XxHash64::default();
38    hasher.write(username.as_bytes());
39    hasher.finish()
40}
41
42/// A convenience wrapper for passing arguments to functions that require searches for a user
43#[derive(Debug, Clone, Serialize, Deserialize)]
44pub enum UserIdentifier {
45    /// Raw user ID
46    ID(u64),
47    /// Username connected by an unspecified ID
48    Username(String),
49}
50
51impl From<String> for UserIdentifier {
52    fn from(username: String) -> Self {
53        Self::Username(username)
54    }
55}
56
57impl From<&str> for UserIdentifier {
58    fn from(username: &str) -> Self {
59        Self::Username(username.to_string())
60    }
61}
62
63impl From<u64> for UserIdentifier {
64    fn from(cid: u64) -> Self {
65        Self::ID(cid)
66    }
67}
68
69impl From<Uuid> for UserIdentifier {
70    fn from(uuid: Uuid) -> Self {
71        Self::Username(uuid.to_string())
72    }
73}