Rust · 1817 bytes Raw Blame History
1 //! IPC protocol types for gargears
2 //!
3 //! This crate defines the IPC protocol used by gargears daemon and gargearsctl.
4
5 use serde::{Deserialize, Serialize};
6
7 /// Commands that can be sent to the gargears daemon
8 #[derive(Debug, Clone, Serialize, Deserialize)]
9 #[serde(tag = "command", rename_all = "snake_case")]
10 pub enum Command {
11 /// Show the gargears window
12 Show,
13 /// Hide the gargears window
14 Hide,
15 /// Toggle window visibility
16 Toggle,
17 /// Get daemon status
18 Status,
19 /// Quit the daemon
20 Quit,
21 }
22
23 /// Response from the gargears daemon
24 #[derive(Debug, Clone, Serialize, Deserialize)]
25 pub struct Response {
26 pub success: bool,
27 #[serde(skip_serializing_if = "Option::is_none")]
28 pub data: Option<serde_json::Value>,
29 #[serde(skip_serializing_if = "Option::is_none")]
30 pub error: Option<String>,
31 }
32
33 impl Response {
34 pub fn ok() -> Self {
35 Self {
36 success: true,
37 data: None,
38 error: None,
39 }
40 }
41
42 pub fn ok_with_data(data: serde_json::Value) -> Self {
43 Self {
44 success: true,
45 data: Some(data),
46 error: None,
47 }
48 }
49
50 pub fn error(msg: impl Into<String>) -> Self {
51 Self {
52 success: false,
53 data: None,
54 error: Some(msg.into()),
55 }
56 }
57 }
58
59 /// Status information returned by the daemon
60 #[derive(Debug, Clone, Serialize, Deserialize)]
61 pub struct DaemonStatus {
62 pub visible: bool,
63 pub running: bool,
64 }
65
66 /// Get the default socket path for gargears
67 pub fn socket_path() -> std::path::PathBuf {
68 if let Ok(runtime_dir) = std::env::var("XDG_RUNTIME_DIR") {
69 std::path::PathBuf::from(runtime_dir).join("gargears.sock")
70 } else {
71 std::path::PathBuf::from("/tmp/gargears.sock")
72 }
73 }
74