1mod actions;
12mod bubbles;
13mod contributions;
14mod messages;
15pub mod plugin;
16pub mod spec;
17mod theme;
18pub mod version;
19
20pub use actions::{ActionSpec, ArgKind, ArgSpec, ModuleActions};
21pub use bubbles::{Area, AreaArrangement, Arrangement, Bubble, BubbleId, Overflow, Stacking};
22pub use contributions::Contribution;
23pub use messages::{
24 Activity, ActivityId, Click, ClientMessage, CompositorStatus, DaemonMessage, ErrorCode,
25 EventKind, PluginState, PluginStatus, Role, Scroll, Status,
26};
27pub use theme::{
28 Anchor, Appearance, Color, ColorError, Colors, Layout, Mode, Motion, Notch, Text, Theme,
29};
30
31pub const API: u32 = 1;
33
34pub const MAX_LINE: usize = 1 << 20;
37
38pub const SOCKET_ENV: &str = "MOCHI_SOCKET";
41
42pub fn socket_path() -> Option<std::path::PathBuf> {
44 let set = |name| std::env::var_os(name).filter(|value| !value.is_empty());
45 if let Some(socket) = set(SOCKET_ENV) {
46 return Some(socket.into());
47 }
48 let runtime: std::path::PathBuf = set("XDG_RUNTIME_DIR")?.into();
49 Some(runtime.join("mochi").join("mochi.sock"))
50}
51
52pub fn encode<T: serde::Serialize>(message: &T) -> Result<Vec<u8>, serde_json::Error> {
54 let mut line = serde_json::to_vec(message)?;
55 line.push(b'\n');
56 Ok(line)
57}
58
59pub fn decode<'a, T: serde::Deserialize<'a>>(line: &'a str) -> Result<T, serde_json::Error> {
61 serde_json::from_str(line.trim_end_matches(['\n', '\r']))
62}
63
64pub const RENAMED_MODULES: [(&str, &str); 1] = [("hub", "control-center")];
67
68pub fn module_id(id: &str) -> &str {
70 RENAMED_MODULES
71 .iter()
72 .find(|(old, _)| *old == id)
73 .map_or(id, |(_, new)| new)
74}
75
76#[cfg(test)]
77mod tests {
78 use super::*;
79
80 #[test]
81 fn encode_ends_with_one_newline() {
82 let line = encode(&ClientMessage::Status).unwrap();
83 assert_eq!(line, b"{\"type\":\"status\"}\n");
84 }
85
86 #[test]
87 fn decode_accepts_the_trailing_newline() {
88 let message: ClientMessage = decode("{\"type\":\"reload\"}\r\n").unwrap();
89 assert_eq!(message, ClientMessage::Reload);
90 }
91}