rocket-pinboard/src/list.rs

44 lines
1.3 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 data: String,
pub name: String,
path: PathBuf
}
impl List {
pub fn load_all() -> Vec<Self> {
let mut lists : Vec<List> = vec![];
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 html = comrak::markdown_to_html(&data,
&comrak::ComrakOptions::default());
let list = List {
data: data,
html: html,
name: String::from(name).to_title_case(),
path: entry.clone()
};
lists.push(list);
}
lists
}
}