rubblets/
lib.rs

1//! Rubblets are the modules that bar-rubble can load to get different types of infomation to
2//! display
3
4use clap::Args;
5use handlebars::Handlebars;
6use handlebars::JsonValue;
7use std::error::Error;
8
9#[cfg(feature = "device")]
10pub mod device;
11#[cfg(feature = "ip")]
12pub mod ip;
13#[cfg(feature = "mail")]
14pub mod mail;
15#[cfg(feature = "mpris")]
16pub mod mpris;
17#[cfg(feature = "network")]
18pub mod network;
19#[cfg(feature = "razor")]
20pub mod razor;
21#[cfg(feature = "weather")]
22pub mod weather;
23#[cfg(feature = "wireguard")]
24pub mod wireguard;
25
26#[derive(Args)]
27/// Wraps the arguments to be passed when using ListFormattingStrings
28pub struct PrintFormattingStringsArgs {
29    pub command: String,
30    pub selector: Option<device::SystemStats>,
31}
32
33/// Rubblets should return a RubbleResult so we can use the formatter to print result to screen
34pub struct RubbleResult {
35    pub json_value: Result<JsonValue, Box<dyn Error>>,
36    pub default_format: &'static str,
37    pub user_format: Option<String>,
38}
39
40impl RubbleResult {
41    /// Constructor for RubbleResult without any user_format
42    /// as this is typically passed in later
43    pub fn new(
44        json_value: Result<JsonValue, Box<dyn Error>>,
45        default_format: &'static str,
46    ) -> Self {
47        Self {
48            json_value: json_value,
49            default_format: default_format,
50            user_format: None,
51        }
52    }
53
54    /// Prints RubbleResult to stdout as formatted by user
55    /// or using the default format for the rubblet
56    pub fn print(&self) {
57        let reg = Handlebars::new();
58        // there has to be a better way to do this
59        let fmt = if self.user_format.is_some() {
60            self.user_format.as_ref().unwrap()
61        } else {
62            self.default_format
63        };
64        let render = reg.render_template(fmt, &self.json_value.as_ref().unwrap());
65        println!("{}", render.unwrap())
66    }
67}
68
69/// Wraps each of the formatting strings in a {{}} so the user can easily see how to use fmt
70/// strings, but leaves a trailing whitespace
71///
72/// # Examples
73///
74/// ```
75/// let result = rubblets::format_formatting_strings("this is a test");
76/// assert_eq!(result, "{{this}} {{is}} {{a}} {{test}} ");
77/// ```
78pub fn format_formatting_strings(options: &str) -> String {
79    options
80        .split_whitespace()
81        .into_iter()
82        .map(|o| format!("{}{}{} ", "{{", o, "}}"))
83        .collect()
84}
85
86/// Creates the default_format() and formatting_strings() Fn
87/// Rubblets can use this macro to reduce boilerplate code
88macro_rules! avaliable_formattings {
89    (
90        $default_format:expr,
91        $formatting_strings:expr
92    ) => {
93        pub fn default_format() -> &'static str {
94            ($default_format)
95        }
96        pub fn formatting_strings() -> &'static str {
97            ($formatting_strings)
98        }
99    };
100}
101
102pub(crate) use avaliable_formattings;