use super::storage::Storage;
use super::toml;
use std::path::PathBuf;
use std::fs;
use std::io;
pub struct Node<'a, 'b: 'a> {
storage: &'a Storage<'b>,
id: u64
}
impl<'a, 'b> Node<'a, 'b> {
pub fn new(storage: &'a Storage<'b>, id: u64) -> Node<'a, 'b> {
Node{storage, id}
}
pub fn id(&self) -> u64 {
self.id
}
pub fn load_meta(&self) -> Result<toml::Value, toml::LoadError> {
<toml::Value as toml::ValueImpl>::load(self.meta_path())
}
pub fn node_path(&self) -> PathBuf {
let mut pb = self.storage.path().clone();
pb.push("nodes");
pb.push(&self.id.to_string());
pb
}
pub fn meta_path(&self) -> PathBuf {
let mut pb = self.storage.path().clone();
pb.push("meta");
pb.push(&self.id.to_string());
pb
}
pub fn exists(&self) -> bool {
if self.node_path().exists() {
if self.meta_path().exists() {
true
} else {
println!("Node {} has no meta file", self.id);
false
}
} else {
if self.node_path().exists() {
println!("Meta file for non-existent node {}", self.id);
}
false
}
}
pub fn storage(&self) -> &Storage<'b> {
self.storage
}
pub fn remove(&self) -> io::Result<()> {
fs::remove_file(self.node_path())?;
fs::remove_file(self.meta_path())
}
}