use fount_core::{LauncherAction, SearchResult};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ClientMessage {
Search { query: String, limit: u32 },
Exec {
id: String,
execute: bool,
},
Reload,
Ping,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum DaemonMessage {
Results {
query: String,
items: Vec<SearchResultWire>,
},
Launched {
id: String,
success: bool,
error: Option<String>,
},
Reloaded { count: u32 },
Pong,
Error { message: String },
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SearchResultWire {
pub id: String,
pub name: String,
pub description: Option<String>,
pub icon: Option<String>,
pub exec: String,
pub is_custom_action: bool,
pub score: u32,
pub matched_indices: Vec<u32>,
}
impl From<SearchResult> for SearchResultWire {
fn from(res: SearchResult) -> Self {
let exec = match &res.entry.action {
LauncherAction::Exec { command, .. } => command.clone(),
LauncherAction::Command { program, args, .. } => {
if args.is_empty() {
program.clone()
} else {
format!("{} {}", program, args.join(" "))
}
}
LauncherAction::Shell(script) => script.clone(),
LauncherAction::Custom(custom_id) => custom_id.clone(),
};
let is_custom_action = res.entry.is_custom_action();
Self {
id: res.entry.id,
name: res.entry.name,
description: res.entry.description,
icon: res.entry.icon,
exec,
is_custom_action,
score: res.score,
matched_indices: res.matched_indices,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use fount_core::LauncherEntry;
#[test]
fn test_bincode_and_json_roundtrip() {
let req = ClientMessage::Search {
query: "fire".into(),
limit: 10,
};
let bytes = bincode::serialize(&req).unwrap();
let decoded: ClientMessage = bincode::deserialize(&bytes).unwrap();
assert_eq!(req, decoded);
let json = serde_json::to_string(&req).unwrap();
let decoded_json: ClientMessage = serde_json::from_str(&json).unwrap();
assert_eq!(req, decoded_json);
}
#[test]
fn test_search_result_wire_conversion() {
let entry = LauncherEntry::custom_action(
"action:poweroff",
"Power Off",
Some("system-shutdown".into()),
Some("Shut down the system".into()),
vec!["shutdown".into()],
LauncherAction::Command {
program: "systemctl".into(),
args: vec!["poweroff".into()],
terminal: false,
},
);
let res = SearchResult {
entry,
score: 140,
matched_indices: vec![0, 1, 2],
};
let wire = SearchResultWire::from(res);
assert_eq!(wire.id, "action:poweroff");
assert_eq!(wire.name, "Power Off");
assert_eq!(wire.exec, "systemctl poweroff");
assert!(wire.is_custom_action);
assert_eq!(wire.score, 140);
assert_eq!(wire.matched_indices, vec![0, 1, 2]);
}
}