Files
fluxo-rs/src/output.rs
T
nvrl 2ce5aceae0
Release / Build and Release (push) Has been cancelled
implemented library calls instead of cli calls
2026-04-01 12:23:04 +02:00

76 lines
2.2 KiB
Rust

use serde::Serialize;
#[derive(Serialize)]
pub struct WaybarOutput {
pub text: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub tooltip: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub class: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub percentage: Option<u8>,
}
impl Default for WaybarOutput {
fn default() -> Self {
Self {
text: String::new(),
tooltip: None,
class: None,
percentage: None,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_full_output_serialization() {
let output = WaybarOutput {
text: "CPU: 50%".to_string(),
tooltip: Some("Details".to_string()),
class: Some("normal".to_string()),
percentage: Some(50),
};
let json = serde_json::to_string(&output).unwrap();
let val: serde_json::Value = serde_json::from_str(&json).unwrap();
assert_eq!(val["text"], "CPU: 50%");
assert_eq!(val["tooltip"], "Details");
assert_eq!(val["class"], "normal");
assert_eq!(val["percentage"], 50);
}
#[test]
fn test_optional_fields_omitted() {
let output = WaybarOutput {
text: "test".to_string(),
tooltip: None,
class: None,
percentage: None,
};
let json = serde_json::to_string(&output).unwrap();
let val: serde_json::Value = serde_json::from_str(&json).unwrap();
assert_eq!(val["text"], "test");
assert!(val.get("tooltip").is_none());
assert!(val.get("class").is_none());
assert!(val.get("percentage").is_none());
}
#[test]
fn test_partial_optional_fields() {
let output = WaybarOutput {
text: "test".to_string(),
tooltip: Some("tip".to_string()),
class: None,
percentage: Some(75),
};
let json = serde_json::to_string(&output).unwrap();
let val: serde_json::Value = serde_json::from_str(&json).unwrap();
assert_eq!(val["tooltip"], "tip");
assert!(val.get("class").is_none());
assert_eq!(val["percentage"], 75);
}
}