Rust: Fulfill most of the spec

This commit is contained in:
Juno Takano 2026-03-24 18:30:10 -03:00
commit afe7e32a2a
2 changed files with 106 additions and 18 deletions

View file

@ -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() }
}

View file

@ -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 '<long help>'
// 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 '<long help>'
// 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, '<short help>' 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<String>,
}
@ -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<String>) -> Task {
fn new(kind: TaskKind, argument: String, parameters: Vec<String>) -> 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!("<long help>");
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<short help>", 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 {}
}