Implemented better ui.
This commit is contained in:
1 parent
358fb19076
commit
20b5f32c71
8 files changed
+399
-49
No files matched your search
+28
-1
@@ -2,6 +2,7 @@ use std::fmt;
|
||||
|
||||
use regex::{Captures, Regex};
|
||||
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
||||
use sha2::{Sha256, Digest};
|
||||
|
||||
#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
|
||||
struct SafeId {
|
||||
@@ -15,6 +16,7 @@ pub trait Id {
|
||||
fn from_string(s: &String) -> Option<Self::Output>;
|
||||
fn from_id(id: i64) -> Self::Output;
|
||||
fn get_id(&self) -> i64;
|
||||
fn micro_id(&self) -> String;
|
||||
}
|
||||
|
||||
impl fmt::Display for SafeId {
|
||||
@@ -58,6 +60,13 @@ impl Id for SafeId {
|
||||
fn get_id(&self) -> i64 {
|
||||
return i64::from_be_bytes(self.encrypted_id);
|
||||
}
|
||||
|
||||
fn micro_id(&self) -> String {
|
||||
let mut hasher = Sha256::new();
|
||||
hasher.update(self.encrypted_id.as_slice());
|
||||
let ret = hex::encode(hasher.finalize());
|
||||
ret.get(0..6).unwrap_or(ret.as_str()).to_string()
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! impl_ids {
|
||||
@@ -100,6 +109,10 @@ macro_rules! impl_ids {
|
||||
fn get_id(&self) -> i64 {
|
||||
self.0.get_id()
|
||||
}
|
||||
|
||||
fn micro_id(&self) -> String {
|
||||
format!("{}-{}", $prefix, self.0.micro_id())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -136,7 +149,7 @@ impl_ids!(UserId, "u");
|
||||
impl_ids!(SecurityId, "s");
|
||||
|
||||
#[derive(PartialEq, Debug)]
|
||||
enum ExternalId { // todo: perhaps this abstraction is useless.. duno
|
||||
pub enum ExternalId {
|
||||
AccountId(AccountId),
|
||||
UserId(UserId),
|
||||
SecurityId(SecurityId),
|
||||
@@ -185,6 +198,14 @@ impl ExternalId {
|
||||
ExternalId::SecurityId(x) => x.get_id(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn micro_id(&self) -> String{
|
||||
match self{
|
||||
ExternalId::UserId(sid) => sid.micro_id(),
|
||||
ExternalId::AccountId(sid) => sid.micro_id(),
|
||||
ExternalId::SecurityId(sid) => sid.micro_id(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -209,6 +230,12 @@ pub mod test_external_ids {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_micro_id() {
|
||||
let sid = SecurityId::from_id(33);
|
||||
assert_eq!(sid.micro_id(), "s-ea676268")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn safe_id_happy_path_1() {
|
||||
let sid = SafeId {
|
||||
|
||||
@@ -1,30 +1,67 @@
|
||||
use chrono::{DateTime, NaiveDateTime, Utc};
|
||||
use regex::Regex;
|
||||
//
|
||||
// fn is_number(value: String) -> bool{
|
||||
// true
|
||||
// }
|
||||
//
|
||||
// fn pretty_number(value: String) -> String {
|
||||
// let re = Regex::new(r"\d+").unwrap();
|
||||
// let cap = re.captures(&value);
|
||||
// if cap.is_none() {
|
||||
// return value;
|
||||
// }
|
||||
// let mut ret = "".to_string();
|
||||
// for (idx, letter) in value.as_bytes().iter().enumerate() {
|
||||
// ret.push(letter);
|
||||
// }
|
||||
// "foo".to_string()
|
||||
//
|
||||
// }
|
||||
//
|
||||
//
|
||||
// #[cfg(test)]
|
||||
// pub mod test_minininja_utils {
|
||||
// use super::*;
|
||||
//
|
||||
// #[test]
|
||||
// fn test_pretty_number() {
|
||||
// assert_eq!(pretty_number("foo".to_string()), "foo");
|
||||
// }
|
||||
// }
|
||||
use lazy_static::lazy_static;
|
||||
use crate::db::external_id::ExternalId;
|
||||
|
||||
|
||||
pub fn add_thousand_separators(value: String) -> String {
|
||||
lazy_static! {
|
||||
static ref is_number: Regex = Regex::new(r"\d+").unwrap();
|
||||
}
|
||||
let cap = is_number.captures(&value);
|
||||
if cap.is_none() {
|
||||
return value;
|
||||
}
|
||||
let start = value.len() % 3;
|
||||
let n_blocks = value.len() / 3;
|
||||
let mut ret = String::new();
|
||||
ret += value.get(0..start).unwrap_or("");
|
||||
for i in 0..n_blocks {
|
||||
ret += " ";
|
||||
ret += value.get((start + i * 3)..(start + (i + 1) * 3)).unwrap_or("");
|
||||
}
|
||||
ret
|
||||
}
|
||||
|
||||
|
||||
pub fn utc_date(value: String) -> String {
|
||||
let timestamp = value.parse::<i64>();
|
||||
if timestamp.is_err() {
|
||||
return value;
|
||||
}
|
||||
let naive = NaiveDateTime::from_timestamp_opt(timestamp.unwrap(), 0);
|
||||
if naive.is_none() {
|
||||
return value;
|
||||
}
|
||||
let datetime: DateTime<Utc> = DateTime::from_utc(naive.unwrap(), Utc);
|
||||
|
||||
datetime.format("%Y-%m-%d %H:%M:%S").to_string()
|
||||
}
|
||||
|
||||
pub fn micro_id(value: String) -> String {
|
||||
ExternalId::from_string(&value).map(|id| id.micro_id()).unwrap_or(value)
|
||||
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod test_minininja_utils {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_add_thousand_separator() {
|
||||
assert_eq!(add_thousand_separators("foo".to_string()), "foo");
|
||||
assert_eq!(add_thousand_separators("💖💖".to_string()), "💖💖");
|
||||
assert_eq!(add_thousand_separators("10".to_string()), "10");
|
||||
assert_eq!(add_thousand_separators("1000".to_string()), "1 000");
|
||||
assert_eq!(add_thousand_separators("1000".to_string()), "1 000");
|
||||
assert_eq!(add_thousand_separators("12345".to_string()), "12 345");
|
||||
assert_eq!(add_thousand_separators("12345678901234".to_string()), "12 345 678 901 234");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_utc_date() {
|
||||
assert_eq!(utc_date("foo".to_string()), "foo");
|
||||
assert_eq!(utc_date("💖💖".to_string()), "💖💖");
|
||||
assert_eq!(utc_date("1524820690".to_string()), "2018-04-27 09:18:10");
|
||||
}
|
||||
}
|
||||
@@ -49,6 +49,9 @@ fn create_auto_reloader(enable_template_autoreload: bool) -> AutoReloader {
|
||||
notifier.watch_path(&tmpl_path, true);
|
||||
}
|
||||
env.set_source(minijinja::Source::from_path(tmpl_path));
|
||||
env.add_filter("thousand_separator", minininja_utils::add_thousand_separators);
|
||||
env.add_filter("utc_date", minininja_utils::utc_date);
|
||||
env.add_filter("micro_id", minininja_utils::micro_id);
|
||||
Ok(env)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user