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) {
                // TODO: Propagate error.
                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]
    // TODO: Put real data into the file.
    // TODO: Add tests with existing inputs in the repository (for non-binary formats).
    fn load_existing_file() {
        let file = tempfile::NamedTempFile::new().unwrap();
        let _ = super::Database::open(file.path().to_str().unwrap());
    }
}
