use std::fs;
pub(crate) struct Database {
file: Option<fs::File>,
}
impl Database {
pub(crate) fn open(filename: &str) -> Database {
let file = match std::fs::File::open(&filename) {
Err(_) => match std::fs::File::create(&filename) {
Err(err_create) => panic!("Can not open or create {}: {}.", &filename, err_create),
Ok(created_file) => {
println!("Created new database file at {}.", &filename);
Some(created_file)
}
},
Ok(file) => Some(file),
};
Database { file }
}
}
#[cfg(test)]
mod tests {
#[test]
fn load_existing_file() {
let file = tempfile::NamedTempFile::new().unwrap();
let _ = super::Database::open(file.path().to_str().unwrap());
}
}