From afe7e32a2a29fe6575b25729e84d74a37423cf77 Mon Sep 17 00:00:00 2001 From: jutty Date: Tue, 24 Mar 2026 18:30:10 -0300 Subject: [PATCH] Rust: Fulfill most of the spec --- rust/src/main.rs | 4 +- rust/src/run.rs | 120 ++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 106 insertions(+), 18 deletions(-) diff --git a/rust/src/main.rs b/rust/src/main.rs index e926ee0..51c26b7 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -1,6 +1,6 @@ use tori::{conf, log, run}; -fn main() { +fn main() -> std::process::ExitCode { log::elog(&format!("tori {}", env!("CARGO_PKG_VERSION"))); let configuration = conf::load(); log::elog(&format!("Configuration: {configuration:#?}")); @@ -8,4 +8,6 @@ fn main() { log::elog(&format!("Order: {order:#?}")); order.fill(); log::elog(&format!("Filled Order: {order:#?}")); + + if order.finished() { 0.into() } else { 1.into() } } diff --git a/rust/src/run.rs b/rust/src/run.rs index c77b9ed..bab96b1 100644 --- a/rust/src/run.rs +++ b/rust/src/run.rs @@ -1,17 +1,17 @@ // B2.1. DONE version | -v | --version -> MUST print the version as in v0.8.0 -// B2.2. TODO help | -h | --help -> MUST print '' -// B2.3. TODO os -> MUST print the OS name and MUST log contents of /etc/os-release -// B2.4. TODO user -> MUST print the output of the 'whoami' command +// B2.2. DONE help | -h | --help -> MUST print '' +// B2.3. DONE os -> MUST print the OS name and MUST log contents of /etc/os-release +// B2.4. DONE user -> MUST print the output of the 'whoami' command // B2.5. TODO pkg p -> MUST call the system package manager using the su_command // to install and then uninstall package p. The user MUST be able to // freely input to these commands' interactive inputs before control // is returned. When done, it MUST log 'Done:', a newline, and the // system commands executed, one per line. If no p is provided, it // MUST NOT run any system commands and print a message -// B2.6. TODO echo x y z -> MUST print x y z -// B2.7. TODO echo -> MUST NOT print any output and exit with status code 0 +// B2.6. DONE echo x y z -> MUST print x y z +// B2.7. DONE echo -> MUST NOT print any output and exit with status code 0 // B2.8. DONE [no input] -> MUST NOT print any output and exit with status code 0 -// B2.9. TODO [any other input] -> MUST print 'Unrecognized command: [command]', +// B2.9. DONE [any other input] -> MUST print 'Unrecognized command: [command]', // a newline, '' and exit with status code 1 #[derive(Default, Debug)] @@ -28,8 +28,12 @@ impl Order { } } - fn is_complete(&self) -> bool { - self.tasks.iter().any(|e| !e.done) + pub fn finished(&self) -> bool { + if self.tasks.is_empty() { + true + } else { + self.tasks.iter().all(|e| e.done) + } } } @@ -37,6 +41,7 @@ impl Order { pub struct Task { kind: TaskKind, done: bool, + argument: String, parameters: Vec, } @@ -47,14 +52,20 @@ impl Task { self.done = match self.kind { Version => { meta::print_version() }, + Help => { meta::print_help() }, + OsInfo => { os::print_info() }, + User => { os::print_user() }, + Echo => { meta::echo(self) }, + Unrecognized => { meta::unrecognized(self) }, _ => false, // TODO } } - fn new(kind: TaskKind, parameters: Vec) -> Task { + fn new(kind: TaskKind, argument: String, parameters: Vec) -> Task { Task { kind, done: false, + argument, parameters, } } @@ -63,10 +74,12 @@ impl Task { #[derive(Debug, Clone)] pub enum TaskKind { Version, - OperatingSystem, + Help, + OsInfo, Package, User, Echo, + Unrecognized, } pub mod teller { @@ -102,21 +115,24 @@ pub mod teller { if argument == "version" || argument == "-v" || argument == "--version" { elog("Command is 'version'"); - Order { tasks: vec![Task::new(Version, parameters)] } + Order { tasks: vec![Task::new(Version, argument, parameters)] } + } else if argument == "help" { + elog("Command is 'help'"); + Order { tasks: vec![Task::new(Help, argument, parameters)] } } else if argument == "os" { elog("Command is 'os'"); - Order { tasks: vec![Task::new(OperatingSystem, parameters)] } + Order { tasks: vec![Task::new(OsInfo, argument, parameters)] } } else if argument == "pkg" { elog("Command is 'pkg'"); - Order { tasks: vec![Task::new(Package, parameters)] } + Order { tasks: vec![Task::new(Package, argument, parameters)] } } else if argument == "user" { elog("Command is 'user'"); - Order { tasks: vec![Task::new(User, parameters)] } + Order { tasks: vec![Task::new(User, argument, parameters)] } } else if argument == "echo" { elog("Command is 'echo'"); - Order { tasks: vec![Task::new(Echo, parameters)] } + Order { tasks: vec![Task::new(Echo, argument, parameters)] } } else { - Order::default() + Order { tasks: vec![Task::new(Unrecognized, argument, parameters)] } } } @@ -166,12 +182,82 @@ pub mod expeditor {} pub mod exec { pub mod meta { + use crate::run::Task; + pub fn print_version() -> bool { println!("v{}", env!("CARGO_PKG_VERSION")); true } + + pub fn print_help() -> bool { + println!(""); + true + } + + pub fn echo(task: &Task) -> bool { + if !task.parameters.is_empty() { + let text = task.parameters.join(" "); + println!("{text}"); + } + true + } + + pub fn unrecognized(task: &Task) -> bool { + println!("Unrecognized command: {}\n", task.argument); + false + } } - pub mod os {} + + pub mod os { + use crate::log::elog; + + pub fn print_info() -> bool { + use std::process::Command; + + let uname_success = if let Ok(output) = Command::new("uname") + .arg("--operating-system") + .output() { + if let Ok(utf8) = String::from_utf8(output.stdout) { + print!("{utf8}"); + true + } else { + elog("Failed UTF8 coversion of uname output"); + false + } + } else { + elog("Failed executing or reading output of uname"); + false + }; + + let os_release_success = if let Ok(os_release) = std::fs::read_to_string("/etc/os-release") { + elog(&os_release); + true + } else { + elog("Failed reading os-release"); + false + }; + + uname_success && os_release_success + } + + pub fn print_user() -> bool { + use std::process::Command; + + if let Ok(output) = Command::new("whoami").output() { + if let Ok(utf8) = String::from_utf8(output.stdout) { + print!("{utf8}"); + true + } else { + elog("Failed UTF8 coversion of whoami output"); + false + } + } else { + elog("Failed executing or reading output of whoami"); + false + } + } + } + pub mod shell {} pub mod pkg {} }