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
use crate::error::Error;
use std::cmp::Ordering;
use std::path::{Path, PathBuf};
pub const MAX_SEQ_NUM: usize = 999_999_999;
pub const NUM_DIGITS: usize = 9;
#[derive(Debug, Eq)]
pub struct LogPath {
path: PathBuf,
seq_num: usize,
}
impl LogPath {
pub fn new(dir: &Path, seq_num: usize) -> LogPath {
assert!(seq_num > 0 && seq_num <= MAX_SEQ_NUM);
let mut path = dir.to_path_buf();
path.push(format!("{:09}.devlog", seq_num));
LogPath { path, seq_num }
}
pub fn from_path(path: PathBuf) -> Option<LogPath> {
let stem = path.file_stem().and_then(|s| s.to_str()).unwrap_or("");
let ext = path.extension().and_then(|s| s.to_str()).unwrap_or("");
let seq_num: Option<usize> = stem.parse().ok();
match (stem, ext, seq_num) {
(s, "devlog", Some(seq_num)) if s.len() == NUM_DIGITS => {
Some(LogPath { path, seq_num })
}
_ => None,
}
}
pub fn next(&self) -> Result<LogPath, Error> {
let seq_num = self.seq_num + 1;
if seq_num > MAX_SEQ_NUM {
Err(Error::LogFileLimitExceeded)
} else {
let mut path = match self.path.parent() {
Some(p) => p.to_path_buf(),
None => PathBuf::new(),
};
path.push(format!("{:09}.devlog", seq_num));
Ok(LogPath { path, seq_num })
}
}
pub fn seq_num(&self) -> usize {
self.seq_num
}
pub fn path(&self) -> &Path {
&self.path
}
}
impl PartialOrd for LogPath {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for LogPath {
fn cmp(&self, other: &Self) -> Ordering {
self.seq_num.cmp(&other.seq_num)
}
}
impl PartialEq for LogPath {
fn eq(&self, other: &Self) -> bool {
self.seq_num == other.seq_num
}
}
#[cfg(test)]
mod tests {
use super::*;
fn dir() -> PathBuf {
From::from(String::from("/foo/bar"))
}
fn rootdir() -> PathBuf {
From::from(String::from("/"))
}
#[test]
fn test_new() {
let d = dir();
let p = LogPath::new(&d, 123);
assert_eq!(p.seq_num(), 123);
assert_eq!(p.path(), d.join("000000123.devlog"));
}
#[test]
fn test_from_path() {
let path = dir().join("000000123.devlog");
let p = LogPath::from_path(path).unwrap();
assert_eq!(p.seq_num(), 123);
assert_eq!(p.path(), dir().join("000000123.devlog"));
}
#[test]
fn test_from_path_max_seq_num() {
let fname = format!("{}.devlog", MAX_SEQ_NUM);
let path = dir().join(&fname);
let p = LogPath::from_path(path).unwrap();
assert_eq!(p.seq_num(), MAX_SEQ_NUM);
assert_eq!(p.path(), dir().join(&fname));
}
#[test]
fn test_from_path_not_a_number() {
let path = dir().join("abc123.devlog");
assert!(LogPath::from_path(path).is_none());
}
#[test]
fn test_from_path_too_few_digits() {
let path = dir().join("12345678.devlog");
assert!(LogPath::from_path(path).is_none());
}
#[test]
fn test_from_path_too_many_digits() {
let path = dir().join("1234567890.devlog");
assert!(LogPath::from_path(path).is_none());
}
#[test]
fn test_from_path_seq_num_too_large() {
let fname = format!("{}.devlog", MAX_SEQ_NUM + 1);
let path = dir().join(&fname);
assert!(LogPath::from_path(path).is_none());
}
#[test]
fn test_from_path_wrong_ext() {
let path = dir().join("000000001.csv");
assert!(LogPath::from_path(path).is_none());
}
#[test]
fn test_next_in_subdir() {
let d = dir();
let p = LogPath::new(&d, 123).next().unwrap();
assert_eq!(p.seq_num(), 124);
assert_eq!(p.path(), dir().join("000000124.devlog"));
}
#[test]
fn test_next_in_rootdir() {
let d = rootdir();
let p = LogPath::new(&d, 123).next().unwrap();
assert_eq!(p.seq_num(), 124);
assert_eq!(p.path(), d.join("000000124.devlog"));
}
#[test]
fn test_next_file_limit_exceeded() {
let d = dir();
let p = LogPath::new(&d, MAX_SEQ_NUM).next();
match p {
Err(Error::LogFileLimitExceeded) => {}
_ => assert!(false),
}
}
#[test]
fn test_ordering() {
let d = dir();
let p1 = LogPath::new(&d, 1);
let p2 = LogPath::new(&d, 2);
let p3 = LogPath::new(&d, 2);
assert!(p1 < p2);
assert!(p2 > p1);
assert!(p2 == p3);
}
}