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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use std::error::Error;
use std::io;
use std::io::prelude::*;
use std::fs;
use std::path::PathBuf;
use serde::{Serialize, Deserialize};
use serde_json::{Value as JsonValue, Map};
use crate::themeable::Themeable;
use crate::operation::Operation;
use crate::config::Config as ThconConfig;
use crate::sockets;
#[derive(Debug, Deserialize)]
pub struct Config {
dark: ConfigSection,
light: ConfigSection,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct ConfigSection {
colorscheme: Option<String>,
r#let: Option<Map<String, JsonValue>>,
set: Option<Map<String, JsonValue>>,
setglobal: Option<Map<String, JsonValue>>,
}
trait ControlledVim {
const SECTION_NAME: &'static str;
fn sock_dir() -> PathBuf {
let addr = sockets::socket_addr(Self::SECTION_NAME, true);
PathBuf::from(addr.parent().unwrap())
}
fn extract_config(thcon_config: &ThconConfig) -> &Option<Config>;
}
pub struct Vim;
impl ControlledVim for Vim {
const SECTION_NAME: &'static str = "vim";
fn extract_config(thcon_config: &ThconConfig) -> &Option<Config> {
&thcon_config.vim
}
}
impl Themeable for Vim {
fn has_config(&self, config: &ThconConfig) -> bool {
Vim::extract_config(config).is_some()
}
fn switch(&self, config: &ThconConfig, operation: &Operation) -> Result<(), Box<dyn Error>> {
anyvim_switch::<Vim>(config, operation)
}
}
pub struct Neovim;
impl ControlledVim for Neovim {
const SECTION_NAME: &'static str = "nvim";
fn extract_config(thcon_config: &ThconConfig) -> &Option<Config> {
&thcon_config.nvim
}
}
impl Themeable for Neovim {
fn has_config(&self, config: &ThconConfig) -> bool {
Neovim::extract_config(config).is_some()
}
fn switch(&self, config: &ThconConfig, operation: &Operation) -> Result<(), Box<dyn Error>> {
anyvim_switch::<Neovim>(config, operation)
}
}
fn anyvim_switch<V: ControlledVim>(config: &ThconConfig, operation: &Operation) -> Result<(), Box<dyn Error>> {
let config = match V::extract_config(config) {
Some(section) => section,
None => {
return Err(
Box::new(
io::Error::new(
io::ErrorKind::NotFound,
format!(
"Couldn't find [{}] section in thcon.toml",
V::SECTION_NAME
)
)
)
)
}
};
let payload = match operation {
Operation::Darken => &config.dark,
Operation::Lighten => &config.light
};
let payload = serde_json::to_vec(payload).unwrap_or_default();
let sock_dir = V::sock_dir();
let sockets = match fs::read_dir(sock_dir) {
Ok(sockets) => Ok(Some(sockets)),
Err(e) => match e.kind() {
io::ErrorKind::NotFound => Ok(None),
_ => Err(Box::new(e) as Box<dyn Error>)
}
};
sockets.map(|sockets| {
match sockets {
None => (),
Some(sockets) => {
for sock in sockets {
if sock.is_err() { continue; }
let sock = sock.unwrap().path();
if let Ok(mut stream) = std::os::unix::net::UnixStream::connect(&sock) {
stream.write_all(&payload).unwrap_or(())
}
}
}
}
})
}