add config parsing logic

This commit is contained in:
2026-03-19 11:29:38 +03:00
parent e05a94f168
commit 5d45b97a70
4 changed files with 217 additions and 16 deletions

View File

@@ -1,31 +1,75 @@
use eui48::MacAddress;
use serde::{Deserialize, self};
use std::{collections::HashMap, net::IpAddr};
use serde::Deserialize;
use std::{collections::HashMap, fs, io, net::IpAddr, path::Path};
#[derive(Deserialize)]
struct BotConfig {
#[derive(Deserialize, Debug)]
pub struct BotConfig {
pub auth: BotUserAuthConfig,
pub machines: HashMap<String, BotTargetMachineConfig>,
#[serde(rename = "deltaChat")]
pub delta_chat: BotDeltaChatConfig,
}
#[derive(Deserialize)]
struct BotUserAuthConfig {
#[derive(Deserialize, Debug)]
pub struct BotUserAuthConfig {
pub password: String,
}
#[derive(Deserialize)]
struct BotTargetMachineConfig {
#[derive(Deserialize, Debug)]
pub struct BotTargetMachineConfig {
#[serde(rename = "default")]
is_default: bool,
mac: MacAddress,
pub is_default: bool,
pub mac: MacAddress,
#[serde(rename = "staticIp")]
static_ip: IpAddr,
pub static_ip: IpAddr,
pub hostname: String,
#[serde(rename = "remoteUnlock")]
pub remote_unlock: Option<BotSshAccessConfig>,
#[serde(rename = "remoteAccess")]
pub remote_access: BotSshAccessConfig,
}
#[derive(Deserialize)]
struct BotDeltaChatConfig {
email: String,
password: String,
#[derive(Deserialize, Debug)]
pub struct BotSshAccessConfig {
#[serde(rename = "sshKeyFile")]
pub ssh_key_file: String,
}
#[derive(Deserialize, Debug)]
pub struct BotDeltaChatConfig {
pub email: String,
pub password: String,
}
#[derive(Debug)]
pub enum ConfigError {
Io(io::Error),
Parse(serde_yaml::Error),
}
impl std::fmt::Display for ConfigError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ConfigError::Io(e) => write!(f, "Failed to read config file: {e}"),
ConfigError::Parse(e) => write!(f, "Failed to parse config file: {e}"),
}
}
}
impl From<io::Error> for ConfigError {
fn from(e: io::Error) -> Self {
ConfigError::Io(e)
}
}
impl From<serde_yaml::Error> for ConfigError {
fn from(e: serde_yaml::Error) -> Self {
ConfigError::Parse(e)
}
}
pub fn read_config(path: &Path) -> Result<BotConfig, ConfigError> {
let contents = fs::read_to_string(path)?;
let config = serde_yaml::from_str(&contents)?;
Ok(config)
}

View File

@@ -1,5 +1,25 @@
mod config;
fn main() {
use clap::Parser;
use std::path::PathBuf;
/// Delta Chat bot for remote control of local network machines.
#[derive(Parser)]
#[command(version, about)]
struct Args {
/// Path to the YAML configuration file.
#[arg(short = 'c', long = "config")]
config: PathBuf,
}
fn main() {
let args = Args::parse();
let _ = match config::read_config(&args.config) {
Ok(c) => c,
Err(e) => {
eprintln!("Error: {e}");
std::process::exit(1);
}
};
}