lila-chat/src/file_io.rs

130 lines
4.0 KiB
Rust
Raw Normal View History

2021-07-17 19:53:10 +00:00
use std::fs::{File, OpenOptions};
use std::io::prelude::*;
use std::io::{self, BufRead};
use std::path::Path;
extern crate log;
use crate::user::User;
use serde_json::Result;
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
2021-07-18 00:33:22 +00:00
where
P: AsRef<Path>,
{
2021-07-17 19:53:10 +00:00
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}
// Function to read json from file into the vector
pub fn read_json() -> Vec<User> {
// Create path to file
let path = Path::new("users.json");
let display = path.display();
let mut users: Vec<User> = Vec::new(); // Create an empty vector of users
// Read through the lines and append them to the array
if let Ok(lines) = read_lines(&path) {
for line in lines {
if let Ok(user) = line {
info!("read {} from json file {}", display, &user);
// Parse line from file into a data structure
let user: User = serde_json::from_str(&user).unwrap();
users.push(user);
}
}
}
return users;
}
// Function to append the last value of the users vector to the file
2021-07-18 17:17:48 +00:00
pub fn append_json(user: &User) -> Result<()> {
2021-07-17 19:53:10 +00:00
// Create a file to write to
let path = Path::new("users.json");
let display = path.display();
let mut file = match OpenOptions::new()
2021-07-18 00:33:22 +00:00
.write(true)
.create(true)
.append(true)
.open(&path)
{
2021-07-17 19:53:10 +00:00
Err(why) => panic!("couldn't create {}: {}", display, why),
Ok(file) => file,
};
// Serialize the last user value
2021-07-18 17:17:48 +00:00
let user_json = serde_json::to_string(&user)?;
2021-07-17 19:53:10 +00:00
// Write to the file
2021-07-18 17:17:48 +00:00
match file.write_all(user_json.as_bytes()) {
2021-07-17 19:53:10 +00:00
Err(why) => panic!("couldn't write to {}: {}", display, why),
Ok(_) => info!("succesfully wrote to {}", display),
};
// Add newline
match file.write_all("\n".as_bytes()) {
Err(why) => panic!("couldn't write to {}: {}", display, why),
2021-07-18 18:11:54 +00:00
Ok(_) => info!("succesfully wrote newline to {}", display),
2021-07-17 19:53:10 +00:00
};
Ok(())
}
// Function to write whole vector of users to file
pub fn write_json(users_list: &Vec<User>) -> Result<()> {
// Create a file to write to
let path = Path::new("users.json");
let display = path.display();
2021-07-18 00:33:22 +00:00
let mut file = match OpenOptions::new().write(true).create(true).open(&path) {
2021-07-17 19:53:10 +00:00
Err(why) => panic!("couldn't create {}: {}", display, why),
Ok(file) => file,
};
2021-07-18 00:33:22 +00:00
2021-07-17 19:53:10 +00:00
let mut users_json = String::new();
for i in 0..users_list.len() {
// Serialize the users
users_json += &serde_json::to_string(&users_list[i])?;
2021-07-18 20:35:28 +00:00
if i != users_list.len()-1 {
2021-07-18 00:33:22 +00:00
// don't append newline if it's the last element
2021-07-17 19:53:10 +00:00
users_json += "\n";
2021-07-18 20:35:28 +00:00
}
2021-07-17 19:53:10 +00:00
}
// Write to the file
match file.write_all(users_json.as_bytes()) {
Err(why) => panic!("couldn't write to {}: {}", display, why),
Ok(_) => info!("succesfully wrote to {}", display),
};
Ok(())
}
2021-07-22 15:01:07 +00:00
// add a user to the database
pub fn db_add(user: &User) {
let db: sled::Db = sled::open("users_db").unwrap();
let bytes = bincode::serialize(&user).unwrap();
db.insert(&user.name, bytes).unwrap();
info!("succesfully appended user {} to database", &user.name);
}
2021-07-20 15:08:43 +00:00
2021-07-22 15:01:07 +00:00
// write all changed users to database
pub fn db_write(users_list: &Vec<User>) {
let db: sled::Db = sled::open("users_db").unwrap();
for i in users_list {
let bytes = bincode::serialize(&i).unwrap();
db.insert(&i.name, bytes).unwrap();
info!("wrote user {} to db", &i.name);
}
info!("wrote all users to db");
}
// read all users from the database
pub fn db_read() -> Vec<User> {
let db: sled::Db = sled::open("users_db").unwrap();
let mut users: Vec<User> = Vec::new();
for (_, bytes) in db.iter().filter_map(|r| r.ok()) {
let read_user: User = bincode::deserialize(&bytes).unwrap();
info!("read user {} from db", read_user.name);
users.push(read_user);
}
return users;
}