Browse Source

some commit

db-refactoring
Coin de Gamma 4 months ago
parent
commit
27bef910a8
  1. 39
      src/main.rs

39
src/main.rs

@ -2,9 +2,9 @@ use clap::{Parser, Subcommand};
use std::fs; use std::fs;
use std::io; use std::io;
use std::fmt;
use std::path::Path; use std::path::Path;
const STORAGE_PATH: &str = "storage"; const STORAGE_PATH: &str = "storage";
@ -29,41 +29,24 @@ enum Commands {
} }
enum RuntimeError { enum InitError {
Reinit, Reinit,
BadInit BadInit
} }
impl fmt::Display for RuntimeError { fn is_inited() -> bool {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
RuntimeError::Reinit => write!(f, "Initialization can't be second time. Remove store folder if you want new inizilization"),
RuntimeError::BadInit => write!(f, "Can`t create storage folder")
}
}
}
impl From<RuntimeError> for std::io::Error {
fn from(err: RuntimeError) -> std::io::Error {
match err {
RuntimeError::Reinit => std::io::Error::new(std::io::ErrorKind::AlreadyExists, "Reinitialization attempted"),
RuntimeError::BadInit => std::io::Error::new(std::io::ErrorKind::Other, "Bad initialization"),
}
}
}
fn init() -> Result<(), RuntimeError> {
let path = Path::new(STORAGE_PATH); let path = Path::new(STORAGE_PATH);
if path.exists() { return path.exists();
return Err(RuntimeError::Reinit);
} }
fn init() -> Result<(), InitError> {
if is_inited() {
return Err(InitError::Reinit);
}
match fs::create_dir(STORAGE_PATH) { match fs::create_dir(STORAGE_PATH) {
Ok(_) => return Ok(()), Ok(_) => return Ok(()),
Err(_) => return Err(RuntimeError::BadInit), Err(_) => return Err(InitError::BadInit),
} }
Ok(())
} }
fn main() -> io::Result<()> { fn main() -> io::Result<()> {
@ -71,8 +54,8 @@ fn main() -> io::Result<()> {
match &cli.command { match &cli.command {
Some(Commands::Init) => { Some(Commands::Init) => {
init().map_err(|e| match e { init().map_err(|e| match e {
RuntimeError::Reinit => std::io::Error::new(std::io::ErrorKind::AlreadyExists, "Reinitialization attempted"), InitError::Reinit => std::io::Error::new(std::io::ErrorKind::AlreadyExists, "Reinitialization attempted"),
RuntimeError::BadInit => std::io::Error::new(std::io::ErrorKind::Other, "Bad initialization"), InitError::BadInit => std::io::Error::new(std::io::ErrorKind::Other, "Bad initialization"),
})?; })?;
println!("Initializing storage and config."); println!("Initializing storage and config.");
} }

Loading…
Cancel
Save