notedeck

One damus client to rule them all
git clone git://jb55.com/notedeck
Log | Files | Refs | README | LICENSE

profile.rs (1011B)


      1 use nostrdb::ProfileRecord;
      2 
      3 pub enum DisplayName<'a> {
      4     One(&'a str),
      5 
      6     Both {
      7         username: &'a str,
      8         display_name: &'a str,
      9     },
     10 }
     11 
     12 impl<'a> DisplayName<'a> {
     13     pub fn username(&self) -> &'a str {
     14         match self {
     15             Self::One(n) => n,
     16             Self::Both { username, .. } => username,
     17         }
     18     }
     19 }
     20 
     21 fn is_empty(s: &str) -> bool {
     22     s.chars().all(|c| c.is_whitespace())
     23 }
     24 
     25 pub fn get_profile_name<'a>(record: &'a ProfileRecord) -> Option<DisplayName<'a>> {
     26     let profile = record.record().profile()?;
     27     let display_name = profile.display_name().filter(|n| !is_empty(n));
     28     let name = profile.name().filter(|n| !is_empty(n));
     29 
     30     match (display_name, name) {
     31         (None, None) => None,
     32         (Some(disp), None) => Some(DisplayName::One(disp)),
     33         (None, Some(username)) => Some(DisplayName::One(username)),
     34         (Some(display_name), Some(username)) => Some(DisplayName::Both {
     35             display_name,
     36             username,
     37         }),
     38     }
     39 }