2017-12-17 13:19:27 +01:00
|
|
|
use comrak;
|
|
|
|
use glob::glob;
|
|
|
|
use inflector::Inflector;
|
|
|
|
use std::fs::File;
|
|
|
|
use std::io::prelude::*;
|
|
|
|
use std::path::PathBuf;
|
2017-12-27 21:04:38 +01:00
|
|
|
use std::time::SystemTime;
|
2017-12-17 13:19:27 +01:00
|
|
|
|
2017-12-27 22:02:55 +01:00
|
|
|
/// Converts raw string data (in Markdown format) to HTML
|
2017-12-27 21:31:12 +01:00
|
|
|
pub fn data_to_html(data: &String) -> String {
|
|
|
|
comrak::markdown_to_html(data,
|
|
|
|
&comrak::ComrakOptions::default())
|
|
|
|
}
|
|
|
|
|
2017-12-23 22:02:46 +01:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
2017-12-27 22:02:55 +01:00
|
|
|
/// Structure for representing a wish list
|
2017-12-17 13:19:27 +01:00
|
|
|
pub struct List {
|
2017-12-27 22:02:55 +01:00
|
|
|
/// The ID of the list (unique string)
|
2017-12-23 22:02:46 +01:00
|
|
|
pub id: String,
|
2017-12-27 22:02:55 +01:00
|
|
|
/// The index of the list (unique number)
|
2017-12-27 17:32:39 +01:00
|
|
|
pub index: i8,
|
2017-12-27 22:02:55 +01:00
|
|
|
/// The raw list data
|
2017-12-23 22:02:46 +01:00
|
|
|
pub data: String,
|
2017-12-27 22:02:55 +01:00
|
|
|
/// The time the list was last modified
|
2017-12-27 21:04:38 +01:00
|
|
|
pub mtime: SystemTime,
|
2017-12-27 22:02:55 +01:00
|
|
|
/// The name of the list, i.e. the person it is for
|
2017-12-23 22:02:46 +01:00
|
|
|
pub name: String,
|
2017-12-27 22:02:55 +01:00
|
|
|
/// The path to the list file
|
2017-12-17 13:19:27 +01:00
|
|
|
path: PathBuf
|
|
|
|
}
|
|
|
|
|
|
|
|
impl List {
|
2017-12-23 22:03:25 +01:00
|
|
|
pub fn to_html(&self) -> String {
|
2017-12-27 21:31:12 +01:00
|
|
|
data_to_html(&self.data)
|
2017-12-23 22:03:25 +01:00
|
|
|
}
|
|
|
|
|
2017-12-27 17:33:28 +01:00
|
|
|
pub fn update_data(&mut self, data : &String) {
|
2017-12-27 20:56:54 +01:00
|
|
|
let mut file = File::create(&self.path)
|
|
|
|
.expect(&format!("Cannot open list file {}",
|
|
|
|
self.path.to_str().unwrap()));
|
|
|
|
file.write_all(data.as_bytes())
|
|
|
|
.expect(&format!("Cannot write list file {}",
|
|
|
|
self.path.to_str().unwrap()));
|
2017-12-27 21:04:38 +01:00
|
|
|
|
|
|
|
self.data = data.clone();
|
|
|
|
let metadata = file.metadata().unwrap();
|
|
|
|
self.mtime = metadata.modified().unwrap();
|
2017-12-27 17:33:28 +01:00
|
|
|
}
|
|
|
|
|
2017-12-17 13:19:27 +01:00
|
|
|
pub fn load_all() -> Vec<Self> {
|
|
|
|
let mut lists : Vec<List> = vec![];
|
2017-12-27 17:32:39 +01:00
|
|
|
let mut index = 0;
|
2017-12-17 13:19:27 +01:00
|
|
|
for entry in glob("lists/*.list").unwrap().filter_map(Result::ok) {
|
|
|
|
let file_name = entry.file_name().unwrap().to_str().unwrap();
|
|
|
|
let name = match file_name.find('.') {
|
|
|
|
Some(index) => &file_name[0..index],
|
|
|
|
None => "unknown"
|
|
|
|
};
|
|
|
|
let mut data = String::new();
|
|
|
|
let mut file = File::open(&entry)
|
|
|
|
.expect(&format!("Cannot open list file {}", file_name));
|
|
|
|
file.read_to_string(&mut data)
|
|
|
|
.expect(&format!("Cannot read list file {}", file_name));
|
2017-12-27 21:04:38 +01:00
|
|
|
let metadata = file.metadata()
|
|
|
|
.expect(&format!("Cannot get metadata of list file {}", file_name));
|
2017-12-17 13:19:27 +01:00
|
|
|
|
2017-12-27 17:33:28 +01:00
|
|
|
let mut list = List {
|
2017-12-23 22:03:25 +01:00
|
|
|
id: String::from(name),
|
2017-12-27 17:32:39 +01:00
|
|
|
index : index,
|
2017-12-17 13:19:27 +01:00
|
|
|
data: data,
|
2017-12-27 21:04:38 +01:00
|
|
|
mtime: metadata.modified().unwrap(),
|
2017-12-17 13:19:27 +01:00
|
|
|
name: String::from(name).to_title_case(),
|
|
|
|
path: entry.clone()
|
|
|
|
};
|
|
|
|
lists.push(list);
|
2017-12-27 17:32:39 +01:00
|
|
|
index += 1;
|
2017-12-17 13:19:27 +01:00
|
|
|
}
|
|
|
|
lists
|
|
|
|
}
|
|
|
|
}
|