--- /dev/null
+use std::{collections::HashMap, io::{Read, Write}, path::Path};
+
+struct Property {
+ props: HashMap<String, String>,
+ path: String,
+}
+
+impl Property {
+ pub fn init(path: &Path) -> Result<Self, std::io::Error> {
+ let maybe_file = std::fs::File::open(path);
+ match maybe_file {
+ Ok(mut file) => {
+ // each line is of the form prop=value
+ let mut buf = String::new();
+ let mut props = HashMap::new();
+ file.read_to_string(&mut buf).unwrap();
+ let words: Vec<&str> = buf.split('\n').collect();
+ words.iter().for_each(|w| {
+ let kvs = w.split('=').collect::<Vec<&str>>();
+ props.insert(kvs[0].to_string(), kvs[1].to_string());
+ });
+ Ok(Property{
+ props: props,
+ path: path.to_string_lossy().to_string(),
+ })
+ },
+ Err(e) => {
+ println!("file path {} does not exist", path.to_str().unwrap());
+ Err(e)
+ }
+ }
+ }
+
+ // save the table into the resource indicated by the path
+ pub fn save(&self) -> Result<(), std::io::Error>{
+ let mut file = std::fs::File::create(&self.path)?;
+
+ for (key, value) in self.props.iter() {
+ let line = format!("{}={}", key, value);
+ let _ = file.write(&line.as_bytes());
+ }
+ Ok(())
+ }
+
+ pub fn get(&self, s: &str) -> Option<&str> {
+ self.props.get(s).map(|x| x.as_str())
+ }
+
+ pub fn set(&mut self, k: &str, v: &str) {
+ self.props.insert(k.to_string(), v.to_string());
+ }
+
+ pub fn clear_all(&mut self) {
+ self.props = HashMap::new();
+ }
+}
+