Skip to main content

src/is_animated_gif.rs

1use std::fs::File;
2use std::io::{BufReader, Result};
3use std::path::PathBuf;
5use image::codecs::gif::GifDecoder;
6use image::AnimationDecoder;
8/// Returns True if a file is an animated GIF, and False otherwise.
9pub fn is_animated_gif(path: &PathBuf) -> Result<bool> {
10 let file = File::open(path)?;
12 let reader = BufReader::new(file);
13 let decoder = GifDecoder::new(reader);
15 match decoder {
16 Ok(dc) => Ok(dc.into_frames().count() > 1),
17 _ => Ok(false),
18 }
21#[cfg(test)]
22mod test_is_animated_gif {
23 use std::path::PathBuf;
25 use super::*;
27 #[test]
28 fn a_png_is_not_an_animated_gif() {
29 let p = PathBuf::from("src/tests/blue.png");
30 assert_eq!(is_animated_gif(&p).unwrap(), false);
31 }
33 #[test]
34 fn a_static_gif_is_not_an_animated_gif() {
35 let p = PathBuf::from("src/tests/static.gif");
36 assert_eq!(is_animated_gif(&p).unwrap(), false);
37 }
39 #[test]
40 fn an_animated_gif_is_animated() {
41 let p = PathBuf::from("src/tests/animated_squares.gif");
42 assert_eq!(is_animated_gif(&p).unwrap(), true);
43 }
45 #[test]
46 fn a_non_image_is_not_animated_gif() {
47 let p = PathBuf::from("Cargo.toml");
48 assert_eq!(is_animated_gif(&p).unwrap(), false);
49 }
51 #[test]
52 fn a_file_which_doesnt_exist_is_an_error() {
53 let p = PathBuf::from("does_not_exist.txt");
54 assert!(is_animated_gif(&p).is_err());
55 }