add some basic config file parsing

This commit is contained in:
sigil-03 2025-04-02 22:05:32 -06:00
parent 9816809143
commit 9a8896e855
5 changed files with 1594 additions and 13 deletions

View file

@ -1,4 +1,7 @@
use clap::{Parser, Subcommand};
use monitor::Monitor;
mod monitor;
#[derive(Subcommand)]
pub enum Commands {
@ -6,9 +9,11 @@ pub enum Commands {
}
impl Commands {
pub fn execute(self) {
pub fn execute(self, config_file: &str) {
match self {
Self::Monitor => {
let _m = Monitor::new_from_file(config_file).unwrap();
println!("[TODO] Power: ----W")
}
}
@ -17,13 +22,15 @@ impl Commands {
#[derive(Parser)]
pub struct Cli {
#[arg(short, long)]
config_file: String,
#[command(subcommand)]
command: Commands,
}
impl Cli {
pub fn execute(self) {
self.command.execute();
self.command.execute(&self.config_file);
}
}

34
src/monitor.rs Normal file
View file

@ -0,0 +1,34 @@
use serde::Deserialize;
use std::fs;
use thiserror::Error;
#[derive(Error, Debug)]
pub enum Error {
#[error("io error")]
IoError(#[from] std::io::Error),
#[error("toml parsing error")]
ParseError(#[from] toml::de::Error),
}
#[derive(Deserialize)]
pub struct MonitorConfig {
target: Vec<String>,
}
impl MonitorConfig {
fn print(&self) {
for t in &self.target {
println!("* {}", t);
}
}
}
pub struct Monitor {}
impl Monitor {
pub fn new_from_file(config_file: &str) -> Result<Self, Error> {
let config_str = fs::read_to_string(config_file)?;
let config: MonitorConfig = toml::from_str(&config_str)?;
config.print();
Ok(Self {})
}
}