forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBdff7d84Tahinli3 months ago58 commits

edith.rs638 lines, 24.3 KiBHistoryRawDownload
1//! The project file: a line of text per thing, and nothing else.
2//!
3//! ```text
4//! edith 2
5//! playhead 90
6//! source test_av.mp4
7//! source /elsewhere/test_av2.mp4
8//! video 0 0 120 0 0
9//! audio 0 0 120 0 0
10//! video 120 0 120 1 -
11//! ```
12//!
13//! A lane line is `<lane> <start> <in> <out> <source> <link>`: where the clip
14//! sits on the timeline, the half-open source range it plays, the file it plays
15//! from, and its group id (`-` for none). Timeline placement is explicit, so a
16//! *gap* is simply a stretch no line covers -- there is nothing to write for
17//! one, and nothing that can disagree about its length.
18//!
19//! **Version 1** wrote one lane, queued end to end: `clip <in> <out> <source>`.
20//! Such a file still loads -- the clips are laid out cumulatively and copied
21//! onto both lanes as one group each, which is exactly what a v1 timeline meant
22//! -- and saving it again writes v2. A v1 reader refuses a v2 file by name.
23//!
24//! Text because an edit list is a few integers and a path, and a path is
25//! *bytes* on this platform -- a JSON string would have to lossily decode one.
26//! So the path field is byte-escaped (`%` -> `%25`, newline -> `%0A`, which is
27//! everything a line-based format cannot carry) and survives round-trip
28//! exactly. Paths under the project file's own directory are written relative
29//! to it, so a folder holding the media and the `.edith` can be moved or
30//! copied anywhere and still open; anything else is absolute.
31//!
32//! The parser is strict and every refusal names its 1-based line: a project
33//! file is generated, so a line it did not generate is a corrupt file, not a
34//! dialect. Structure is checked here (fields, ordering, empty clips,
35//! out-of-range source indexes); whether the files on disk still match the
36//! timeline is [`crate::PlaybackSession::open_project`]'s business.
37//!
38//! Writing goes through `<path>.part` and a rename, as an export does, so an
39//! interrupted save cannot destroy the previous version of the project. The
40//! bytes are fsynced before the rename and the directory after it, so what a
41//! power loss can lose is a whole save, never half of one under the real name.
42
43use std::ffi::OsString;
44use std::io::Write;
45use std::os::unix::ffi::{OsStrExt, OsStringExt};
46use std::path::{Path, PathBuf};
47
48use crate::project::Clip;
49
50/// What [`save`] writes. Read support goes back to `edith 1`; see the module
51/// docs for what that dialect looked like.
52const MAGIC: &[u8] = b"edith 2";
53const MAGIC_V1: &[u8] = b"edith 1";
54
55/// What a project file says: an edit list plus where the playhead stood.
56/// Structurally valid by construction -- see [`parse`].
57#[derive(Debug)]
58pub struct Document {
59 /// Absolute, relative entries already joined to the file's own directory.
60 pub sources: Vec<PathBuf>,
61 pub video: Vec<Clip>,
62 pub audio: Vec<Clip>,
63 pub playhead: u32,
64}
65
66/// Writes the project to `path`, atomically. `sources` should already be
67/// orphan-free ([`crate::Project::without_orphan_sources`]).
68pub fn save(
69 path: &Path,
70 sources: &[PathBuf],
71 video: &[Clip],
72 audio: &[Clip],
73 playhead: u32,
74) -> crate::Result<()> {
75 let dir = project_dir(path);
76 let mut part = path.to_path_buf().into_os_string();
77 part.push(".part");
78 let part = PathBuf::from(part);
79 // The rename publishes the file under the caller's name in one step, on the
80 // same directory; until it happens the old project file is still the whole
81 // truth. `sync_all` puts the bytes on the disk before that name exists, and
82 // the second one puts the name itself there.
83 let result = std::fs::File::create(&part)
84 .and_then(|mut f| {
85 f.write_all(&emit(&dir, sources, video, audio, playhead))?;
86 f.sync_all()
87 })
88 .and_then(|()| std::fs::rename(&part, path))
89 .and_then(|()| std::fs::File::open(&dir)?.sync_all());
90 if result.is_err() {
91 let _ = std::fs::remove_file(&part);
92 }
93 result.map_err(Into::into)
94}
95
96pub fn load(path: &Path) -> crate::Result<Document> {
97 let data = std::fs::read(path)?;
98 parse(&data, path.parent().unwrap_or(Path::new("")))
99}
100
101/// The directory a project file's relative source lines are measured from, in
102/// the same canonical form [`crate::Project`] keeps its sources in -- without
103/// that the prefix simply never matches and every line comes out absolute,
104/// which is what `cd dir && edith clip.mp4` + save used to write (the parent of
105/// a bare filename is `""`). Canonicalizing fails only if the directory does
106/// not exist, and then the write is about to fail anyway.
107fn project_dir(path: &Path) -> PathBuf {
108 let dir = path
109 .parent()
110 .filter(|p| !p.as_os_str().is_empty())
111 .unwrap_or(Path::new("."));
112 dir.canonicalize().unwrap_or_else(|_| dir.to_path_buf())
113}
114
115fn emit(dir: &Path, sources: &[PathBuf], video: &[Clip], audio: &[Clip], playhead: u32) -> Vec<u8> {
116 let mut out = Vec::new();
117 out.extend_from_slice(MAGIC);
118 out.push(b'\n');
119 out.extend_from_slice(format!("playhead {playhead}\n").as_bytes());
120 for s in sources {
121 out.extend_from_slice(b"source ");
122 escape(s.strip_prefix(dir).unwrap_or(s), &mut out);
123 out.push(b'\n');
124 }
125 // Lane by lane rather than interleaved by time: a lane reads as a list, and
126 // the parser gets its sortedness check for free from the order it is in.
127 for (keyword, clips) in [("video", video), ("audio", audio)] {
128 for c in clips {
129 let link = c.link.map_or("-".to_string(), |l| l.to_string());
130 out.extend_from_slice(
131 format!(
132 "{keyword} {} {} {} {} {link}\n",
133 c.start, c.in_frame, c.out_frame, c.source
134 )
135 .as_bytes(),
136 );
137 }
138 }
139 out
140}
141
142fn parse(data: &[u8], dir: &Path) -> crate::Result<Document> {
143 // One trailing newline is the line terminator of the last line, not an
144 // empty line -- any further one is, and is refused below.
145 let body = data.strip_suffix(b"\n").unwrap_or(data);
146 let mut lines = body.split(|&b| b == b'\n').enumerate();
147 let (_, first) = lines.next().unwrap_or((0, &[]));
148 let v1 = first == MAGIC_V1;
149 if first != MAGIC && !v1 {
150 return Err(match first.strip_prefix(b"edith ") {
151 Some(v) => format!("line 1: unsupported version {}", String::from_utf8_lossy(v)),
152 None => "line 1: not a edith file".to_string(),
153 }
154 .into());
155 }
156
157 let mut doc = Document {
158 sources: Vec::new(),
159 video: Vec::new(),
160 audio: Vec::new(),
161 playhead: 0,
162 };
163 let mut playhead_seen = false;
164 // Where the next v1 clip is placed: that dialect had no `start` field, the
165 // clips simply queued up.
166 let mut queued = 0u32;
167 for (i, line) in lines {
168 let n = i + 1;
169 let (keyword, rest) = match line.iter().position(|&b| b == b' ') {
170 Some(at) => (&line[..at], &line[at + 1..]),
171 None => (line, &line[line.len()..]),
172 };
173 match keyword {
174 b"playhead" => {
175 if playhead_seen || !doc.sources.is_empty() {
176 return Err(
177 format!("line {n}: playhead belongs once, before the sources").into(),
178 );
179 }
180 doc.playhead = number(rest, n)?;
181 playhead_seen = true;
182 }
183 b"source" => {
184 if !doc.video.is_empty() || !doc.audio.is_empty() {
185 return Err(format!("line {n}: source after a clip").into());
186 }
187 if rest.is_empty() {
188 return Err(format!("line {n}: source without a path").into());
189 }
190 let path = unescape(rest, n)?;
191 // Relative means "next to the project file", which is what
192 // makes a whole folder relocatable.
193 doc.sources.push(if path.is_absolute() {
194 path
195 } else {
196 dir.join(path)
197 });
198 }
199 // v1: one lane, no placement, no groups. Every clip becomes one
200 // grouped video+audio pair laid where the queue reached, which is
201 // what the file always meant.
202 b"clip" if v1 => {
203 let f = fields(rest, 3, "clip", n)?;
204 let clip = check(
205 Clip {
206 start: queued,
207 in_frame: number(f[0], n)?,
208 out_frame: number(f[1], n)?,
209 source: number(f[2], n)? as usize,
210 link: Some(doc.video.len() as u32),
211 },
212 &doc,
213 n,
214 )?;
215 queued = clip.end();
216 doc.video.push(clip);
217 doc.audio.push(clip);
218 }
219 b"video" | b"audio" if !v1 => {
220 let f = fields(rest, 5, "clip", n)?;
221 let clip = check(
222 Clip {
223 start: number(f[0], n)?,
224 in_frame: number(f[1], n)?,
225 out_frame: number(f[2], n)?,
226 source: number(f[3], n)? as usize,
227 link: match f[4] {
228 b"-" => None,
229 field => Some(number(field, n)?),
230 },
231 },
232 &doc,
233 n,
234 )?;
235 let lane = match keyword {
236 b"video" => &mut doc.video,
237 _ => &mut doc.audio,
238 };
239 // The sorted, non-overlapping placement invariant, checked at
240 // the only door untrusted clips come in through.
241 if lane.last().is_some_and(|prev| prev.end() > clip.start) {
242 return Err(format!(
243 "line {n}: clip at {} overlaps the one before it, or comes before it",
244 clip.start
245 )
246 .into());
247 }
248 lane.push(clip);
249 }
250 // Empty lines land here too: nothing in this format is optional
251 // whitespace. A lane keyword in a v1 file (or `clip` in a v2 one)
252 // is a file mixing dialects, and is refused the same way.
253 _ => {
254 return Err(format!(
255 "line {n}: unknown keyword {:?}",
256 String::from_utf8_lossy(keyword)
257 )
258 .into());
259 }
260 }
261 }
262 if doc.video.is_empty() && doc.audio.is_empty() {
263 return Err("no clips: an empty timeline is not a project".into());
264 }
265 Ok(doc)
266}
267
268/// Splits a line's fields, refusing the wrong count by name.
269fn fields<'a>(
270 rest: &'a [u8],
271 want: usize,
272 what: &str,
273 line: usize,
274) -> crate::Result<Vec<&'a [u8]>> {
275 let fields: Vec<&[u8]> = rest.split(|&b| b == b' ').collect();
276 if fields.len() != want {
277 return Err(format!(
278 "line {line}: {what} wants {want} fields, found {}",
279 fields.len()
280 )
281 .into());
282 }
283 Ok(fields)
284}
285
286/// The per-clip structure checks both dialects share.
287fn check(clip: Clip, doc: &Document, line: usize) -> crate::Result<Clip> {
288 if clip.out_frame <= clip.in_frame {
289 return Err(format!(
290 "line {line}: clip [{}, {}) is empty",
291 clip.in_frame, clip.out_frame
292 )
293 .into());
294 }
295 if clip.source >= doc.sources.len() {
296 return Err(format!(
297 "line {line}: clip names source {} of {}",
298 clip.source,
299 doc.sources.len()
300 )
301 .into());
302 }
303 // Everything downstream -- `Clip::end`, the overlap check, the v1 queue --
304 // adds `start + len` as plain `u32`, which panics in debug and wraps in
305 // release on a crafted file. Bound it here, at the door.
306 if clip.start.checked_add(clip.len()).is_none() {
307 return Err(format!(
308 "line {line}: clip at {} runs past the last frame there is",
309 clip.start
310 )
311 .into());
312 }
313 Ok(clip)
314}
315
316fn number(field: &[u8], line: usize) -> crate::Result<u32> {
317 match std::str::from_utf8(field).ok().and_then(|s| s.parse().ok()) {
318 Some(n) => Ok(n),
319 None => Err(format!(
320 "line {line}: {:?} is not a number",
321 String::from_utf8_lossy(field)
322 )
323 .into()),
324 }
325}
326
327/// The two bytes a line-based format cannot hold, and the escape byte itself.
328fn escape(path: &Path, out: &mut Vec<u8>) {
329 for &b in path.as_os_str().as_bytes() {
330 match b {
331 b'%' => out.extend_from_slice(b"%25"),
332 b'\n' => out.extend_from_slice(b"%0A"),
333 _ => out.push(b),
334 }
335 }
336}
337
338/// Accepts any `%XX`, not just the two [`escape`] writes -- a hand-edited file
339/// may spell a space or a quote that way, and there is no reason to refuse it.
340fn unescape(field: &[u8], line: usize) -> crate::Result<PathBuf> {
341 let mut out = Vec::with_capacity(field.len());
342 let mut i = 0;
343 while i < field.len() {
344 if field[i] != b'%' {
345 out.push(field[i]);
346 i += 1;
347 continue;
348 }
349 let byte = field
350 .get(i + 1..i + 3)
351 .and_then(|h| std::str::from_utf8(h).ok())
352 .and_then(|h| u8::from_str_radix(h, 16).ok())
353 .ok_or_else(|| -> crate::Error {
354 format!("line {line}: truncated % escape in the path").into()
355 })?;
356 out.push(byte);
357 i += 3;
358 }
359 Ok(PathBuf::from(OsString::from_vec(out)))
360}
361
362#[cfg(test)]
363mod tests {
364 use super::*;
365
366 fn clip(start: u32, in_frame: u32, out_frame: u32, source: usize, link: Option<u32>) -> Clip {
367 Clip {
368 start,
369 in_frame,
370 out_frame,
371 source,
372 link,
373 }
374 }
375
376 /// Two sources, one under the project's own directory and one not, a clip
377 /// from each on the video lane, and an audio lane with a gap in the middle
378 /// -- the shape every case below starts from.
379 fn doc() -> (PathBuf, Vec<PathBuf>, Vec<Clip>, Vec<Clip>) {
380 (
381 PathBuf::from("/proj"),
382 vec![
383 PathBuf::from("/proj/a.mp4"),
384 PathBuf::from("/elsewhere/b.mp4"),
385 ],
386 vec![clip(0, 0, 30, 0, Some(0)), clip(30, 10, 20, 1, Some(1))],
387 vec![clip(0, 0, 30, 0, Some(0))],
388 )
389 }
390
391 #[test]
392 fn relative_and_absolute_paths_round_trip() {
393 let (dir, sources, video, audio) = doc();
394 let bytes = emit(&dir, &sources, &video, &audio, 12);
395 assert_eq!(
396 String::from_utf8_lossy(&bytes),
397 "edith 2\nplayhead 12\nsource a.mp4\nsource /elsewhere/b.mp4\n\
398 video 0 0 30 0 0\nvideo 30 10 20 1 1\naudio 0 0 30 0 0\n",
399 "the file under the project directory is written relative to it"
400 );
401 let back = parse(&bytes, &dir).expect("parse");
402 assert_eq!(back.sources, sources, "relative entries rejoin the dir");
403 assert_eq!(back.video, video);
404 assert_eq!(
405 back.audio, audio,
406 "the trailing gap needs no line of its own"
407 );
408 assert_eq!(back.playhead, 12);
409 // ...and emitting the parsed document reproduces the same bytes.
410 assert_eq!(
411 emit(&dir, &back.sources, &back.video, &back.audio, back.playhead),
412 bytes
413 );
414 }
415
416 /// The compatibility promise: a v1 file is a fully-grouped, gapless pair of
417 /// lanes, and saving it again writes v2.
418 #[test]
419 fn a_v1_file_loads_as_two_grouped_lanes() {
420 let dir = PathBuf::from("/proj");
421 let v1 = b"edith 1\nplayhead 5\nsource a.mp4\nclip 0 30 0\nclip 40 60 0\n";
422 let back = parse(v1, &dir).expect("v1 parses");
423 assert_eq!(
424 back.video,
425 vec![clip(0, 0, 30, 0, Some(0)), clip(30, 40, 60, 0, Some(1))],
426 "v1 clips queue up: the second starts where the first ended"
427 );
428 assert_eq!(back.audio, back.video, "one take per clip, on both lanes");
429 assert_eq!(back.playhead, 5);
430 // Saved again it is v2, and that round-trips to the same document.
431 let v2 = emit(&dir, &back.sources, &back.video, &back.audio, back.playhead);
432 assert!(v2.starts_with(b"edith 2\n"));
433 let again = parse(&v2, &dir).expect("v2 parses");
434 assert_eq!((again.video, again.audio), (back.video, back.audio));
435 // A dialect may not be mixed: lane lines under v1, `clip` under v2.
436 for (bytes, want) in [
437 (
438 &b"edith 1\nsource a.mp4\nvideo 0 0 30 0 -\n"[..],
439 "line 3: unknown keyword \"video\"",
440 ),
441 (
442 b"edith 2\nsource a.mp4\nclip 0 30 0\n",
443 "line 3: unknown keyword \"clip\"",
444 ),
445 ] {
446 assert_eq!(parse(bytes, &dir).unwrap_err().to_string(), want);
447 }
448 }
449
450 /// A gap is the absence of a line, so a lane's placements must arrive in
451 /// order and never overlap -- the invariant the whole model rests on.
452 #[test]
453 fn a_lane_out_of_order_or_overlapping_is_refused() {
454 let dir = PathBuf::from("/proj");
455 for bytes in [
456 &b"edith 2\nsource a.mp4\nvideo 30 0 30 0 -\nvideo 0 0 30 0 -\n"[..],
457 b"edith 2\nsource a.mp4\nvideo 0 0 30 0 -\nvideo 10 0 30 0 -\n",
458 ] {
459 let err = parse(bytes, &dir).unwrap_err().to_string();
460 assert!(err.starts_with("line 4: clip at "), "{err}");
461 }
462 // The lanes are independent, so a video line never crowds an audio one.
463 assert!(
464 parse(
465 b"edith 2\nsource a.mp4\nvideo 0 0 30 0 -\naudio 0 0 30 0 -\n",
466 &dir
467 )
468 .is_ok()
469 );
470 // ...and a lane may be empty as long as the other one is not.
471 let only_audio = parse(b"edith 2\nsource a.mp4\naudio 0 0 30 0 -\n", &dir).expect("parse");
472 assert!(only_audio.video.is_empty());
473 }
474
475 /// A `start` near the top of `u32` used to make `Clip::end` panic in debug
476 /// and wrap in release, and a wrapped end passes the overlap check --
477 /// crafted numbers must be refused by name, in both dialects.
478 #[test]
479 fn a_clip_whose_end_overflows_is_refused() {
480 let dir = PathBuf::from("/proj");
481 for bytes in [
482 &b"edith 2\nsource a.mp4\nvideo 4294967290 0 30 0 -\naudio 0 0 30 0 -\n"[..],
483 // Two of them: the second would land *before* the first once wrapped.
484 b"edith 2\nsource a.mp4\nvideo 4294967290 0 30 0 -\nvideo 4294967295 0 30 0 -\n",
485 // v1 has no `start` field, but its queue reaches the same ceiling.
486 b"edith 1\nsource a.mp4\nclip 0 4294967295 0\nclip 0 4294967295 0\n",
487 ] {
488 let err = parse(bytes, &dir).unwrap_err().to_string();
489 assert!(
490 err.contains("runs past the last frame there is"),
491 "not refused by name: {err}"
492 );
493 }
494 }
495
496 /// The relocatability promise, from the two directions a project path
497 /// reaches [`save`] non-canonical: a bare filename (`cd dir && edith
498 /// clip.mp4`, whose parent is `""`) and a directory reached through a
499 /// symlink. Both used to emit absolute source lines.
500 #[test]
501 fn a_non_canonical_project_path_still_writes_relative_sources() {
502 assert_eq!(
503 project_dir(Path::new("a.edith")),
504 std::env::current_dir().expect("cwd"),
505 "a bare filename is saved into the working directory"
506 );
507
508 let dir = std::env::temp_dir().join(format!("ve_edith_link_{}", std::process::id()));
509 let _ = std::fs::remove_dir_all(&dir);
510 std::fs::create_dir_all(dir.join("real")).expect("scratch dir");
511 let dir = std::fs::canonicalize(&dir).expect("canonical scratch dir");
512 std::os::unix::fs::symlink(dir.join("real"), dir.join("link")).expect("symlink");
513 let source = dir.join("real/a.mp4");
514 let path = dir.join("link/p.edith");
515
516 let one = [clip(0, 0, 30, 0, None)];
517 save(&path, &[source], &one, &one, 0).expect("save");
518 let bytes = std::fs::read(&path).expect("read back");
519 assert_eq!(
520 String::from_utf8_lossy(&bytes),
521 "edith 2\nplayhead 0\nsource a.mp4\nvideo 0 0 30 0 -\naudio 0 0 30 0 -\n"
522 );
523 // Loading rejoins the *given* directory, so the file is reached by the
524 // way the project was opened -- the same file, through the link.
525 assert_eq!(
526 load(&path).expect("load").sources,
527 vec![dir.join("link/a.mp4")]
528 );
529 std::fs::remove_dir_all(&dir).expect("cleanup");
530 }
531
532 #[test]
533 fn escapes_survive_the_bytes_json_would_not() {
534 let dir = PathBuf::from("/proj");
535 // A newline and a percent in a filename, plus a non-UTF-8 byte.
536 let nasty = PathBuf::from(OsString::from_vec(b"/proj/we\nird 100%\xff.mp4".to_vec()));
537 let bytes = emit(&dir, &[nasty.clone()], &[clip(0, 0, 5, 0, None)], &[], 0);
538 assert_eq!(
539 bytes.iter().filter(|&&b| b == b'\n').count(),
540 4,
541 "the escaped newline must not become a line break"
542 );
543 let back = parse(&bytes, &dir).expect("parse");
544 assert_eq!(back.sources, vec![nasty]);
545 }
546
547 #[test]
548 fn a_wrong_first_line_is_refused_by_name() {
549 let dir = PathBuf::from("/proj");
550 let err = parse(b"edith 3\nsource a.mp4\nvideo 0 0 5 0 -\n", &dir)
551 .unwrap_err()
552 .to_string();
553 assert_eq!(err, "line 1: unsupported version 3");
554 for junk in [&b""[..], b"{}\n", b"source a.mp4\n"] {
555 assert_eq!(
556 parse(junk, &dir).unwrap_err().to_string(),
557 "line 1: not a edith file"
558 );
559 }
560 }
561
562 #[test]
563 fn malformed_lines_name_their_line_number() {
564 let dir = PathBuf::from("/proj");
565 let cases: [(&[u8], &str); 8] = [
566 (
567 b"edith 1\nsource a.mp4\nclip 0 5\n",
568 "line 3: clip wants 3 fields, found 2",
569 ),
570 (
571 b"edith 1\nsource a.mp4\nclip 0 five 0\n",
572 "line 3: \"five\" is not a number",
573 ),
574 (
575 b"edith 1\nsource a.mp4\nclip 5 5 0\n",
576 "line 3: clip [5, 5) is empty",
577 ),
578 (
579 b"edith 1\nsource a.mp4\nclip 7 5 0\n",
580 "line 3: clip [7, 5) is empty",
581 ),
582 (
583 b"edith 1\nsource a.mp4\nclip 0 5 1\n",
584 "line 3: clip names source 1 of 1",
585 ),
586 (
587 b"edith 1\nsource a.mp4\nclip 0 5 0\nsource b.mp4\n",
588 "line 4: source after a clip",
589 ),
590 (
591 b"edith 1\nsource a.mp4\n\nclip 0 5 0\n",
592 "line 3: unknown keyword \"\"",
593 ),
594 (
595 b"edith 1\nsource a.mp4\nplayhead 3\nclip 0 5 0\n",
596 "line 3: playhead belongs once, before the sources",
597 ),
598 ];
599 for (bytes, want) in cases {
600 assert_eq!(parse(bytes, &dir).unwrap_err().to_string(), want);
601 }
602 }
603
604 #[test]
605 fn a_project_without_clips_is_not_a_project() {
606 let dir = PathBuf::from("/proj");
607 assert_eq!(
608 parse(b"edith 1\nsource a.mp4\n", &dir)
609 .unwrap_err()
610 .to_string(),
611 "no clips: an empty timeline is not a project"
612 );
613 assert_eq!(
614 parse(b"edith 1\n", &dir).unwrap_err().to_string(),
615 "no clips: an empty timeline is not a project"
616 );
617 }
618
619 /// Every truncation of a good file either parses or refuses; none panics.
620 #[test]
621 fn truncations_never_panic() {
622 let (dir, sources, video, audio) = doc();
623 let bytes = emit(&dir, &sources, &video, &audio, 12);
624 for cut in 0..bytes.len() {
625 let _ = parse(&bytes[..cut], &dir);
626 // ...and the same file with a byte lopped off the front, which
627 // shifts every field into the wrong place.
628 let _ = parse(&bytes[cut..], &dir);
629 }
630 // A % escape at the very end of a line has nothing to consume.
631 assert_eq!(
632 parse(b"edith 1\nsource a%\nclip 0 5 0\n", &dir)
633 .unwrap_err()
634 .to_string(),
635 "line 2: truncated % escape in the path"
636 );
637 }
638}