-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.rs
More file actions
65 lines (58 loc) · 2.01 KB
/
Copy pathconfig.rs
File metadata and controls
65 lines (58 loc) · 2.01 KB
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
use serde::Deserialize;
use std::fs;
use std::path::Path;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum ConfigError {
#[error(
"config file not found at .config/dramma.toml !!!! please create it with:\ntoken = \"your-bearer-token\""
)]
NotFound,
#[error("failed to read config file: {0}")]
ReadError(#[from] std::io::Error),
#[error("failed to parse config file: {0}")]
ParseError(#[from] toml::de::Error),
}
#[derive(Debug, Clone, Deserialize)]
#[serde(default)]
pub struct Config {
pub token: Option<String>,
pub home_assistant_url: String,
/// Port for the HTTP listener that accepts `POST /close-hass` from HASS.
pub hass_api_port: u16,
pub cashcode_serial_port: String,
pub cctalk_serial_port: String,
/// Override the value for specific coin positions (channels).
/// Use this when the device has misconfigured coin IDs.
/// Format in dramma.toml:
/// cctalk_coin_overrides = [[1, 50], [3, 500]]
/// means position 1 → 50 AMD, position 3 → 500 AMD.
pub cctalk_coin_overrides: Vec<[i32; 2]>,
pub stats_db_path: String,
}
impl Default for Config {
fn default() -> Self {
Self {
token: None,
home_assistant_url: "https://ha.hackem.cc/web-dramma/0?BrowserID=dramma".to_string(),
hass_api_port: 8321,
cashcode_serial_port:
"/dev/serial/by-id/usb-Prolific_Technology_Inc._USB-Serial_Controller_D-if00-port0"
.to_string(),
cctalk_serial_port: "/dev/ttyUSB0".to_string(),
cctalk_coin_overrides: Vec::new(),
stats_db_path: "data/Stats.db".to_string(),
}
}
}
impl Config {
pub fn load() -> Result<Self, ConfigError> {
let config_path = Path::new(".config/dramma.toml");
if !config_path.exists() {
return Err(ConfigError::NotFound);
}
let content = fs::read_to_string(config_path)?;
let config: Config = toml::from_str(&content)?;
Ok(config)
}
}