diff options
author | Joris | 2022-02-06 19:30:53 +0100 |
---|---|---|
committer | Joris | 2022-02-06 19:30:53 +0100 |
commit | 20cfe717065fa53953e9799036a9972880fec801 (patch) | |
tree | 7fc2a7b4a43eb6d280d52c468927592c12798cb0 /src/deck.rs | |
parent | dc0f32017cceabb6c683b6e1b4a2ae0248c37dbf (diff) |
Replace a card whenever the front or the back changed
Diffstat (limited to 'src/deck.rs')
-rw-r--r-- | src/deck.rs | 23 |
1 files changed, 16 insertions, 7 deletions
diff --git a/src/deck.rs b/src/deck.rs index 0fe8a7b..ce20ee9 100644 --- a/src/deck.rs +++ b/src/deck.rs @@ -1,8 +1,8 @@ -use crate::{model::deck::Entry, util::serialization}; -use anyhow::{Result, Error}; +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::fmt; #[derive(Debug, Clone)] struct ParseError { @@ -22,7 +22,6 @@ impl std::error::Error for ParseError { } } - pub fn read(deck: String) -> Result<Vec<Entry>> { let file = File::open(deck)?; let reader = BufReader::new(file); @@ -34,16 +33,26 @@ pub fn read(deck: String) -> Result<Vec<Entry>> { 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() })) + 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() })) + 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() })) + 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()), |