forked from ankitects/anki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.rs
93 lines (82 loc) · 1.88 KB
/
mod.rs
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
// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
pub(crate) mod card;
mod collection_timestamps;
mod config;
mod dbcheck;
mod deck;
mod deckconfig;
mod graves;
mod note;
mod notetype;
mod revlog;
mod sqlite;
mod sync;
mod sync_check;
mod tag;
mod upgrades;
use std::fmt::Write;
pub(crate) use sqlite::SqliteStorage;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SchemaVersion {
V11,
V18,
}
impl SchemaVersion {
pub(super) fn has_journal_mode_delete(self) -> bool {
self == Self::V11
}
}
/// Write a list of IDs as '(x,y,...)' into the provided string.
pub(crate) fn ids_to_string<D, I>(buf: &mut String, ids: I)
where
D: std::fmt::Display,
I: IntoIterator<Item = D>,
{
buf.push('(');
write_comma_separated_ids(buf, ids);
buf.push(')');
}
/// Write a list of Ids as 'x,y,...' into the provided string.
pub(crate) fn write_comma_separated_ids<D, I>(buf: &mut String, ids: I)
where
D: std::fmt::Display,
I: IntoIterator<Item = D>,
{
let mut trailing_sep = false;
for id in ids {
write!(buf, "{},", id).unwrap();
trailing_sep = true;
}
if trailing_sep {
buf.pop();
}
}
pub(crate) fn comma_separated_ids<T>(ids: &[T]) -> String
where
T: std::fmt::Display,
{
let mut buf = String::new();
write_comma_separated_ids(&mut buf, ids);
buf
}
#[cfg(test)]
mod test {
use super::ids_to_string;
#[test]
fn ids_string() {
let mut s = String::new();
ids_to_string(&mut s, [0; 0]);
assert_eq!(s, "()");
s.clear();
ids_to_string(&mut s, [7]);
assert_eq!(s, "(7)");
s.clear();
ids_to_string(&mut s, [7, 6]);
assert_eq!(s, "(7,6)");
s.clear();
ids_to_string(&mut s, [7, 6, 5]);
assert_eq!(s, "(7,6,5)");
s.clear();
}
}