refactor: pull things into own files, have a library

This commit is contained in:
Khaïs COLIN 2025-05-02 20:35:45 +02:00
parent 4848f2be2f
commit ee23572983
7 changed files with 176 additions and 166 deletions

8
src/branding.rs Normal file
View file

@ -0,0 +1,8 @@
pub 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 2025 {authors}. All rights reserved.")
}

18
src/cli.rs Normal file
View file

@ -0,0 +1,18 @@
pub 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)
}
}

65
src/command.rs Normal file
View file

@ -0,0 +1,65 @@
use crate::meta_commands::{MetaCommand, MetaCommandParseError};
use crate::statements::{Statement, StatementParseError};
pub enum Command {
MetaCommand(MetaCommand),
Statement(Statement),
}
pub enum CommandParseError {
MetaCommand(MetaCommandParseError),
Statement(StatementParseError),
}
impl std::fmt::Display for CommandParseError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
CommandParseError::MetaCommand(meta_command_parse_error) => {
write!(f, "{meta_command_parse_error}")
}
CommandParseError::Statement(statement_parse_error) => {
write!(f, "{statement_parse_error}")
}
}
}
}
impl From<MetaCommand> for Command {
fn from(value: MetaCommand) -> Self {
Command::MetaCommand(value)
}
}
impl From<MetaCommandParseError> for CommandParseError {
fn from(value: MetaCommandParseError) -> Self {
CommandParseError::MetaCommand(value)
}
}
impl From<Statement> for Command {
fn from(value: Statement) -> Self {
Command::Statement(value)
}
}
impl From<StatementParseError> for CommandParseError {
fn from(value: StatementParseError) -> Self {
CommandParseError::Statement(value)
}
}
impl std::str::FromStr for Command {
type Err = CommandParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.starts_with(".") {
s.parse::<MetaCommand>()
.map(|x| x.into())
.map_err(|x| x.into())
} else {
s.parse::<Statement>()
.map(|x| x.into())
.map_err(|x| x.into())
}
}
}

5
src/lib.rs Normal file
View file

@ -0,0 +1,5 @@
pub mod branding;
pub mod cli;
pub mod command;
pub mod meta_commands;
pub mod statements;

View file

@ -1,133 +1,7 @@
enum MetaCommand { use osdb::branding::startup_msg;
Exit, use osdb::cli::read_input;
} use osdb::command::Command;
use osdb::meta_commands::MetaCommand;
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(),
}),
}
}
}
enum Statement {
Insert,
Select,
}
enum StatementParseError {
Unrecognized { stmt: String },
}
impl std::fmt::Display for StatementParseError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
StatementParseError::Unrecognized { stmt } => {
write!(f, "unrecognized statement {stmt:?}")
}
}
}
}
impl std::str::FromStr for Statement {
type Err = StatementParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let s = s.trim();
let lower = s.to_lowercase();
if lower.starts_with("insert") {
Ok(Statement::Insert)
} else if lower.starts_with("select") {
Ok(Statement::Select)
} else {
Err(StatementParseError::Unrecognized {
stmt: s.to_string(),
})
}
}
}
enum Command {
MetaCommand(MetaCommand),
Statement(Statement),
}
enum CommandParseError {
MetaCommand(MetaCommandParseError),
Statement(StatementParseError),
}
impl std::fmt::Display for CommandParseError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
CommandParseError::MetaCommand(meta_command_parse_error) => {
write!(f, "{meta_command_parse_error}")
}
CommandParseError::Statement(statement_parse_error) => {
write!(f, "{statement_parse_error}")
}
}
}
}
impl From<MetaCommand> for Command {
fn from(value: MetaCommand) -> Self {
Command::MetaCommand(value)
}
}
impl From<MetaCommandParseError> for CommandParseError {
fn from(value: MetaCommandParseError) -> Self {
CommandParseError::MetaCommand(value)
}
}
impl From<Statement> for Command {
fn from(value: Statement) -> Self {
Command::Statement(value)
}
}
impl From<StatementParseError> for CommandParseError {
fn from(value: StatementParseError) -> Self {
CommandParseError::Statement(value)
}
}
impl std::str::FromStr for Command {
type Err = CommandParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.starts_with(".") {
s.parse::<MetaCommand>()
.map(|x| x.into())
.map_err(|x| x.into())
} else {
s.parse::<Statement>()
.map(|x| x.into())
.map_err(|x| x.into())
}
}
}
fn main() { fn main() {
startup_msg(); startup_msg();
@ -140,44 +14,9 @@ fn main() {
break; break;
} }
}, },
Command::Statement(stmt) => execute_statment(stmt), Command::Statement(stmt) => stmt.execute(),
}, },
Err(err) => eprintln!("{err}"), Err(err) => eprintln!("{err}"),
} }
} }
} }
fn execute_statment(stmt: Statement) {
match stmt {
Statement::Insert => println!("insert"),
Statement::Select => println!("select"),
}
}
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 2025 {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)
}
}

30
src/meta_commands.rs Normal file
View file

@ -0,0 +1,30 @@
pub enum MetaCommand {
Exit,
}
pub 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(),
}),
}
}
}

45
src/statements.rs Normal file
View file

@ -0,0 +1,45 @@
pub enum Statement {
Insert,
Select,
}
pub enum StatementParseError {
Unrecognized { stmt: String },
}
impl std::fmt::Display for StatementParseError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
StatementParseError::Unrecognized { stmt } => {
write!(f, "unrecognized statement {stmt:?}")
}
}
}
}
impl std::str::FromStr for Statement {
type Err = StatementParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let s = s.trim();
let lower = s.to_lowercase();
if lower.starts_with("insert") {
Ok(Statement::Insert)
} else if lower.starts_with("select") {
Ok(Statement::Select)
} else {
Err(StatementParseError::Unrecognized {
stmt: s.to_string(),
})
}
}
}
impl Statement {
pub fn execute(&self) {
match self {
Statement::Insert => println!("insert"),
Statement::Select => println!("select"),
}
}
}