| 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 | use std::ffi::OsString; |
| 44 | use std::io::Write; |
| 45 | use std::os::unix::ffi::{OsStrExt, OsStringExt}; |
| 46 | use std::path::{Path, PathBuf}; |
| 47 | |
| 48 | use crate::project::Clip; |
| 49 | |
| 50 | |
| 51 | |
| 52 | const MAGIC: &[u8] = b"edith 2"; |
| 53 | const MAGIC_V1: &[u8] = b"edith 1"; |
| 54 | |
| 55 | |
| 56 | |
| 57 | #[derive(Debug)] |
| 58 | pub struct Document { |
| 59 | |
| 60 | pub sources: Vec<PathBuf>, |
| 61 | pub video: Vec<Clip>, |
| 62 | pub audio: Vec<Clip>, |
| 63 | pub playhead: u32, |
| 64 | } |
| 65 | |
| 66 | |
| 67 | |
| 68 | pub 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 | |
| 80 | |
| 81 | |
| 82 | |
| 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 | |
| 96 | pub 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 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | fn 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 | |
| 115 | fn 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 | |
| 126 | |
| 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 | |
| 142 | fn parse(data: &[u8], dir: &Path) -> crate::Result<Document> { |
| 143 | |
| 144 | |
| 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 | |
| 165 | |
| 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 | |
| 192 | |
| 193 | doc.sources.push(if path.is_absolute() { |
| 194 | path |
| 195 | } else { |
| 196 | dir.join(path) |
| 197 | }); |
| 198 | } |
| 199 | |
| 200 | |
| 201 | |
| 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 | |
| 240 | |
| 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 | |
| 251 | |
| 252 | |
| 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 | |
| 269 | fn 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 | |
| 287 | fn 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 | |
| 304 | |
| 305 | |
| 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 | |
| 316 | fn 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 | |
| 328 | fn 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 | |
| 339 | |
| 340 | fn 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)] |
| 363 | mod 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 | |
| 377 | |
| 378 | |
| 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 | |
| 410 | assert_eq!( |
| 411 | emit(&dir, &back.sources, &back.video, &back.audio, back.playhead), |
| 412 | bytes |
| 413 | ); |
| 414 | } |
| 415 | |
| 416 | |
| 417 | |
| 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 | |
| 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 | |
| 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 | |
| 451 | |
| 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 | |
| 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 | |
| 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 | |
| 476 | |
| 477 | |
| 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 | |
| 484 | b"edith 2\nsource a.mp4\nvideo 4294967290 0 30 0 -\nvideo 4294967295 0 30 0 -\n", |
| 485 | |
| 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 | |
| 497 | |
| 498 | |
| 499 | |
| 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 | |
| 524 | |
| 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 | |
| 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 | |
| 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 | |
| 627 | |
| 628 | let _ = parse(&bytes[cut..], &dir); |
| 629 | } |
| 630 | |
| 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 | } |