Skip to content

Commit

Permalink
integrated local crate inside project
Browse files Browse the repository at this point in the history
  • Loading branch information
Lonca Emmanuel committed Jul 15, 2024
1 parent 15b2431 commit fc5dccc
Show file tree
Hide file tree
Showing 19 changed files with 74 additions and 902 deletions.
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ debug = true
[dependencies]
anyhow = "1.0"
bitvec = "1.0"
crusti_app_helper = {path = "local_crates/crusti_app_helper-v0.1"}
chrono = "0.4.19"
clap = "2.33.3"
fern = { version = "0.6.0", features = ["colored"] }
log = "0.4.20"
rug = "1.22"
rustc-hash = "1.1"
sysinfo = "0.30"
11 changes: 0 additions & 11 deletions local_crates/crusti_app_helper-v0.1/CHANGELOG.md

This file was deleted.

15 changes: 0 additions & 15 deletions local_crates/crusti_app_helper-v0.1/Cargo.toml

This file was deleted.

674 changes: 0 additions & 674 deletions local_crates/crusti_app_helper-v0.1/LICENSE

This file was deleted.

103 changes: 0 additions & 103 deletions local_crates/crusti_app_helper-v0.1/src/app_helper/license_command.rs

This file was deleted.

2 changes: 0 additions & 2 deletions local_crates/crusti_app_helper-v0.1/src/app_helper/mod.rs

This file was deleted.

5 changes: 0 additions & 5 deletions local_crates/crusti_app_helper-v0.1/src/cli_manager/mod.rs

This file was deleted.

33 changes: 0 additions & 33 deletions local_crates/crusti_app_helper-v0.1/src/lib.rs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

use crate::cli_manager::{cli_manager::CliManager, command::Command};
use crate::app::{cli_manager::CliManager, command::Command};
use anyhow::Result;
use log::{error, info};
use std::{ffi::OsString, sync::Once, time::SystemTime};
Expand Down Expand Up @@ -76,7 +76,7 @@ impl<'a> AppHelper<'a> {
///
/// This function consumes the helper.
pub fn launch_app(self) {
self.launch_app_with_args(std::env::args_os())
self.launch_app_with_args(std::env::args_os());
}

/// Launch the application.
Expand Down Expand Up @@ -120,7 +120,7 @@ impl<'a> AppHelper<'a> {
}

pub fn init_logger() {
init_logger_with_level(log::LevelFilter::Info)
init_logger_with_level(log::LevelFilter::Info);
}

pub fn init_logger_with_level(level: log::LevelFilter) {
Expand All @@ -133,7 +133,7 @@ pub fn init_logger_with_level(level: log::LevelFilter) {
colors.color(record.level()),
chrono::Local::now().format("[%Y-%m-%d %H:%M:%S]"),
message
))
));
})
.level(level)
.chain(std::io::stdout())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.

use super::{command::Command, writable_string::WritableString};
use crate::{init_logger, app_helper::app_helper::init_logger_with_level};
use crate::app::app_helper;
use anyhow::{anyhow, Result};
use clap::{App, AppSettings, Arg};
use log::info;
use std::{ffi::OsString, str::FromStr};
use sysinfo::{ProcessorExt, System, SystemExt};
use sysinfo::System;

/// A structure used to handle the set of commands and to process the CLI arguments against them.
pub(crate) struct CliManager<'a> {
Expand Down Expand Up @@ -71,15 +71,15 @@ impl<'a> CliManager<'a> {
.version(self.version)
.author(self.author)
.about(self.about);
for c in self.commands.iter() {
for c in &self.commands {
app = app.subcommand(c.clap_subcommand());
}
let matches_result = app
.clone()
.get_matches_from_safe(&mut args.clone().into_iter());
match matches_result {
Ok(matches) => {
for c in self.commands.iter() {
for c in &self.commands {
if let Some(matches) = matches.subcommand_matches(c.name()) {
let log_level = if let Some(str_log_level) =
matches.value_of(APP_HELPER_LOGGING_LEVEL_ARG)
Expand All @@ -88,7 +88,7 @@ impl<'a> CliManager<'a> {
} else {
log::LevelFilter::Info
};
init_logger_with_level(log_level);
app_helper::init_logger_with_level(log_level);
info!("{} {}", self.app_name, self.version);
sys_info();
return c.execute(matches);
Expand All @@ -100,12 +100,12 @@ impl<'a> CliManager<'a> {
kind: clap::ErrorKind::HelpDisplayed,
..
}) => {
init_logger();
app_helper::init_logger();
self.print_help(&mut app, args.as_slice());
Ok(())
}
Err(e) => {
init_logger();
app_helper::init_logger();
info!("{} {}", self.app_name, self.version);
Err(anyhow!("{}", e))
}
Expand All @@ -117,30 +117,30 @@ impl<'a> CliManager<'a> {
T: Into<OsString> + Clone,
{
const HELP_STRINGS: [&str; 3] = ["help", "-h", "--help"];
fn print_message(message: WritableString) {
fn print_message(message: &WritableString) {
message.to_string().split('\n').for_each(|s| info!("{}", s));
info!("");
}
fn search_subcommand(commands: &[Box<dyn Command>], subcommand_arg: &str) -> bool {
for c in commands.iter() {
for c in commands {
if c.name() == subcommand_arg {
let mut message = WritableString::default();
c.clap_subcommand().write_long_help(&mut message).unwrap();
print_message(message);
print_message(&message);
return true;
}
}
panic!("unreachable") // kcov-ignore
}
if args.len() >= 2 {
let arg1 = args[1].clone().into().into_string().unwrap();
if !HELP_STRINGS.contains(&arg1.as_ref() as &&str)
&& search_subcommand(&self.commands, &arg1)
let second_arg = args[1].clone().into().into_string().unwrap();
if !HELP_STRINGS.contains(&second_arg.as_ref() as &&str)
&& search_subcommand(&self.commands, &second_arg)
{
return;
}
if args.len() >= 3
&& HELP_STRINGS.contains(&arg1.as_ref() as &&str)
&& HELP_STRINGS.contains(&second_arg.as_ref() as &&str)
&& search_subcommand(
&self.commands,
args[2].clone().into().into_string().as_ref().unwrap(),
Expand All @@ -151,23 +151,23 @@ impl<'a> CliManager<'a> {
}
let mut message = WritableString::default();
app.write_long_help(&mut message).unwrap();
print_message(message);
print_message(&message);
}
}

fn sys_info() {
info!("----------------------------------------");
let sys = System::new_all();
let unknown = || "[unknown]".to_string();
info!("running on {}", sys.host_name().unwrap_or_else(unknown));
info!("running on {}", System::host_name().unwrap_or_else(unknown));
info!(
"OS is {} {} with kernel {}",
sys.name().unwrap_or_else(unknown),
sys.os_version().unwrap_or_else(unknown),
sys.kernel_version().unwrap_or_else(unknown)
System::name().unwrap_or_else(unknown),
System::os_version().unwrap_or_else(unknown),
System::kernel_version().unwrap_or_else(unknown)
);
let mut processor_kinds: Vec<&str> = sys.processors().iter().map(|p| p.brand()).collect();
processor_kinds.sort();
let mut processor_kinds: Vec<&str> = sys.cpus().iter().map(sysinfo::Cpu::brand).collect();
processor_kinds.sort_unstable();
processor_kinds.dedup();
info!(
"physical core count: {} {:?}",
Expand Down Expand Up @@ -219,9 +219,9 @@ mod tests {
}
}

fn test_local_command_result(
args: Vec<&'static str>,
) -> Result<(Rc<RefCell<bool>>, Rc<RefCell<bool>>)> {
type LocalCmdResult = Result<(Rc<RefCell<bool>>, Rc<RefCell<bool>>)>;

fn test_local_command_result(args: Vec<&'static str>) -> LocalCmdResult {
let mut manager = CliManager::new("app_name", "app_version", "author", "about");
let command_involved = Rc::new(RefCell::new(false));
let argument_set = Rc::new(RefCell::new(false));
Expand Down
File renamed without changes.
7 changes: 3 additions & 4 deletions src/app/common.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use anyhow::{anyhow, Context, Result};
use crusti_app_helper::{info, warn, Arg, ArgMatches};
use clap::{Arg, ArgMatches};
use decdnnf_rs::{CheckingVisitorData, D4Reader, DecisionDNNF, Literal};
use log::{info, warn};
use std::{
fs::{self, File},
io::BufReader,
Expand Down Expand Up @@ -31,9 +32,7 @@ pub(crate) fn arg_n_vars<'a>() -> Arg<'a, 'a> {
)
}

pub(crate) fn read_input_ddnnf(
arg_matches: &crusti_app_helper::ArgMatches<'_>,
) -> Result<DecisionDNNF> {
pub(crate) fn read_input_ddnnf(arg_matches: &ArgMatches<'_>) -> Result<DecisionDNNF> {
let file_reader = create_input_file_reader(arg_matches)?;
let mut ddnnf = D4Reader::read(file_reader).context("while parsing the input Decision-DNNF")?;
if let Some(str_n) = arg_matches.value_of(ARG_N_VARS) {
Expand Down
8 changes: 8 additions & 0 deletions src/app/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
pub(crate) mod app_helper;

pub(crate) mod cli_manager;

pub(crate) mod command;

mod common;

mod model_computer;
Expand All @@ -11,3 +17,5 @@ pub(crate) use model_enumeration::Command as ModelEnumerationCommand;

mod translation;
pub(crate) use translation::Command as TranslationCommand;

pub(crate) mod writable_string;
Loading

0 comments on commit fc5dccc

Please sign in to comment.