karton/src/util/dbio.rs
Daniel Szabo 4cc737731a Multiple enhancements and bugfixes
!Breaking change! - The updated version will not be able to read your old database file

Major improvements:
- Added editable pastas
- Added private pastas
- Added line numbers
- Added support for wide mode (1080p instead of 720p)
- Added syntax highlighting support
- Added read-only mode
- Added built-in help page
- Added option to remove logo, change title and footer text

Minor improvements:
- Improved looks in pure html mode
- Removed link to GitHub repo from navbar
- Broke up 7km long main.rs file into smaller modules
- Moved water.css into a template instead of serving it as an external resource
- Made Save button a bit bigger
- Updated README.MD

Bugfixes:
- Fixed a bug where an incorrect animal ID in a request would cause a crash
- Fixed a bug where an empty or corrupt JSON database would cause a crash
2022-06-03 17:24:34 +01:00

56 lines
1.7 KiB
Rust

use std::fs::File;
use std::io;
use std::io::{BufReader, BufWriter};
use crate::Pasta;
static DATABASE_PATH: &'static str = "pasta_data/database.json";
pub fn save_to_file(pasta_data: &Vec<Pasta>) {
let mut file = File::create(DATABASE_PATH);
match file {
Ok(_) => {
let writer = BufWriter::new(file.unwrap());
serde_json::to_writer(writer, &pasta_data).expect("Failed to create JSON writer");
}
Err(_) => {
log::info!("Database file {} not found!", DATABASE_PATH);
file = File::create(DATABASE_PATH);
match file {
Ok(_) => {
log::info!("Database file {} created.", DATABASE_PATH);
save_to_file(pasta_data);
}
Err(err) => {
log::error!(
"Failed to create database file {}: {}!",
&DATABASE_PATH,
&err
);
panic!("Failed to create database file {}: {}!", DATABASE_PATH, err)
}
}
}
}
}
pub fn load_from_file() -> io::Result<Vec<Pasta>> {
let file = File::open(DATABASE_PATH);
match file {
Ok(_) => {
let reader = BufReader::new(file.unwrap());
let data: Vec<Pasta> = match serde_json::from_reader(reader) {
Ok(t) => t,
_ => Vec::new(),
};
Ok(data)
}
Err(_) => {
log::info!("Database file {} not found!", DATABASE_PATH);
save_to_file(&Vec::<Pasta>::new());
log::info!("Database file {} created.", DATABASE_PATH);
load_from_file()
}
}
}