1use std::path::PathBuf;
2
3use serde::{Deserialize, Serialize};
4
5use crate::error::{Error, Result};
6use crate::kind::Kind;
7
8#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
10pub struct MailMessageId(String);
11
12impl MailMessageId {
13 pub fn parse(raw: &str) -> Result<Self> {
14 let trimmed = raw.trim();
15 let inner = trimmed
16 .strip_prefix('<')
17 .and_then(|s| s.strip_suffix('>'))
18 .unwrap_or(trimmed);
19 let (local, domain) = inner
20 .split_once('@')
21 .ok_or_else(|| Error::InvalidMessageId(raw.into()))?;
22 if local.is_empty()
23 || domain.is_empty()
24 || local.contains(char::is_whitespace)
25 || domain.contains(char::is_whitespace)
26 {
27 return Err(Error::InvalidMessageId(raw.into()));
28 }
29 if !domain.contains('.') && domain != "localhost" {
30 return Err(Error::InvalidMessageId(raw.into()));
31 }
32 Ok(Self(format!("<{local}@{domain}>")))
33 }
34
35 pub fn as_str(&self) -> &str {
36 &self.0
37 }
38}
39
40#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
41pub struct FormField {
42 pub name: String,
43 pub value: String,
44}
45
46#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
47pub struct Measure {
48 pub name: String,
49 pub value: String,
50}
51
52#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
53pub struct Step {
54 pub text: String,
55 pub done: bool,
56}
57
58#[derive(Clone, Debug, Serialize, Deserialize)]
60#[serde(rename_all = "camelCase")]
61pub enum Body {
62 File {
63 path: PathBuf,
64 media_type: Option<String>,
65 },
66 Set {
67 title: String,
68 members: Vec<PathBuf>,
69 },
70 Quote {
71 edition: String,
72 start: u64,
73 end: u64,
74 excerpt: String,
75 urls: Vec<String>,
76 },
77 Patch {
78 tree: String,
79 diffs: Vec<PathBuf>,
80 functionaries: Vec<String>,
81 },
82 MailDraft {
83 message_id: MailMessageId,
84 in_reply_to: Option<MailMessageId>,
85 subject: String,
86 path: PathBuf,
87 },
88 Clip {
89 sources: Vec<PathBuf>,
90 in_point: f64,
91 out_point: f64,
92 duration: f64,
93 path: PathBuf,
94 },
95 Page {
96 url: String,
97 snapshot: PathBuf,
98 },
99 Form {
100 blank: String,
101 fields: Vec<FormField>,
102 path: PathBuf,
103 },
104 Table {
105 measures: Vec<Measure>,
106 },
107 Procedure {
108 steps: Vec<Step>,
109 },
110 Event {
111 when: String,
112 where_: String,
113 who: String,
114 },
115}
116
117impl PartialEq for Body {
118 fn eq(&self, other: &Self) -> bool {
119 match (self, other) {
120 (
121 Self::Clip {
122 sources: a_s,
123 in_point: a_i,
124 out_point: a_o,
125 duration: a_d,
126 path: a_p,
127 },
128 Self::Clip {
129 sources: b_s,
130 in_point: b_i,
131 out_point: b_o,
132 duration: b_d,
133 path: b_p,
134 },
135 ) => {
136 a_s == b_s
137 && a_p == b_p
138 && a_i.to_bits() == b_i.to_bits()
139 && a_o.to_bits() == b_o.to_bits()
140 && a_d.to_bits() == b_d.to_bits()
141 }
142 _ => format!("{self:?}") == format!("{other:?}"),
143 }
144 }
145}
146
147impl Eq for Body {}
148
149impl Body {
150 pub fn kind(&self) -> Kind {
151 match self {
152 Self::File { .. } => Kind::File,
153 Self::Set { .. } => Kind::Set,
154 Self::Quote { .. } => Kind::Quote,
155 Self::Patch { .. } => Kind::Patch,
156 Self::MailDraft { .. } => Kind::MailDraft,
157 Self::Clip { .. } => Kind::Clip,
158 Self::Page { .. } => Kind::Page,
159 Self::Form { .. } => Kind::Form,
160 Self::Table { .. } => Kind::Table,
161 Self::Procedure { .. } => Kind::Procedure,
162 Self::Event { .. } => Kind::Event,
163 }
164 }
165
166 pub fn media_type(&self) -> Option<&str> {
167 match self {
168 Self::File { media_type, .. } => media_type.as_deref(),
169 _ => None,
170 }
171 }
172
173 pub fn paths(&self) -> Vec<PathBuf> {
174 match self {
175 Self::File { path, .. } => vec![path.clone()],
176 Self::Set { members, .. } => members.clone(),
177 Self::Quote { .. } => Vec::new(),
178 Self::Patch { diffs, .. } => diffs.clone(),
179 Self::MailDraft { path, .. } => vec![path.clone()],
180 Self::Clip { path, sources, .. } => {
181 let mut p = sources.clone();
182 p.push(path.clone());
183 p
184 }
185 Self::Page { snapshot, .. } => vec![snapshot.clone()],
186 Self::Form { path, .. } => vec![path.clone()],
187 Self::Table { .. } | Self::Procedure { .. } | Self::Event { .. } => Vec::new(),
188 }
189 }
190
191 pub fn rewrite_path(&mut self, from: &std::path::Path, to: PathBuf) {
192 match self {
193 Self::File { path, .. } | Self::MailDraft { path, .. } | Self::Form { path, .. } => {
194 if path == from {
195 *path = to;
196 }
197 }
198 Self::Set { members, .. } => {
199 for p in members.iter_mut() {
200 if p == from {
201 *p = to.clone();
202 }
203 }
204 }
205 Self::Patch { diffs, .. } => {
206 for p in diffs.iter_mut() {
207 if p == from {
208 *p = to.clone();
209 }
210 }
211 }
212 Self::Clip { path, sources, .. } => {
213 if path == from {
214 *path = to.clone();
215 }
216 for p in sources.iter_mut() {
217 if p == from {
218 *p = to.clone();
219 }
220 }
221 }
222 Self::Page { snapshot, .. } => {
223 if snapshot == from {
224 *snapshot = to;
225 }
226 }
227 Self::Quote { .. }
228 | Self::Table { .. }
229 | Self::Procedure { .. }
230 | Self::Event { .. } => {}
231 }
232 }
233
234 pub fn validate(&self) -> Result<()> {
235 match self {
236 Self::File { path, .. } => {
237 if path.as_os_str().is_empty() {
238 return Err(Error::InvalidBody("file path is empty".into()));
239 }
240 }
241 Self::Set { title, members } => {
242 if title.trim().is_empty() {
243 return Err(Error::InvalidBody("set title is empty".into()));
244 }
245 if members.is_empty() {
246 return Err(Error::InvalidBody("set has no members".into()));
247 }
248 if members.iter().any(|p| p.as_os_str().is_empty()) {
249 return Err(Error::InvalidBody("set member path is empty".into()));
250 }
251 }
252 Self::Quote {
253 edition,
254 excerpt,
255 urls,
256 start,
257 end,
258 } => {
259 if edition.trim().is_empty() {
260 return Err(Error::InvalidBody("quote edition is empty".into()));
261 }
262 if excerpt.trim().is_empty() {
263 return Err(Error::InvalidBody("quote excerpt is empty".into()));
264 }
265 if urls.is_empty() {
266 return Err(Error::InvalidBody("quote has no source urls".into()));
267 }
268 if start > end {
269 return Err(Error::InvalidBody("quote range start after end".into()));
270 }
271 for url in urls {
272 crate::source::Source::url(url)?;
273 }
274 }
275 Self::Patch {
276 tree,
277 diffs,
278 functionaries,
279 } => {
280 if tree.trim().is_empty() {
281 return Err(Error::InvalidBody("patch tree is empty".into()));
282 }
283 if diffs.is_empty() {
284 return Err(Error::InvalidBody("patch has no diffs".into()));
285 }
286 if functionaries.is_empty() {
287 return Err(Error::InvalidBody("patch has no functionaries".into()));
288 }
289 }
290 Self::MailDraft { subject, path, .. } => {
291 if subject.trim().is_empty() {
292 return Err(Error::InvalidBody("mail draft subject is empty".into()));
293 }
294 if path.as_os_str().is_empty() {
295 return Err(Error::InvalidBody("mail draft path is empty".into()));
296 }
297 }
298 Self::Clip {
299 sources,
300 path,
301 in_point,
302 out_point,
303 duration,
304 } => {
305 if sources.is_empty() {
306 return Err(Error::InvalidBody("clip has no sources".into()));
307 }
308 if path.as_os_str().is_empty() {
309 return Err(Error::InvalidBody("clip path is empty".into()));
310 }
311 if *out_point < *in_point {
312 return Err(Error::InvalidBody("clip out before in".into()));
313 }
314 if *duration < 0.0 {
315 return Err(Error::InvalidBody("clip duration is negative".into()));
316 }
317 }
318 Self::Page { url, snapshot } => {
319 crate::source::Source::url(url)?;
320 if snapshot.as_os_str().is_empty() {
321 return Err(Error::InvalidBody("page snapshot is empty".into()));
322 }
323 }
324 Self::Form {
325 blank,
326 fields,
327 path,
328 } => {
329 if blank.trim().is_empty() {
330 return Err(Error::InvalidBody("form blank is empty".into()));
331 }
332 if fields.is_empty() {
333 return Err(Error::InvalidBody("form has no fields".into()));
334 }
335 if path.as_os_str().is_empty() {
336 return Err(Error::InvalidBody("form path is empty".into()));
337 }
338 }
339 Self::Table { measures } => {
340 if measures.is_empty() {
341 return Err(Error::InvalidBody("table has no measures".into()));
342 }
343 }
344 Self::Procedure { steps } => {
345 if steps.is_empty() {
346 return Err(Error::InvalidBody("procedure has no steps".into()));
347 }
348 }
349 Self::Event { when, where_, who } => {
350 if when.trim().is_empty() {
351 return Err(Error::InvalidBody("event when is empty".into()));
352 }
353 if where_.trim().is_empty() {
354 return Err(Error::InvalidBody("event where is empty".into()));
355 }
356 if who.trim().is_empty() {
357 return Err(Error::InvalidBody("event who is empty".into()));
358 }
359 }
360 }
361 Ok(())
362 }
363}