rusty_x/
snippet.rs

1use error;
2use std::fs::File;
3use std::io::{BufRead, BufReader};
4
5/**
6 * The snippet struct that has uses multiple tags, to order the snippets
7 */
8#[derive(Debug)]
9pub struct Snippet {
10    pub name: String,
11    pub tags: Vec<String>,
12}
13
14impl Snippet {
15    pub fn new(name: String, tags: &Vec<String>) -> Snippet {
16        Snippet {
17            name,
18            tags: tags.to_owned(),
19        }
20    }
21}
22
23pub fn read_tags(path: &str) -> Result<Vec<String>, error::Error> {
24    // Open the file
25    let f = File::open(path)?;
26    let mut file = BufReader::new(f);
27
28    // Read the first line of the file
29    let mut buffer = String::new();
30    file.read_line(&mut buffer)?;
31
32    // Read the tags, remove empty ones
33    let mut t: Vec<&str> = buffer.as_str().split(',').map(|s| s.trim()).collect();
34    t.retain(|s| !s.is_empty());
35
36    let tags: Vec<String> = t.iter().map(|s| String::from(s.to_owned())).collect();
37
38    // Return the tags found
39    Ok(tags)
40}