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
|
use crate::{
db, deck,
model::{DbEntry, Line, Question},
};
use anyhow::Result;
use rusqlite::Connection;
use std::collections::HashSet;
pub fn run(conn: &mut Connection, deck_path: &str) -> Result<()> {
let db_entries = db::all(conn)?;
let lines = deck::read(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: HashSet<Question> = HashSet::new();
let mut db_questions_not_deleted: HashSet<Question> = HashSet::new();
let mut db_questions_deleted: HashSet<Question> = HashSet::new();
for Line { part_1, part_2 } in lines {
for question in part_1.clone() {
let mut responses = part_2.clone();
responses.sort();
file_questions.insert(Question {
question,
responses,
});
}
for question in part_2 {
let mut responses = part_1.clone();
responses.sort();
file_questions.insert(Question {
question,
responses,
});
}
}
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,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_diff() {
let db_entries = vec![
DbEntry {
question: "A".to_string(),
responses: vec!["A".to_string()],
deleted: None,
},
DbEntry {
question: "B".to_string(),
responses: vec!["B".to_string()],
deleted: None,
},
DbEntry {
question: "C".to_string(),
responses: vec!["C".to_string()],
deleted: Some(0),
},
DbEntry {
question: "D".to_string(),
responses: vec!["D".to_string()],
deleted: Some(0),
},
];
let lines = vec![
Line {
part_1: vec!["A".to_string()],
part_2: vec!["A".to_string()],
},
Line {
part_1: vec!["C".to_string()],
part_2: vec!["C".to_string()],
},
Line {
part_1: vec!["E".to_string()],
part_2: vec!["E".to_string()],
},
];
let Diff {
new,
deleted,
undeleted,
} = diff(db_entries, lines);
assert_eq!(
new,
vec!(Question {
question: "E".to_string(),
responses: vec!("E".to_string())
})
);
assert_eq!(
deleted,
vec!(Question {
question: "B".to_string(),
responses: vec!("B".to_string())
})
);
assert_eq!(
undeleted,
vec!(Question {
question: "C".to_string(),
responses: vec!("C".to_string())
})
);
}
}
|