diff --git a/Cargo.toml b/Cargo.toml index bfd336c..f9a6772 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,6 +21,10 @@ crate-type = ["cdylib", "rlib"] name = "pt" path = "src/bin/main/mod.rs" +[[bin]] +name = "ccc" +path = "src/bin/ccc/mod.rs" + [dependencies] clap = { version = "4.3.11", features = ["derive"] } clap-num = "1.0.2" diff --git a/src/bin/ccc/mod.rs b/src/bin/ccc/mod.rs new file mode 100644 index 0000000..88d9532 --- /dev/null +++ b/src/bin/ccc/mod.rs @@ -0,0 +1,132 @@ +//! # Executable for the math/compute submodule +//! +//! Compute computations with your computer! +//! +//! This command line tool allows you to input a mathematical expression. It will then process the +//! expression. + +//// ATTRIBUTES //////////////////////////////////////////////////////////////////////////////////// +// we want docs +#![warn(missing_docs)] +#![warn(rustdoc::missing_crate_level_docs)] +// we want Debug everywhere. +#![warn(missing_debug_implementations)] +// enable clippy's extra lints, the pedantic version +#![warn(clippy::pedantic)] + +//// IMPORTS /////////////////////////////////////////////////////////////////////////////////////// +use pt::math::computer::*; +use pt::logger::*; + +use clap::{Parser, Subcommand}; +use clap_num::number_range; +use clap_verbosity_flag::{Verbosity, InfoLevel}; + +use std::path::PathBuf; + +//// TYPES ///////////////////////////////////////////////////////////////////////////////////////// + +//// CONSTANTS ///////////////////////////////////////////////////////////////////////////////////// +/// short about section displayed in help +const ABOUT_ROOT: &'static str = r##" +Compute computations with your computer + + This commandline tool allows you to calculate complex mathematical expressions right in your + shell. +"##; +/// longer about section displayed in help, is combined with [the short help](ABOUT_ROOT) +static LONG_ABOUT_ROOT: &'static str = r##" + + libpt is a personal general purpose library, offering this executable, a python module and a + dynamic library. +"##; + +//// STATICS /////////////////////////////////////////////////////////////////////////////////////// +#[derive(Debug, Clone, Parser)] +#[command( + author, + version, + about = ABOUT_ROOT, + long_about = format!("{}{}", ABOUT_ROOT ,LONG_ABOUT_ROOT), + help_template = +r#"libpt: {version}{about-section}Author: +{author-with-newline} +{usage-heading} {usage}{all-args}{tab}"# + )] +pub struct Cli { + // clap_verbosity_flag seems to make this a global option implicitly + /// set a verbosity, multiple allowed (f.e. -vvv) + #[command(flatten)] + pub verbose: Verbosity, + + /// show logger meta + #[arg(short, long, global = true)] + pub log_meta: bool, + + /// your exporession(s) + #[clap(trailing_var_arg=true)] + pub expression: Vec, +} + +//// MACROS //////////////////////////////////////////////////////////////////////////////////////// + +//// ENUMS ///////////////////////////////////////////////////////////////////////////////////////// + +//// STRUCTS /////////////////////////////////////////////////////////////////////////////////////// + +//// IMPLEMENTATION //////////////////////////////////////////////////////////////////////////////// + +//// PUBLIC FUNCTIONS ////////////////////////////////////////////////////////////////////////////// + +//// PRIVATE FUNCTIONS ///////////////////////////////////////////////////////////////////////////// +fn main() { + let cli = Cli::parse(); + let ll: tracing::Level = match cli.verbose.log_level().unwrap().as_str() { + "TRACE" => tracing::Level::TRACE, + "DEBUG" => tracing::Level::DEBUG, + "INFO" => tracing::Level::INFO, + "WARN" => tracing::Level::WARN, + "ERROR" => tracing::Level::ERROR, + _ => { + eprintln!("'{}' is not a valid loglevel", cli.verbose.to_string()); + std::process::exit(1); + } + }; + if cli.log_meta { + Logger::init_customized( + false, + PathBuf::from("/dev/null"), + true, + false, + true, + true, + ll, + false, + false, + false, + ) + .expect("could not initialize Logger"); + } else { + // less verbose version + Logger::init_customized( + false, + PathBuf::from("/dev/null"), + true, + false, + true, + false, + ll, + false, + false, + false, + ) + .expect("could not initialize Logger"); + } + let mut expr: String = String::new(); + for part in cli.expression { + expr += ∂ + } + + info!("exporssion: {}", expr); +} +//////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/math/computer/mod.rs b/src/math/computer/mod.rs index a0fe4da..e5911b6 100644 --- a/src/math/computer/mod.rs +++ b/src/math/computer/mod.rs @@ -17,9 +17,8 @@ //// IMPORTS /////////////////////////////////////////////////////////////////////////////////////// pub mod result; -use result::{Error, Result}; +pub use result::{Error, Result, ComputeResult}; -use self::result::ComputeResult; use crate::logger::{trace, debug, info, warn, error}; //// TYPES ///////////////////////////////////////////////////////////////////////////////////////// @@ -78,6 +77,7 @@ impl Computer { /// This method only processes a single term at a time, without caching. pub fn compute(mut t: Term) -> Result { trace!("computing term {t:?}"); + return Ok(ComputeResult::from(0)) } } diff --git a/src/math/computer/result.rs b/src/math/computer/result.rs index 01b06de..ccc588a 100644 --- a/src/math/computer/result.rs +++ b/src/math/computer/result.rs @@ -75,6 +75,20 @@ impl From for NumericResult where } } +impl From for ComputeResult where + u128: TryFrom, + u128: TryFrom { + fn from(value: T) -> Self { + NumericResult::from(value).into() + } +} + +impl From for ComputeResult { + fn from(value: NumericResult) -> Self { + ComputeResult::Numerical(value) + } +} + //// PUBLIC FUNCTIONS ////////////////////////////////////////////////////////////////////////////// //// PRIVATE FUNCTIONS /////////////////////////////////////////////////////////////////////////////