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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
use crate::{
db, deck,
model::{DbEntry, Line, Question},
};
use anyhow::Result;
use rusqlite::Connection;
use std::collections::HashMap;
use std::collections::HashSet;
pub fn run(conn: &mut Connection, deck_path: &str) -> Result<()> {
let db_entries = db::all(conn)?;
let lines = deck::read_file(deck_path)?;
let Diff {
new,
deleted,
undeleted,
} = diff(db_entries, lines);
db::insert(conn, &new)?;
db::delete(conn, &deleted)?;
db::undelete(conn, &undeleted)?;
Ok(())
}
struct Diff {
pub new: Vec<Question>,
pub deleted: Vec<Question>,
pub undeleted: Vec<Question>,
}
fn diff(db_entries: Vec<DbEntry>, lines: Vec<Line>) -> Diff {
let mut file_questions = HashMap::<String, Vec<String>>::new();
let mut db_questions_not_deleted = HashSet::<Question>::new();
let mut db_questions_deleted = HashSet::<Question>::new();
for Line { part_1, part_2 } in lines {
insert(&mut file_questions, part_1.clone(), part_2.clone());
insert(&mut file_questions, part_2, part_1);
}
let file_questions: HashSet<Question> = file_questions
.iter()
.map(|(question, responses)| Question {
question: question.to_string(),
responses: responses.to_vec(),
})
.collect();
for DbEntry {
question,
mut responses,
deleted,
} in db_entries
{
responses.sort();
if deleted.is_some() {
db_questions_deleted.insert(Question {
question,
responses,
});
} else {
db_questions_not_deleted.insert(Question {
question,
responses,
});
}
}
let new = file_questions
.difference(&db_questions_not_deleted)
.cloned()
.collect::<HashSet<Question>>()
.difference(&db_questions_deleted)
.cloned()
.collect();
let deleted = db_questions_not_deleted
.difference(&file_questions)
.cloned()
.collect();
let undeleted = file_questions
.intersection(&db_questions_deleted)
.cloned()
.collect();
Diff {
new,
deleted,
undeleted,
}
}
fn insert(map: &mut HashMap<String, Vec<String>>, questions: Vec<String>, responses: Vec<String>) {
for question in questions {
let mut responses = responses.clone();
responses.sort();
match map.get_mut(&question) {
Some(existing_responses) => existing_responses.append(&mut responses),
None => {
map.insert(question, responses);
}
};
}
}
#[cfg(test)]
mod tests {
use super::{deck, DbEntry, Diff, Question};
use std::collections::HashSet;
#[test]
fn test_added() {
let diff = deck_diff("- A : a", "- A : a\n- B : b");
has_questions(diff.new, vec![("B", vec!["b"]), ("b", vec!["B"])]);
assert!(diff.deleted.is_empty());
assert!(diff.undeleted.is_empty());
}
#[test]
fn test_updated() {
let diff = deck_diff("- A : a1", "- A : a2");
has_questions(diff.new, vec![("A", vec!["a2"]), ("a2", vec!["A"])]);
has_questions(diff.deleted, vec![("A", vec!["a1"]), ("a1", vec!["A"])]);
assert!(diff.undeleted.is_empty());
}
#[test]
fn test_deleted() {
let diff = deck_diff("- A : a", "");
assert!(diff.new.is_empty());
has_questions(diff.deleted, vec![("A", vec!["a"]), ("a", vec!["A"])]);
assert!(diff.undeleted.is_empty());
}
#[test]
fn test_undeleted() {
let db_entries = vec![DbEntry {
question: "A".to_string(),
responses: vec!["a".to_string()],
deleted: Some(0),
}];
let diff = super::diff(db_entries, deck::tests::read_string("- A : a").unwrap());
has_questions(diff.new, vec![("a", vec!["A"])]);
assert!(diff.deleted.is_empty());
has_questions(diff.undeleted, vec![("A", vec!["a"])]);
}
#[test]
fn regroup_same_question() {
let diff = deck_diff("", "- A : a\n- A | B : b");
has_questions(
diff.new,
vec![
("A", vec!["a", "b"]),
("B", vec!["b"]),
("a", vec!["A"]),
("b", vec!["A", "B"]),
],
);
assert!(diff.deleted.is_empty());
assert!(diff.undeleted.is_empty());
}
fn deck_diff(from: &str, to: &str) -> Diff {
super::diff(db_entries(from), deck::tests::read_string(to).unwrap())
}
fn has_questions(questions: Vec<Question>, xs: Vec<(&str, Vec<&str>)>) {
assert_eq!(
to_set(questions),
HashSet::from_iter(
xs.iter()
.map(|(y, ys)| Question {
question: y.to_string(),
responses: ys.iter().map(|z| z.to_string()).collect::<Vec<_>>()
})
.collect::<Vec<_>>()
)
)
}
fn to_set<A: std::cmp::Eq + std::hash::Hash + std::clone::Clone>(xs: Vec<A>) -> HashSet<A> {
xs.iter().cloned().collect()
}
fn db_entries(deck: &str) -> Vec<DbEntry> {
let lines = deck::tests::read_string(deck).unwrap();
let diff = super::diff(vec![], lines);
diff.new
.iter()
.map(
|Question {
question,
responses,
}| DbEntry {
question: question.to_string(),
responses: responses.to_vec(),
deleted: None,
},
)
.collect()
}
}
|