rocket-pinboard/src/list.rs

51 lines
1.4 KiB
Rust
Raw Normal View History

use comrak;
use glob::glob;
use inflector::Inflector;
use std::fs::File;
use std::io::prelude::*;
use std::path::PathBuf;
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct List {
pub id: String,
pub index: i8,
pub data: String,
pub name: String,
path: PathBuf
}
impl List {
pub fn to_html(&self) -> String {
comrak::markdown_to_html(&self.data,
&comrak::ComrakOptions::default())
}
pub fn load_all() -> Vec<Self> {
let mut lists : Vec<List> = vec![];
let mut index = 0;
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));
let list = List {
id: String::from(name),
index : index,
data: data,
name: String::from(name).to_title_case(),
path: entry.clone()
};
lists.push(list);
index += 1;
}
lists
}
}