feat(osdb): basic read loop
This commit is contained in:
parent
6d0b96ef6c
commit
a9ab0fff01
3 changed files with 82 additions and 2 deletions
|
|
@ -2,5 +2,6 @@
|
|||
name = "osdb"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
authors = ["Khaïs COLIN"]
|
||||
|
||||
[dependencies]
|
||||
|
|
|
|||
9
src/build.rs
Normal file
9
src/build.rs
Normal file
|
|
@ -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);
|
||||
}
|
||||
74
src/main.rs
74
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<Self, Self::Err> {
|
||||
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<String> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue