astrox_macros 0.2.9

add user management and canister relation management for canister
Documentation
use ic_cdk::export::candid::{CandidType};
use serde::Serialize;


#[derive(Clone, Debug, CandidType, Serialize)]
pub struct LogEntry {
  pub log: String,
  pub created_at: u64,
}


#[derive(Clone, Debug, CandidType, Serialize)]
pub struct Log {
  pub logs: Vec<LogEntry>,
}

impl Log {
  pub fn new() -> Self {
    Log { logs: vec![] }
  }

  pub fn log_add(&mut self, ts: u64, log: String) {
    self.logs.insert(
      0,
      LogEntry {
        created_at: ts,
        log,
      },
    );
  }

  pub fn log_list_between(&self, from_ts: u64, to_ts: u64) -> Vec<LogEntry>{
    self.logs
      .iter()
      .filter_map(|log| {
        if log.created_at >= from_ts && log.created_at <= to_ts {
          Some(log.clone())
        } else {
          None
        }
      })
      .collect()
  }

  pub fn log_list_after(&self, after_ts: u64) -> Vec<LogEntry>{
    self.logs
      .iter()
      .filter_map(|log| {
        if log.created_at >= after_ts {
          Some(log.clone())
        } else {
          None
        }
      })
      .collect()
  }

  pub fn log_clear(&mut self, before_ts: u64) {
    self.logs.retain(|log| log.created_at > before_ts)
  }
}