lab_presence

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

lib.rs (3057B)


      1 use serde::{Deserialize, Serialize};
      2 use std::error::Error;
      3 
      4 #[derive(Serialize, Deserialize, Clone)]
      5 pub struct LabPresence {
      6     presence: String,
      7     status_msg: Option<String>,
      8     timestamp: u64,
      9 }
     10 
     11 impl LabPresence {
     12     pub fn new(presence: String, status_msg: Option<String>, timestamp: u64) -> Self {
     13         Self {
     14             presence,
     15             status_msg,
     16             timestamp,
     17         }
     18     }
     19 
     20     pub fn presence(&self) -> &str {
     21         &self.presence
     22     }
     23 
     24     pub fn status_msg(&self) -> Option<&str> {
     25         self.status_msg.as_deref()
     26     }
     27 
     28     pub fn timestamp(&self) -> u64 {
     29         self.timestamp
     30     }
     31 
     32     pub fn set(
     33         &mut self,
     34         presence: String,
     35         status_msg: Option<String>,
     36         timestamp: u64,
     37     ) -> Result<(), Box<dyn Error>> {
     38         self.presence = presence;
     39         self.status_msg = status_msg;
     40         self.timestamp = timestamp;
     41         Ok(())
     42     }
     43 
     44     pub fn update(
     45         &mut self,
     46         presence: String,
     47         status_msg: Option<String>,
     48         timestamp: u64,
     49     ) -> Result<(), Box<dyn std::error::Error>> {
     50         if timestamp > self.timestamp {
     51             self.presence = presence;
     52             self.status_msg = status_msg;
     53             self.timestamp = timestamp;
     54             Ok(())
     55         } else {
     56             Err(Box::new(std::io::Error::new(
     57                 std::io::ErrorKind::Other,
     58                 "timestamp is not greater than current timestamp",
     59             )))
     60         }
     61     }
     62 }
     63 
     64 #[derive(Serialize, Deserialize, Debug, Clone)]
     65 pub struct UserId {
     66     pub id: String,
     67 }
     68 
     69 #[derive(Serialize, Deserialize, Debug, Clone)]
     70 pub struct User {
     71     pub id: UserId,
     72     pub card_serial_number: String,
     73 }
     74 
     75 #[derive(Serialize, Deserialize)]
     76 pub struct MemberDirectory {
     77     pub users: Vec<User>,
     78 }
     79 impl MemberDirectory {
     80     pub fn new(users: Vec<User>) -> Self {
     81         Self { users }
     82     }
     83     pub fn init_from_env() -> Result<Self, Box<dyn std::error::Error>> {
     84         let member_directory = std::env::var("MEMBER_DIRECTORY")?;
     85         let users: Vec<User> = serde_json::from_str(&member_directory)?;
     86         Ok(Self::new(users))
     87     }
     88     pub fn inquire(&self, card_serial_number: &str) -> Result<UserId, Box<dyn std::error::Error>> {
     89         self.users
     90             .iter()
     91             .find(|u| u.card_serial_number == card_serial_number)
     92             .map(|u| u.id.clone())
     93             .ok_or_else(|| "User not found".into())
     94     }
     95 }
     96 
     97 #[derive(Serialize, Deserialize)]
     98 pub struct LoginRequest {
     99     pub device_id: String,
    100     pub device_secret: String,
    101 }
    102 
    103 #[derive(Serialize, Deserialize)]
    104 pub struct LoginResponse {
    105     pub access_token: String,
    106     pub expires_in: u32,
    107 }
    108 
    109 #[derive(Serialize, Deserialize)]
    110 pub struct PresenceResponse {
    111     pub presence: String,
    112     pub status_msg: String,
    113     pub timestamp: u64,
    114 }
    115 
    116 #[derive(Serialize, Deserialize)]
    117 pub struct PresenceUpdateRequest {
    118     pub presence: String,
    119     pub status_msg: String,
    120 }
    121 
    122 #[derive(Serialize, Deserialize)]
    123 pub struct ErrorResponse {
    124     pub errcode: String,
    125     pub error: String,
    126 }