add some basic config file parsing
This commit is contained in:
parent
9816809143
commit
9a8896e855
5 changed files with 1594 additions and 13 deletions
1553
Cargo.lock
generated
1553
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
|
@ -5,3 +5,7 @@ edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
clap = { version = "4.5.35", features = ["derive"] }
|
clap = { version = "4.5.35", features = ["derive"] }
|
||||||
|
reqwest = "0.12.15"
|
||||||
|
serde = { version = "1.0.219", features = ["derive"] }
|
||||||
|
thiserror = "2.0.12"
|
||||||
|
toml = "0.8.20"
|
||||||
|
|
|
||||||
5
config.toml
Normal file
5
config.toml
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
target = [
|
||||||
|
"target 1",
|
||||||
|
"target 2",
|
||||||
|
"target 3",
|
||||||
|
]
|
||||||
11
src/main.rs
11
src/main.rs
|
|
@ -1,4 +1,7 @@
|
||||||
use clap::{Parser, Subcommand};
|
use clap::{Parser, Subcommand};
|
||||||
|
use monitor::Monitor;
|
||||||
|
|
||||||
|
mod monitor;
|
||||||
|
|
||||||
#[derive(Subcommand)]
|
#[derive(Subcommand)]
|
||||||
pub enum Commands {
|
pub enum Commands {
|
||||||
|
|
@ -6,9 +9,11 @@ pub enum Commands {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Commands {
|
impl Commands {
|
||||||
pub fn execute(self) {
|
pub fn execute(self, config_file: &str) {
|
||||||
match self {
|
match self {
|
||||||
Self::Monitor => {
|
Self::Monitor => {
|
||||||
|
let _m = Monitor::new_from_file(config_file).unwrap();
|
||||||
|
|
||||||
println!("[TODO] Power: ----W")
|
println!("[TODO] Power: ----W")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -17,13 +22,15 @@ impl Commands {
|
||||||
|
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
pub struct Cli {
|
pub struct Cli {
|
||||||
|
#[arg(short, long)]
|
||||||
|
config_file: String,
|
||||||
#[command(subcommand)]
|
#[command(subcommand)]
|
||||||
command: Commands,
|
command: Commands,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Cli {
|
impl Cli {
|
||||||
pub fn execute(self) {
|
pub fn execute(self) {
|
||||||
self.command.execute();
|
self.command.execute(&self.config_file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
34
src/monitor.rs
Normal file
34
src/monitor.rs
Normal 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 {})
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue