use bitflags::bitflags;
use std::collections::{HashMap, HashSet};
use std::fs;
use std::io;
use std::path::{Path, PathBuf};
use std::result;
bitflags!{
struct PermissionSet: u8 {
const READ = 0x01;
const WRITE = 0x02;
const LOG = 0x04;
}
}
pub type Id = String;
pub struct Attribute {
current_state: String, parent: Option<Id>,
base_dir: PathBuf,
participants: HashMap<Id, PermissionSet>,
children: HashSet<Id>,
log_buffer: io::BufWriter<fs::File>,
}
impl Attribute {
fn new<P: AsRef<Path>>(id: Id, parent_path: P, parent: Option<Id>) -> Self {
let base_dir = parent_path.as_ref().join(id);
fs::create_dir(&base_dir).unwrap_or(());
let file_log =
fs::File::create(&base_dir.join("log")).unwrap_or(panic!("Could not open file"));
let log_buffer = io::BufWriter::new(file_log);
Attribute {
current_state: String::new(),
base_dir,
log_buffer,
parent,
participants: HashMap::new(),
children: HashSet::new(),
}
}
fn add_participant(&self, id: Id, permissions: PermissionSet) -> AttributeResult {
unimplemented!();
}
fn remove_participant(&mut self, participant_id: Id) -> Option<Id> {
match self.participants.remove(&participant_id) {
Some(_) => Some(participant_id),
None => None,
}
}
}
type AttributeResult = result::Result<Attribute, io::Error>;