notedeck

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

profile.rs (2546B)


      1 use serde_json::{Map, Value};
      2 
      3 #[derive(Debug, Clone)]
      4 pub struct ProfileState(Value);
      5 
      6 impl Default for ProfileState {
      7     fn default() -> Self {
      8         ProfileState::new(Map::default())
      9     }
     10 }
     11 
     12 impl ProfileState {
     13     pub fn new(value: Map<String, Value>) -> Self {
     14         Self(Value::Object(value))
     15     }
     16 
     17     pub fn get_str(&self, name: &str) -> Option<&str> {
     18         self.0.get(name).and_then(|v| v.as_str())
     19     }
     20 
     21     pub fn values_mut(&mut self) -> &mut Map<String, Value> {
     22         self.0.as_object_mut().unwrap()
     23     }
     24 
     25     /// Insert or overwrite an existing value with a string
     26     pub fn str_mut(&mut self, name: &str) -> &mut String {
     27         let val = self
     28             .values_mut()
     29             .entry(name)
     30             .or_insert(Value::String("".to_string()));
     31 
     32         // if its not a string, make it one. this will overrwrite
     33         // the old value, so be careful
     34         if !val.is_string() {
     35             *val = Value::String("".to_string());
     36         }
     37 
     38         match val {
     39             Value::String(s) => s,
     40             // SAFETY: we replace it above, so its impossible to be something
     41             // other than a string
     42             _ => panic!("impossible"),
     43         }
     44     }
     45 
     46     pub fn value(&self) -> &Value {
     47         &self.0
     48     }
     49 
     50     pub fn to_json(&self) -> String {
     51         // SAFETY: serializing a value should be irrefutable
     52         serde_json::to_string(self.value()).unwrap()
     53     }
     54 
     55     #[inline]
     56     pub fn name(&self) -> Option<&str> {
     57         self.get_str("name")
     58     }
     59 
     60     #[inline]
     61     pub fn banner(&self) -> Option<&str> {
     62         self.get_str("name")
     63     }
     64 
     65     #[inline]
     66     pub fn display_name(&self) -> Option<&str> {
     67         self.get_str("display_name")
     68     }
     69 
     70     #[inline]
     71     pub fn lud06(&self) -> Option<&str> {
     72         self.get_str("lud06")
     73     }
     74 
     75     #[inline]
     76     pub fn nip05(&self) -> Option<&str> {
     77         self.get_str("nip05")
     78     }
     79 
     80     #[inline]
     81     pub fn lud16(&self) -> Option<&str> {
     82         self.get_str("lud16")
     83     }
     84 
     85     #[inline]
     86     pub fn about(&self) -> Option<&str> {
     87         self.get_str("about")
     88     }
     89 
     90     #[inline]
     91     pub fn picture(&self) -> Option<&str> {
     92         self.get_str("picture")
     93     }
     94 
     95     #[inline]
     96     pub fn website(&self) -> Option<&str> {
     97         self.get_str("website")
     98     }
     99 
    100     pub fn from_note_contents(contents: &str) -> Self {
    101         let json = serde_json::from_str(contents);
    102         let data = if let Ok(Value::Object(data)) = json {
    103             data
    104         } else {
    105             Map::new()
    106         };
    107 
    108         Self::new(data)
    109     }
    110 }