From e152b73c512a63c881921b18f54dff9e7d0c38d6 Mon Sep 17 00:00:00 2001 From: daladim Date: Tue, 16 Nov 2021 23:25:41 +0100 Subject: [PATCH] Better config API --- src/config.rs | 13 +++++++------ src/ical/builder.rs | 4 ++-- src/ical/mod.rs | 2 +- src/lib.rs | 4 ++-- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/config.rs b/src/config.rs index 80615ab..fa74fdf 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,11 +1,12 @@ //! Support for compile-time configuration options +use std::sync::{Arc, Mutex}; use once_cell::sync::Lazy; -/// Part of the ProdID string that describes the organization (example of a ProdID string: `-//ABC Corporation//My Product//EN`) -/// You can override it at compile-time with the `KITCHEN_FRIDGE_ICAL_ORG_NAME` environment variable, or keep the default value -pub static ORG_NAME: Lazy = Lazy::new(|| option_env!("KITCHEN_FRIDGE_ICAL_ORG_NAME").unwrap_or("My organization").to_string() ); +/// Part of the ProdID string that describes the organization (example of a ProdID string: `-//ABC Corporation//My Product//EN`). +/// Feel free to override it when initing this library. +pub static ORG_NAME: Lazy>> = Lazy::new(|| Arc::new(Mutex::new("My organization".to_string()))); -/// Part of the ProdID string that describes the product name (example of a ProdID string: `-//ABC Corporation//My Product//EN`) -/// You can override it at compile-time with the `KITCHEN_FRIDGE_ICAL_PRODUCT_NAME` environment variable, or keep the default value -pub static PRODUCT_NAME: Lazy = Lazy::new(|| option_env!("KITCHEN_FRIDGE_ICAL_PRODUCT_NAME").unwrap_or("KitchenFridge").to_string() ); +/// Part of the ProdID string that describes the product name (example of a ProdID string: `-//ABC Corporation//My Product//EN`). +/// Feel free to override it when initing this library. +pub static PRODUCT_NAME: Lazy>> = Lazy::new(|| Arc::new(Mutex::new("KitchenFridge".to_string()))); diff --git a/src/ical/builder.rs b/src/ical/builder.rs index 3e4840e..2e1f7e1 100644 --- a/src/ical/builder.rs +++ b/src/ical/builder.rs @@ -104,7 +104,7 @@ mod tests { COMPLETED:{}\r\n\ STATUS:COMPLETED\r\n\ END:VTODO\r\n\ - END:VCALENDAR\r\n", *ORG_NAME, *PRODUCT_NAME, uid, s_now, s_now, s_now, s_now); + END:VCALENDAR\r\n", ORG_NAME.lock().unwrap(), PRODUCT_NAME.lock().unwrap(), uid, s_now, s_now, s_now, s_now); assert_eq!(ical, expected_ical); } @@ -124,7 +124,7 @@ mod tests { SUMMARY:This is a task with ÜTF-8 characters\r\n\ STATUS:NEEDS-ACTION\r\n\ END:VTODO\r\n\ - END:VCALENDAR\r\n", *ORG_NAME, *PRODUCT_NAME, uid, s_now, s_now, s_now); + END:VCALENDAR\r\n", ORG_NAME.lock().unwrap(), PRODUCT_NAME.lock().unwrap(), uid, s_now, s_now, s_now); assert_eq!(ical, expected_ical); } diff --git a/src/ical/mod.rs b/src/ical/mod.rs index 4931105..ee600af 100644 --- a/src/ical/mod.rs +++ b/src/ical/mod.rs @@ -10,7 +10,7 @@ pub use builder::build_from; use crate::config::{ORG_NAME, PRODUCT_NAME}; pub fn default_prod_id() -> String { - format!("-//{}//{}//EN", *ORG_NAME, *PRODUCT_NAME) + format!("-//{}//{}//EN", ORG_NAME.lock().unwrap(), PRODUCT_NAME.lock().unwrap()) } diff --git a/src/lib.rs b/src/lib.rs index 1a02dfc..ed3bf24 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,9 +22,9 @@ //! See example usage in the `examples/` folder, that you can run using `cargo run --example `. \ //! You can also have a look at `tasklist`, a GUI app that uses `kitchen-fridge` under the hood. //! -//! ## Compile-time configuration options +//! ## Configuration options //! -//! Have a look at the [`config`] module to see what options can be overridden at compile-time. +//! Have a look at the [`config`] module to see what default options can be overridden. pub mod traits;