-
Notifications
You must be signed in to change notification settings - Fork 0
/
wm_lib.rs
96 lines (84 loc) · 2.71 KB
/
wm_lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#![deny(clippy::all)]
use log::error;
use serde::{Deserialize, Serialize};
use std::path::Path;
use std::{collections::HashMap, fs::read_to_string};
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
pub struct Program {
pub name: String,
pub state: Option<String>,
pub wm_class: Option<String>,
pub args: Option<Vec<String>>,
pub delay: Option<u8>,
}
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
pub struct Conf {
pub desktops: Vec<DesktopLayout>,
pub workspaces: Option<HashMap<i32, i32>>,
}
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
pub struct DesktopLayout {
pub desktop: String,
pub asyncro: Option<bool>,
pub programs: Vec<Program>,
pub clear: Option<bool>,
}
pub fn get_layout(fname: &str) -> Result<Conf, u8> {
let file_path = match get_layout_file(fname) {
Ok(path) => path,
Err(_) => {
error!("can't load layout stored in \"{fname}\", file doesn't exist.");
return Err(4);
}
};
let layout_file = match read_to_string(&file_path) {
Ok(data) => data,
Err(_) => {
error!("could not layout file \"{file_path}\"");
return Err(4);
}
};
match serde_yaml::from_str(&layout_file) {
Ok(data) => Ok(data),
Err(e) => {
error!("could not parse yaml layout file {fname}. error: \"{e}\"");
Err(4)
}
}
}
fn get_layout_file(file_name: &str) -> Result<String, ()> {
// let shellexpand::tilde(
// &if file_name.ends_with(".layout") || file_name.ends_with(".yml") {
// format!("~/.config/auto-desk/layouts/{}", file_name)
// } else {
// format!("~/.config/auto-desk/layouts/{}.layout", file_name)
// },
// )
// .to_string();
// #[cfg("test")]
// {
// if Path::new(file_name).exists() {
// return Ok(Path::new(file_name).to_str().unwrap().to_string());
// }
// }
// TODO: pull path from config file
let mut layout_dir = shellexpand::tilde("~/.config/auto-desk/layouts/").to_string();
if shellexpand::tilde(&file_name)
.to_string()
.starts_with(&layout_dir)
&& Path::new(file_name).exists()
{
return Ok(shellexpand::tilde(file_name).to_string());
}
// TODO: pull path from config file
layout_dir =
shellexpand::tilde(&format!("~/.config/auto-desk/layouts/{}", file_name)).to_string();
let f_types = ["", ".yml", ".yaml", ".layout"];
for f_type in f_types {
let p = Path::new(&format!("{}{}", layout_dir, f_type)).to_owned();
if p.exists() {
return Ok(p.to_str().unwrap().to_string());
}
}
Err(())
}