Skip to main content

gitfox_client/models/
user.rs

1use serde::{Deserialize, Serialize};
2
3/// The authenticated principal, as reported by `GET /api/v1/user`.
4///
5/// Every field is optional because self-hosted GitFox instances of different
6/// versions populate slightly different subsets, and `fx auth status` must not
7/// fail just because one of them is missing.
8#[derive(Debug, Clone, Default, Serialize, Deserialize)]
9pub struct User {
10    #[serde(default, alias = "uid", alias = "user_id")]
11    pub uid: Option<String>,
12    #[serde(default)]
13    pub email: Option<String>,
14    #[serde(default, alias = "display_name")]
15    pub display_name: Option<String>,
16    #[serde(default)]
17    pub admin: Option<bool>,
18}
19
20impl User {
21    /// The best available name for display purposes.
22    pub fn label(&self) -> String {
23        self.display_name
24            .clone()
25            .or_else(|| self.uid.clone())
26            .or_else(|| self.email.clone())
27            .unwrap_or_else(|| "(unknown)".to_string())
28    }
29}