use serde::{Deserialize, Serialize};
use std::{env, fs, path::Path};
mod test;
pub struct JsonDB {
path: String,
}
impl JsonDB {
pub fn init(path: &str) -> Result<JsonDB, Box<dyn std::error::Error>> {
let db_path = format!("{}/.JsonDB/{}/", env::var("HOME")?, path);
if !Path::new(&*format!("{}/.JsonDB/{}/", env::var("HOME")?, path)).is_dir() {
fs::create_dir_all(format!("{}/.JsonDB/{}/", env::var("HOME")?, path))?;
}
Ok(JsonDB { path: db_path })
}
pub fn create_collection<S>(
self,
collection_path: S,
) -> Result<JsonDB, Box<dyn std::error::Error>>
where
S: Into<String>,
{
fs::create_dir(format!("{}/{}", self.path, collection_path.into()))?;
Ok(self)
}
pub fn write<J, S>(
self,
collection_path: S,
document: S,
data: J,
) -> Result<JsonDB, Box<dyn std::error::Error>>
where
J: Serialize,
S: Into<String>,
{
let serialized = serde_json::to_string(&data)?;
fs::write(
format!(
"{}/{}/{}.data.json",
self.path,
collection_path.into(),
document.into()
),
serialized,
)?;
Ok(self)
}
pub fn read<D, S>(
self,
collection_path: S,
document: S,
) -> Result<D, Box<dyn std::error::Error>>
where
for<'a> D: Deserialize<'a>,
S: Into<String>,
{
let data = serde_json::from_str::<D>(
fs::read_to_string(format!(
"{}/{}/{}.data.json",
self.path,
collection_path.into(),
document.into()
))?
.as_str(),
)?;
Ok(data)
}
pub fn delete<S>(
self,
collection_path: S,
document: S,
) -> Result<JsonDB, Box<dyn std::error::Error>>
where
S: Into<String>,
{
fs::remove_file(format!(
"{}/{}/{}.data.json",
self.path,
collection_path.into(),
document.into()
))?;
Ok(self)
}
pub fn list<S>(self, collection_path: S) -> Result<Vec<String>, Box<dyn std::error::Error>>
where
S: Into<String>,
{
let mut list: Vec<String> = Vec::new();
for file in fs::read_dir(format!("{}/{}/", self.path, collection_path.into()))? {
let mut value = file?.file_name().into_string().unwrap();
let len = value.chars().count() - 10;
let string = String::from(value.drain(0..len).as_str());
list.push(string);
}
Ok(list)
}
}