chore: rename executor -> evaluator and integrate into app

This commit is contained in:
multisn8 2024-01-21 03:22:46 +01:00
parent de9ca81b65
commit 3c529c3a1a
Signed by: multisamplednight
GPG key ID: 6D525AA147CBDAE2
16 changed files with 229 additions and 115 deletions

View file

@ -6,14 +6,16 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
clap = { workspace = true, features = [ "derive", "env" ] }
serde = { workspace = true, features = [ "derive" ] }
ron = "0.8"
serde_json = "1.0"
ariadne = "0.4"
time = { version = "0.3", features = [ "local-offset" ] }
clap = { workspace = true, features = [ "derive", "env" ] }
dirs = "5"
eval = { path = "../eval" }
ir = { path = "../ir" }
owo-colors = "4"
ron = "0.8"
serde = { workspace = true, features = [ "derive" ] }
serde_json = "1.0"
time = { version = "0.3", features = [ "local-offset" ] }
[lints]
workspace = true

View file

@ -1,3 +1,5 @@
use std::path::PathBuf;
use clap::Parser;
use self::{
@ -10,6 +12,9 @@ mod config_file;
/// this struct may hold all configuration
pub struct Config {
pub source: PathBuf,
pub evaluator: eval::Available,
pub startup_msg: bool,
}
@ -17,37 +22,37 @@ impl Config {
/// Get the configs from all possible places (args, file, env...)
pub fn read() -> Self {
let args = Args::parse();
let config_path = if let Some(config_path) = args.config_path {
Ok(config_path)
let config = if let Some(config) = args.config_path {
Ok(config)
} else {
find_config_file()
};
// try to read a maybe existing config file
let file_config = if let Ok(config_path) = config_path {
let file_config = Configs::read(config_path);
match file_config {
Ok(c) => Some(c),
Err(e) => {
let config = config.ok().and_then(|path| {
Configs::read(path).map_or_else(
|e| {
eprintln!("Config error: {e:?}");
eprintln!("Proceeding with defaults or cli args...");
None
}
}
} else {
None
};
},
Some,
)
});
if let Some(file_config) = file_config {
if let Some(file) = config {
Self {
source: args.source,
evaluator: args.evaluator.and(file.evaluator).unwrap_or_default(),
// this is negated because to an outward api, the negative is more intuitive,
// while in the source the other way around is more intuitive
startup_msg: !(args.no_startup_message || file_config.no_startup_message),
startup_msg: !(args.no_startup_message || file.no_startup_message),
}
} else {
Self {
source: args.source,
startup_msg: !args.no_startup_message,
evaluator: args.evaluator.unwrap_or_default(),
}
}
}
@ -56,7 +61,7 @@ impl Config {
pub mod error {
/// Errors that can occur when reading configs
#[derive(Debug)]
pub enum ConfigError {
pub enum Config {
/// The config dir doesn't exist
NoConfigDir,
/// We didn't find a config file in the config dir
@ -73,19 +78,19 @@ pub mod error {
SerdeRonError(ron::error::SpannedError),
}
impl From<std::io::Error> for ConfigError {
impl From<std::io::Error> for Config {
fn from(value: std::io::Error) -> Self {
Self::IoError(value)
}
}
impl From<serde_json::Error> for ConfigError {
impl From<serde_json::Error> for Config {
fn from(value: serde_json::Error) -> Self {
Self::SerdeJsonError(value)
}
}
impl From<ron::error::SpannedError> for ConfigError {
impl From<ron::error::SpannedError> for Config {
fn from(value: ron::error::SpannedError) -> Self {
Self::SerdeRonError(value)
}

View file

@ -4,6 +4,14 @@ use clap::{builder::BoolishValueParser, ArgAction, Parser};
#[derive(Parser)]
pub(crate) struct Args {
/// What file contains the pipeline to evaluate.
pub source: PathBuf,
/// How to actually run the pipeline.
/// Overrides the config file. Defaults to the debug evaluator.
#[arg(short, long)]
pub evaluator: Option<eval::Available>,
/// Read this config file.
#[arg(short, long)]
pub config_path: Option<PathBuf>,

View file

@ -5,7 +5,7 @@ use std::{
use serde::{Deserialize, Serialize};
use super::error::ConfigError;
use super::error::Config;
#[derive(Debug, Serialize, Deserialize)]
pub struct Configs {
@ -13,6 +13,7 @@ pub struct Configs {
pub example_value: i32,
#[serde(default)]
pub no_startup_message: bool,
pub evaluator: Option<eval::Available>,
}
/// what the fuck serde why do i need this
@ -21,9 +22,9 @@ fn default_example_value() -> i32 {
}
/// Find the location of a config file and check if there is, in fact, a file
pub(super) fn find_config_file() -> Result<PathBuf, ConfigError> {
pub(super) fn find_config_file() -> Result<PathBuf, Config> {
let Some(config_path) = dirs::config_dir() else {
return Err(ConfigError::NoConfigDir);
return Err(Config::NoConfigDir);
};
let ron_path = config_path.with_file_name("config.ron");
@ -34,19 +35,19 @@ pub(super) fn find_config_file() -> Result<PathBuf, ConfigError> {
} else if Path::new(&json_path).exists() {
Ok(json_path)
} else {
Err(ConfigError::NoConfigFileFound)
Err(Config::NoConfigFileFound)
}
}
impl Configs {
pub fn read(p: PathBuf) -> Result<Self, ConfigError> {
pub fn read(p: PathBuf) -> Result<Self, Config> {
match p
.extension()
.map(|v| v.to_str().expect("config path to be UTF-8"))
{
Some("ron") => Ok(serde_json::from_str(&fs::read_to_string(p)?)?),
Some("json") => Ok(ron::from_str(&fs::read_to_string(p)?)?),
e => Err(ConfigError::UnknownExtension(e.map(str::to_string))),
e => Err(Config::UnknownExtension(e.map(str::to_string))),
}
}
}

View file

@ -1,3 +1,5 @@
use std::fs;
use config::Config;
use welcome_msg::print_startup_msg;
@ -14,4 +16,14 @@ fn main() {
if cfg.startup_msg {
print_startup_msg();
}
let source =
fs::read_to_string(cfg.source).expect("can't find source file lol handle me better please");
let ir =
ir::from_ron(&source).expect("aww failed to parse source to graph ir handle me better");
let mut machine = cfg.evaluator.pick();
machine.feed(ir);
machine.eval_full();
}