aboutsummaryrefslogtreecommitdiff
path: root/src/deck.rs
blob: a414a02149d18a942c50b1405964d25d1aff0450 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use crate::{model::entry::Entry, util::serialization};
use anyhow::{Error, Result};
use std::fmt;
use std::fs::File;
use std::io::{prelude::*, BufReader};
use std::path::Path;

#[derive(Debug, Clone)]
struct ParseError {
    line: usize,
    message: String,
}

impl fmt::Display for ParseError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{} (parsing line {})", self.message, self.line)
    }
}

impl std::error::Error for ParseError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        None
    }
}

pub fn read(deck: &str) -> Result<Vec<Entry>> {
    let file = File::open(deck)?;
    let reader = BufReader::new(file);
    let mut entries: Vec<Entry> = Vec::new();

    for (index, line) in reader.lines().enumerate() {
        let line = line?;
        let line = line.trim();

        if !line.starts_with('#') && !line.is_empty() {
            if !line.starts_with('-') {
                return Err(Error::from(ParseError {
                    line: index + 1,
                    message: "an entry should starts with “-”.".to_string(),
                }));
            } else {
                let translation = line[1..].trim().split(':').collect::<Vec<&str>>();
                if translation.len() != 2 {
                    return Err(Error::from(ParseError {
                        line: index + 1,
                        message: "an entry should contain two parts separated by “:”.".to_string(),
                    }));
                } else {
                    let t1 = translation[0].trim();
                    let t2 = translation[1].trim();
                    if t1.is_empty() || t2.is_empty() {
                        return Err(Error::from(ParseError {
                            line: index + 1,
                            message: "an entry should contain two parts separated by “:”."
                                .to_string(),
                        }));
                    } else {
                        entries.push(Entry {
                            part_1: serialization::line_to_words(&t1.to_string()),
                            part_2: serialization::line_to_words(&t2.to_string()),
                        })
                    }
                }
            }
        }
    }

    Ok(entries)
}

pub fn pp_from_path(path: &str) -> Option<String> {
    Some(capitalize(
        Path::new(&path).with_extension("").file_name()?.to_str()?,
    ))
}

fn capitalize(s: &str) -> String {
    let mut c = s.chars();
    match c.next() {
        None => String::new(),
        Some(f) => f.to_uppercase().collect::<String>() + c.as_str(),
    }
}