aboutsummaryrefslogtreecommitdiffstats
path: root/src/render.rs
diff options
context:
space:
mode:
authorDaniel Jankowski <daniel.jankowski@rub.de>2023-12-26 12:07:33 +0100
committerDaniel Jankowski <daniel.jankowski@rub.de>2023-12-26 12:07:33 +0100
commit7e939328f9ec45fbcd856474dd7c464e3ff402bb (patch)
tree9bb682a5d63c4214d14d6ab25f80091a54c64bea /src/render.rs
parentchore: update cargo deps (diff)
downloadsl0pstatus-7e939328f9ec45fbcd856474dd7c464e3ff402bb.tar.gz
sl0pstatus-7e939328f9ec45fbcd856474dd7c464e3ff402bb.tar.xz
sl0pstatus-7e939328f9ec45fbcd856474dd7c464e3ff402bb.zip
feat: implement cache for dynamic command rendering
Diffstat (limited to '')
-rw-r--r--src/render.rs24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/render.rs b/src/render.rs
index 3f645f6..048b82d 100644
--- a/src/render.rs
+++ b/src/render.rs
@@ -1,3 +1,4 @@
+use cached::{proc_macro::cached, SizedCache};
use lazy_static::lazy_static;
use std::{collections::BTreeMap, sync::Arc};
@@ -20,6 +21,24 @@ pub struct FormattedPart {
pub content: String,
}
+#[cached(
+ type = "SizedCache<String, FormattedPart>",
+ create = "{ SizedCache::with_size(100) }",
+ convert = r#"{ (format.to_owned()) }"#
+)]
+pub fn formatted_part_from_string_cached(format: &str) -> FormattedPart {
+ FormattedPart::from_format_string(format)
+}
+
+#[cached(
+ type = "SizedCache<String, Vec<FormattedPart>>",
+ create = "{ SizedCache::with_size(100) }",
+ convert = r#"{ (config.to_owned()) }"#
+)]
+pub fn formatted_parts_from_string_cached(config: &str) -> Vec<FormattedPart> {
+ FormattedPart::multiple_from_format_string(config)
+}
+
impl FormattedPart {
pub fn multiple_from_format_string(config: &str) -> Vec<Self> {
config
@@ -144,6 +163,11 @@ fn hex_to_rgb(s: &str) -> anyhow::Result<Vec<u8>> {
.collect()
}
+#[cached(
+ type = "SizedCache<String, Option<Color>>",
+ create = "{ SizedCache::with_size(100) }",
+ convert = r#"{ (color.to_owned()) }"#
+)]
fn parse_color(color: &str) -> Option<Color> {
if color.starts_with('#') {
let rgb = match hex_to_rgb(color.strip_prefix('#').unwrap()) {