~exprez135/castor

ref: 193de0c2079abf371286916b814804f34dd97087 castor/src/settings.rs -rw-r--r-- 4.5 KiB
193de0c2Julien Blanchard [gopher] Fix parsing of h type 2 years ago
                                                                                
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
extern crate dirs;

use std::fs;
use std::fs::File;
use std::fs::OpenOptions;
use std::io::Read;

use serde_derive::Deserialize;

#[derive(Deserialize)]
struct Settings {
    general: Option<General>,
    colors: Option<Color>,
    characters: Option<Character>,
    monospace: Option<Monospace>,
}

#[derive(Deserialize)]
struct General {
    start_url: Option<String>,
}

#[derive(Deserialize)]
struct Color {
    h1: Option<String>,
    h2: Option<String>,
    h3: Option<String>,
    list: Option<String>,
    text: Option<String>,
    background: Option<String>,
}

#[derive(Deserialize)]
struct Character {
    h1: Option<String>,
    h2: Option<String>,
    h3: Option<String>,
    list: Option<String>,
}

#[derive(Deserialize)]
struct Monospace {
    finger: Option<bool>,
    gemini: Option<bool>,
    gopher: Option<bool>,
}

pub fn start_url() -> Option<String> {
    match read().general {
        Some(general) => general.start_url,
        None => None,
    }
}

pub fn h1_color() -> String {
    match read().colors {
        Some(colors) => match colors.h1 {
            Some(color) => color,
            None => String::from("#9932CC"),
        },
        None => String::from("#9932CC"),
    }
}

pub fn h2_color() -> String {
    match read().colors {
        Some(colors) => match colors.h2 {
            Some(color) => color,
            None => String::from("#FF1493"),
        },
        None => String::from("#FF1493"),
    }
}

pub fn h3_color() -> String {
    match read().colors {
        Some(colors) => match colors.h3 {
            Some(color) => color,
            None => String::from("#87CEFA"),
        },
        None => String::from("#87CEFA"),
    }
}

pub fn list_color() -> String {
    match read().colors {
        Some(colors) => match colors.list {
            Some(color) => color,
            None => String::from("green"),
        },
        None => String::from("green"),
    }
}

pub fn text_color() -> String {
    match read().colors {
        Some(colors) => match colors.text {
            Some(color) => color,
            None => String::from("black"),
        },
        None => String::from("black"),
    }
}

pub fn background_color() -> Option<String> {
    match read().colors {
        Some(colors) => colors.background,
        None => None,
    }
}

pub fn h1_character() -> String {
    match read().characters {
        Some(characters) => match characters.h1 {
            Some(character) => character,
            None => String::new(),
        },
        None => String::new(),
    }
}

pub fn h2_character() -> String {
    match read().characters {
        Some(characters) => match characters.h2 {
            Some(character) => character,
            None => String::new(),
        },
        None => String::new(),
    }
}

pub fn h3_character() -> String {
    match read().characters {
        Some(characters) => match characters.h3 {
            Some(character) => character,
            None => String::new(),
        },
        None => String::new(),
    }
}

pub fn list_character() -> String {
    match read().characters {
        Some(characters) => match characters.list {
            Some(character) => character,
            None => String::from("■"),
        },
        None => String::from("■"),
    }
}

pub fn finger_monospace() -> bool {
    match read().monospace {
        Some(monospace) => match monospace.finger {
            Some(setting) => setting,
            None => true,
        },
        None => true,
    }
}

pub fn gemini_monospace() -> bool {
    match read().monospace {
        Some(monospace) => match monospace.gemini {
            Some(setting) => setting,
            None => true,
        },
        None => true,
    }
}

pub fn gopher_monospace() -> bool {
    match read().monospace {
        Some(monospace) => match monospace.gopher {
            Some(setting) => setting,
            None => true,
        },
        None => true,
    }
}

fn read() -> Settings {
    let mut file = settings_file();
    let mut content = String::new();
    file.read_to_string(&mut content)
        .expect("Unable to read file");

    let settings: Settings = toml::from_str(&content).unwrap();
    settings
}

fn settings_file() -> File {
    let mut settings = dirs::config_dir().unwrap();
    settings.push("castor");
    fs::create_dir_all(&settings).unwrap();
    settings.push("settings.toml");
    let file_path = settings.into_os_string();

    OpenOptions::new()
        .create(true)
        .append(true)
        .read(true)
        .open(file_path)
        .expect("file not found")
}