From a9ab0fff013ca8d8d5d1d60ea655feff1255bcec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Thu, 1 May 2025 21:00:28 +0200 Subject: [PATCH] feat(osdb): basic read loop --- Cargo.toml | 1 + src/build.rs | 9 +++++++ src/main.rs | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 src/build.rs diff --git a/Cargo.toml b/Cargo.toml index b4adac2..f1dfdcc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,5 +2,6 @@ name = "osdb" version = "0.1.0" edition = "2024" +authors = ["Khaïs COLIN"] [dependencies] diff --git a/src/build.rs b/src/build.rs new file mode 100644 index 0000000..85070ac --- /dev/null +++ b/src/build.rs @@ -0,0 +1,9 @@ +use std::process::Command; +fn main() { + let output = Command::new("git") + .args(&["rev-parse", "HEAD"]) + .output() + .unwrap(); + let git_hash = String::from_utf8(output.stdout).unwrap(); + println!("cargo:rustc-env=GIT_HASH={}", git_hash); +} diff --git a/src/main.rs b/src/main.rs index e7a11a9..51c4b5f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,73 @@ -fn main() { - println!("Hello, world!"); +enum MetaCommand { + Exit, +} + +enum MetaCommandParseError { + Unrecognized { cmd: String }, +} + +impl std::fmt::Display for MetaCommandParseError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + MetaCommandParseError::Unrecognized { cmd } => { + write!(f, "unrecognized meta-command {cmd:?}") + } + } + } +} + +impl std::str::FromStr for MetaCommand { + type Err = MetaCommandParseError; + + fn from_str(s: &str) -> Result { + match s.trim() { + ".exit" => Ok(MetaCommand::Exit), + cmd => Err(MetaCommandParseError::Unrecognized { + cmd: cmd.to_string(), + }), + } + } +} + +fn main() { + startup_msg(); + while let Some(input) = read_input() { + match input.parse() { + Ok(cmd) => match cmd { + MetaCommand::Exit => { + println!("Good-bye"); + break; + } + }, + Err(err) => eprintln!("{err}"), + } + } +} + +fn startup_msg() { + let name = env!("CARGO_PKG_NAME"); + let version = env!("CARGO_PKG_VERSION"); + let authors = env!("CARGO_PKG_AUTHORS"); + + println!("{name} v{version} started.",); + println!("Copyright 2024 {authors}. All rights reserved.") +} + +fn read_input() -> Option { + use std::io::{BufRead, Write}; + + print!("osdb > "); + std::io::stdout().flush().expect("failed to flush stdout"); + + let mut input = String::new(); + let len = std::io::stdin() + .lock() + .read_line(&mut input) + .expect("failed to read input from stdin"); + + if len == 0 { + None + } else { + Some(input) + } }