Skip to content

Commit

Permalink
Add docs and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nabijaczleweli committed Sep 13, 2016
1 parent b03e011 commit 6f3e793
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description = "Display images in your terminal, kind of"
documentation = "https://cdn.rawgit.com/nabijaczleweli/termimage/doc/termimage/index.html"
repository = "https://github.com/nabijaczleweli/termimage"
readme = "README.md"
keywords = ["twitter", "tweets", "queue", "schedule", "content"]
keywords = ["image", "terminal", "picture", "display", "show"]
license = "MIT"
# Remember to also update in appveyor.yml
version = "0.1.0"
Expand All @@ -18,7 +18,7 @@ lazy_static = "0.2"
term_size = "0.2"
image = "0.10"
regex = "0.1"
clap = "2.11"
clap = "2.12"


[[bin]]
Expand Down
34 changes: 34 additions & 0 deletions src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,35 @@ use std::io::BufReader;
use std::fs::File;


/// Guess the image format from its extension.
///
/// # Examples
///
/// Correct:
///
/// ```
/// # extern crate image;
/// # extern crate termimage;
/// # use image::ImageFormat;
/// # use std::path::PathBuf;
/// # use termimage::ops::guess_format;
/// # fn main() {
/// assert_eq!(guess_format(&(String::new(), PathBuf::from("img.png"))), Ok(ImageFormat::PNG));
/// assert_eq!(guess_format(&(String::new(), PathBuf::from("img.jpg"))), Ok(ImageFormat::JPEG));
/// assert_eq!(guess_format(&(String::new(), PathBuf::from("img.gif"))), Ok(ImageFormat::GIF));
/// assert_eq!(guess_format(&(String::new(), PathBuf::from("img.bmp"))), Ok(ImageFormat::BMP));
/// assert_eq!(guess_format(&(String::new(), PathBuf::from("img.ico"))), Ok(ImageFormat::ICO));
/// # }
/// ```
///
/// Incorrect:
///
/// ```
/// # use std::path::PathBuf;
/// # use termimage::Outcome;
/// # use termimage::ops::guess_format;
/// assert_eq!(guess_format(&("../ops.rs".to_string(), PathBuf::from("ops.rs"))), Err(Outcome::GuessingFormatFailed("../ops.rs".to_string())));
/// ```
pub fn guess_format(file: &(String, PathBuf)) -> Result<ImageFormat, Outcome> {
file.1
.extension()
Expand All @@ -24,10 +53,15 @@ pub fn guess_format(file: &(String, PathBuf)) -> Result<ImageFormat, Outcome> {
.ok_or_else(|| Outcome::GuessingFormatFailed(file.0.clone()))
}

/// Load an image from the specified file as the specified format.
///
/// Get the image fromat with `guess_format()`.
pub fn load_image(file: &(String, PathBuf), format: ImageFormat) -> DynamicImage {
image::load(BufReader::new(File::open(&file.1).unwrap()), format).unwrap()
}


/// Resize the specified image to the specified size, optionally preserving its aspect ratio.
pub fn resize_image(img: &DynamicImage, size: (u32, u32), preserve_aspect: bool) -> DynamicImage {
if preserve_aspect {
img.resize(size.0, size.1, FilterType::Nearest)
Expand Down
1 change: 1 addition & 0 deletions tests/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod ops;
76 changes: 76 additions & 0 deletions tests/ops/guess_format.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
extern crate image;
extern crate termimage;

use std::path::PathBuf;
use self::image::ImageFormat;
use self::termimage::Outcome;
use self::termimage::ops::guess_format;


#[test]
fn png() {
test_correct("png", ImageFormat::PNG);
}

#[test]
fn jpeg() {
test_correct("jpg", ImageFormat::JPEG);
test_correct("jpeg", ImageFormat::JPEG);
test_correct("jpe", ImageFormat::JPEG);
test_correct("jif", ImageFormat::JPEG);
test_correct("jfif", ImageFormat::JPEG);
test_correct("jfi", ImageFormat::JPEG);
}

#[test]
fn gif() {
test_correct("gif", ImageFormat::GIF);
}

#[test]
fn webp() {
test_correct("webp", ImageFormat::WEBP);
}

#[test]
fn ppm() {
test_correct("ppm", ImageFormat::PPM);
}

#[test]
fn tiff() {
test_correct("tiff", ImageFormat::TIFF);
test_correct("tif", ImageFormat::TIFF);
}

#[test]
fn tga() {
test_correct("tga", ImageFormat::TGA);
}

#[test]
fn bmp() {
test_correct("bmp", ImageFormat::BMP);
test_correct("dib", ImageFormat::BMP);
}

#[test]
fn ico() {
test_correct("ico", ImageFormat::ICO);
}

#[test]
fn hdr() {
test_correct("hdr", ImageFormat::HDR);
}

#[test]
fn unknown() {
let p = "../tests/ops/guess_format.rs".to_string();
assert_eq!(guess_format(&(p.clone(), PathBuf::from(&p))), Err(Outcome::GuessingFormatFailed(p)));
}


fn test_correct(ext: &str, f: ImageFormat) {
assert_eq!(guess_format(&(String::new(), PathBuf::from(format!("img.{}", ext)))), Ok(f));
}
1 change: 1 addition & 0 deletions tests/ops/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod guess_format;

0 comments on commit 6f3e793

Please sign in to comment.