veridian_kernel/desktop/
session_config.rs1#[cfg(feature = "alloc")]
8extern crate alloc;
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12pub enum SessionPreference {
13 Plasma,
15 Builtin,
17}
18
19const SESSION_CONF_PATH: &str = "/etc/veridian/session.conf";
21
22const KDE_INIT_SCRIPT: &str = "/usr/share/veridian/veridian-kde-init.sh";
24
25#[cfg(feature = "alloc")]
31pub fn read_session_preference() -> SessionPreference {
32 match crate::fs::read_file(SESSION_CONF_PATH) {
33 Ok(data) => parse_session_config(&data),
34 Err(_) => {
35 SessionPreference::Plasma
37 }
38 }
39}
40
41#[cfg(feature = "alloc")]
43fn parse_session_config(data: &[u8]) -> SessionPreference {
44 let text = match core::str::from_utf8(data) {
45 Ok(s) => s,
46 Err(_) => return SessionPreference::Plasma,
47 };
48
49 for line in text.lines() {
50 let trimmed = line.trim();
51 if trimmed.starts_with('#') || trimmed.is_empty() {
52 continue;
53 }
54 if let Some(value) = trimmed.strip_prefix("session_type=") {
55 return match value.trim() {
56 "plasma" | "kde" => SessionPreference::Plasma,
57 "builtin" | "default" => SessionPreference::Builtin,
58 _ => SessionPreference::Plasma,
59 };
60 }
61 }
62
63 SessionPreference::Plasma
65}
66
67#[cfg(feature = "alloc")]
71pub fn kde_binaries_available() -> bool {
72 crate::fs::read_file(KDE_INIT_SCRIPT).is_ok()
73}
74
75#[cfg(test)]
80mod tests {
81 use super::*;
82
83 #[test]
84 fn test_parse_plasma() {
85 let data = b"session_type=plasma\n";
86 assert_eq!(parse_session_config(data), SessionPreference::Plasma);
87 }
88
89 #[test]
90 fn test_parse_kde_alias() {
91 let data = b"session_type=kde\n";
92 assert_eq!(parse_session_config(data), SessionPreference::Plasma);
93 }
94
95 #[test]
96 fn test_parse_builtin() {
97 let data = b"session_type=builtin\n";
98 assert_eq!(parse_session_config(data), SessionPreference::Builtin);
99 }
100
101 #[test]
102 fn test_parse_default_alias() {
103 let data = b"session_type=default\n";
104 assert_eq!(parse_session_config(data), SessionPreference::Builtin);
105 }
106
107 #[test]
108 fn test_parse_empty_file() {
109 let data = b"";
110 assert_eq!(parse_session_config(data), SessionPreference::Plasma);
111 }
112
113 #[test]
114 fn test_parse_comments_only() {
115 let data = b"# This is a comment\n# Another comment\n";
116 assert_eq!(parse_session_config(data), SessionPreference::Plasma);
117 }
118
119 #[test]
120 fn test_parse_unknown_value() {
121 let data = b"session_type=gnome\n";
122 assert_eq!(parse_session_config(data), SessionPreference::Plasma);
123 }
124
125 #[test]
126 fn test_parse_with_comments_and_whitespace() {
127 let data = b"# Session configuration\n\nsession_type=builtin\n";
128 assert_eq!(parse_session_config(data), SessionPreference::Builtin);
129 }
130
131 #[test]
132 fn test_parse_invalid_utf8() {
133 let data = &[0xFF, 0xFE, 0x00];
134 assert_eq!(parse_session_config(data), SessionPreference::Plasma);
135 }
136}