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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
use std::path::PathBuf;
use glob;
use error::SourceError;
use super::{Meta, Date};
use super::index::{self, Index};
use super::yaml::{self, Yaml};
#[derive(Debug, Clone)]
pub struct Entry<EntryMeta>
where EntryMeta: Meta
{
pub title: String,
pub description: Option<String>,
pub date: Option<Date>,
pub index: Option<Index>,
pub cc: Vec<String>,
pub meta: EntryMeta,
pub name: String,
pub content: String,
pub format: String,
}
impl<EntryMeta> Entry<EntryMeta>
where EntryMeta: Meta
{
pub fn from_file(full_path: PathBuf, name: &str) -> Result<Self, SourceError> {
const U32_MAX_AS_I64: i64 = ::std::u32::MAX as i64;
fn dir_to_glob(mut dir: String) -> Result<glob::Pattern, SourceError> {
if !dir.ends_with('/') {
dir.push('/');
}
dir.push('*');
glob::Pattern::new(&dir).map_err(From::from)
}
fn name_to_glob(name: &str) -> glob::Pattern {
let mut s = glob::Pattern::escape(name);
s.push_str("/*");
glob::Pattern::new(&s).unwrap()
}
let (mut meta, content) = yaml::load_front(&full_path)?;
Ok(Entry {
content: content,
format: full_path.extension().and_then(|e| e.to_str()).unwrap_or("").to_owned(),
title: match meta.remove(&yaml::TITLE) {
Some(Yaml::String(title)) => title,
Some(..) => return Err("titles must be strings".into()),
None => return Err("entries must have titles".into()),
},
description: match meta.remove(&yaml::DESCRIPTION) {
Some(Yaml::String(desc)) => Some(desc),
None => None,
Some(..) => return Err("invalid description type".into()),
},
date: match meta.remove(&yaml::DATE) {
Some(Yaml::String(date)) => {
match Date::parse_from_str(&date, "%Y-%m-%d") {
Ok(date) => Some(date),
Err(_) => return Err("invalid date format".into()),
}
}
Some(..) => return Err("date must be a string".into()),
None => None,
},
index: match meta.remove(&yaml::INDEX) {
Some(Yaml::Boolean(b)) => {
if b {
Some(Index {
paginate: None,
max: None,
compact: false,
sort: index::Sort::default(),
directories: vec![name_to_glob(&name)],
})
} else {
None
}
}
Some(Yaml::String(dir)) => {
Some(Index {
paginate: None,
max: None,
compact: false,
sort: index::Sort::default(),
directories: vec![dir_to_glob(dir)?],
})
}
Some(Yaml::Array(array)) => {
Some(Index {
paginate: None,
max: None,
compact: false,
sort: index::Sort::default(),
directories: array.into_iter()
.map(|i| match i {
Yaml::String(dir) => dir_to_glob(dir),
_ => Err(SourceError::from("index directories must be strings")),
})
.collect::<Result<_, _>>()?,
})
}
Some(Yaml::Hash(mut index)) => {
Some(Index {
paginate: match index.remove(&yaml::PAGINATE) {
Some(Yaml::Integer(i @ 1...U32_MAX_AS_I64)) => Some(i as u32),
Some(Yaml::Boolean(false)) |
None => None,
Some(..) => return Err("invalid pagination setting".into()),
},
max: match index.remove(&yaml::MAX) {
Some(Yaml::Integer(i @ 1...U32_MAX_AS_I64)) => Some(i as u32),
Some(Yaml::Boolean(false)) |
None => None,
Some(..) => return Err("invalid max setting".into()),
},
compact: match index.remove(&yaml::COMPACT) {
Some(Yaml::Boolean(b)) => b,
None => false,
Some(..) => return Err("invalid compact setting".into()),
},
sort: match index.remove(&yaml::SORT) {
Some(Yaml::String(key)) => {
let (dir, key) = if key.starts_with('+') {
(index::SortDirection::Ascending, &key[1..])
} else if key.starts_with('-') {
(index::SortDirection::Descending, &key[1..])
} else {
(index::SortDirection::default(), &key[..])
};
index::Sort {
direction: dir,
field: match key {
"date" => index::SortField::Date,
"title" => index::SortField::Title,
"default" => index::SortField::default(),
_ => return Err("invalid sort value".into()),
},
}
}
Some(..) => return Err("invalid sort value".into()),
None => index::Sort::default(),
},
directories: match index.remove(&yaml::SORT) {
Some(Yaml::Array(array)) => {
array.into_iter()
.map(|i| match i {
Yaml::String(dir) => dir_to_glob(dir),
_ => {
Err(SourceError::from("index directories must be \
strings"))
}
})
.collect::<Result<_, _>>()?
}
Some(Yaml::String(dir)) => vec![dir_to_glob(dir)?],
Some(..) => return Err("invalid directory list in index".into()),
None => vec![name_to_glob(name)],
},
})
}
Some(..) => return Err("invalid index value".into()),
None => None,
},
cc: match meta.remove(&yaml::CC) {
Some(Yaml::String(cc)) => vec![cc],
Some(Yaml::Array(cc)) => {
cc.into_iter()
.map(|v| match v {
Yaml::String(ci) => Ok(ci),
_ => Err(SourceError::from("invlaid cc value")),
})
.collect::<Result<_, _>>()?
}
Some(..) => return Err("invalid cc value".into()),
None => Vec::new(),
},
meta: EntryMeta::from_yaml(meta)?,
name: name.to_owned(),
})
}
}
#[derive(Debug, Clone)]
pub struct StaticEntry {
pub name: String,
pub source: PathBuf,
}