diff --git a/flake.nix b/flake.nix index 787d704..4946d9c 100644 --- a/flake.nix +++ b/flake.nix @@ -65,10 +65,7 @@ # Inherit inputs from checks. checks = self.checks.${system}; - # Additional dev-shell environment variables can be set directly - # MY_CUSTOM_DEVELOPMENT_VAR = "something else"; - - # Extra inputs can be added here; cargo and rustc are provided by default. + RUST_LOG = "debug"; packages = [ # pkgs.ripgrep ]; diff --git a/src/config.rs b/src/config.rs index 25491f4..57ce7be 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,6 +1,5 @@ use serde::{Deserialize, Serialize}; use toml::Table; -use toml::map::Map; #[derive(Serialize, Deserialize)] pub struct Config { diff --git a/src/cpu.rs b/src/cpu.rs index 3952fbb..b1a313d 100644 --- a/src/cpu.rs +++ b/src/cpu.rs @@ -22,12 +22,25 @@ pub struct CpuCore { } #[derive(Debug, Clone, Copy)] -enum CoreType { +pub enum CoreType { P, // Performence E, // Efficent L, // Low power } +impl TryFrom for CoreType { + type Error = &'static str; + + fn try_from(value: String) -> Result { + match value.as_str() { + "E" => Ok(CoreType::E), + "P" => Ok(CoreType::P), + "L" => Ok(CoreType::L), + _ => Err(""), + } + } +} + impl CpuCore { fn determine_core_type() -> Result { if !(CpuCore::is_atomic()?) { diff --git a/src/main.rs b/src/main.rs index d4769db..4057763 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,11 @@ use clap::Parser; use raw_cpuid::*; use std::path::PathBuf; -use tracing::{debug, info, warn}; +use tracing::{debug, error, info, warn}; use crate::{ config::Config, - cpu::{Cpu, CpuCore}, + cpu::{CoreType, Cpu, CpuCore}, }; mod config; mod cpu; @@ -19,40 +19,45 @@ struct Args { } fn main() { + tracing_subscriber::fmt::init(); let args = Args::parse(); + let mut custom_cpu: Option = None; if let Some(config) = args.config { let config_buf = std::fs::read(config).unwrap(); let config_par: Config = toml::from_slice(&config_buf).unwrap(); if let Some(comp) = config_par.cpu.comp { warn!("Using custom cpu topo loaded from config!"); - let custom_topo: Vec = Vec::new(); + let mut custom_topo: Vec = Vec::new(); for x in comp { custom_topo.push(CpuCore { id: x.0.parse().unwrap(), core_type: { match x.1 { - toml::Value::String(y) => match y.as_str() { - "P" => {} - "E" => {} - "L" => {} - _ => { - unreachable!() + toml::Value::String(y) => { + if let Ok(ParsedCoreType) = CoreType::try_from(y.clone()) { + ParsedCoreType + } else { + error!("Couldn't parse {:?} as CoreType", y); + unreachable!(""); } - }, + } _ => { - unreachable!() + unreachable!("") } } }, }) } + custom_cpu = Some(Cpu { cores: custom_topo }); } - Cpu { cores: vec![] }; } - tracing_subscriber::fmt::init(); - - let cpu = cpu::Cpu::new(); + let mut cpu: Cpu; + if custom_cpu.is_none() { + cpu = cpu::Cpu::new(); + } else { + cpu = custom_cpu.unwrap(); + }; debug!("{cpu:?}"); } diff --git a/test_config.toml b/test_config.toml index 490af3b..57c179b 100644 --- a/test_config.toml +++ b/test_config.toml @@ -1,4 +1,4 @@ [cpu] comp = { -"0" = {type = E} +"0" = "E" }