Use ? instead of .unwrap() in doctests.

This commit is contained in:
KAMADA Ken'ichi 2020-02-01 20:28:21 +09:00
parent 4621acaaab
commit af94ad50b6
4 changed files with 27 additions and 9 deletions

View File

@ -35,16 +35,18 @@
//! An example to parse JPEG/TIFF files: //! An example to parse JPEG/TIFF files:
//! //!
//! ``` //! ```
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! for path in &["tests/exif.jpg", "tests/exif.tif"] { //! for path in &["tests/exif.jpg", "tests/exif.tif"] {
//! let file = std::fs::File::open(path).unwrap(); //! let file = std::fs::File::open(path)?;
//! let mut bufreader = std::io::BufReader::new(&file); //! let mut bufreader = std::io::BufReader::new(&file);
//! let exifreader = exif::Reader::new(); //! let exifreader = exif::Reader::new();
//! let exif = exifreader.read_from_container(&mut bufreader).unwrap(); //! let exif = exifreader.read_from_container(&mut bufreader)?;
//! for f in exif.fields() { //! for f in exif.fields() {
//! println!("{} {} {}", //! println!("{} {} {}",
//! f.tag, f.ifd_num, f.display_value().with_unit(&exif)); //! f.tag, f.ifd_num, f.display_value().with_unit(&exif));
//! } //! }
//! } //! }
//! # Ok(()) }
//! ``` //! ```
//! //!
//! # Upgrade Guide from 0.4.x to 0.5.x //! # Upgrade Guide from 0.4.x to 0.5.x

View File

@ -40,13 +40,22 @@ use crate::tiff::{Field, IfdEntry, In, ProvideUnit};
/// ///
/// # Examples /// # Examples
/// ``` /// ```
/// # use std::fmt::{Display, Formatter, Result};
/// # #[derive(Debug)] struct Error(&'static str);
/// # impl std::error::Error for Error {}
/// # impl Display for Error {
/// # fn fmt(&self, f: &mut Formatter) -> Result { f.write_str(self.0) }
/// # }
/// # fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
/// use exif::{In, Reader, Tag}; /// use exif::{In, Reader, Tag};
/// let file = std::fs::File::open("tests/exif.jpg").unwrap(); /// let file = std::fs::File::open("tests/exif.jpg")?;
/// let exif = Reader::new().read_from_container( /// let exif = Reader::new()
/// &mut std::io::BufReader::new(&file)).unwrap(); /// .read_from_container(&mut std::io::BufReader::new(&file))?;
/// let xres = exif.get_field(Tag::XResolution, In::PRIMARY).unwrap(); /// let xres = exif.get_field(Tag::XResolution, In::PRIMARY)
/// .ok_or(Error("tests/exif.jpg must have XResolution"))?;
/// assert_eq!(xres.display_value().with_unit(&exif).to_string(), /// assert_eq!(xres.display_value().with_unit(&exif).to_string(),
/// "72 pixels per inch"); /// "72 pixels per inch");
/// # Ok(()) }
/// ``` /// ```
pub struct Reader { pub struct Reader {
} }
@ -115,18 +124,21 @@ impl Reader {
/// ///
/// # Examples /// # Examples
/// ``` /// ```
/// # fn main() { sub(); }
/// # fn sub() -> Option<()> {
/// # use exif::{In, Reader, Tag}; /// # use exif::{In, Reader, Tag};
/// # let file = std::fs::File::open("tests/exif.jpg").unwrap(); /// # let file = std::fs::File::open("tests/exif.jpg").unwrap();
/// # let exif = Reader::new().read_from_container( /// # let exif = Reader::new().read_from_container(
/// # &mut std::io::BufReader::new(&file)).unwrap(); /// # &mut std::io::BufReader::new(&file)).unwrap();
/// // Get a specific field. /// // Get a specific field.
/// let xres = exif.get_field(Tag::XResolution, In::PRIMARY).unwrap(); /// let xres = exif.get_field(Tag::XResolution, In::PRIMARY)?;
/// assert_eq!(xres.display_value().with_unit(&exif).to_string(), /// assert_eq!(xres.display_value().with_unit(&exif).to_string(),
/// "72 pixels per inch"); /// "72 pixels per inch");
/// // Iterate over all fields. /// // Iterate over all fields.
/// for f in exif.fields() { /// for f in exif.fields() {
/// println!("{} {} {}", f.tag, f.ifd_num, f.display_value()); /// println!("{} {} {}", f.tag, f.ifd_num, f.display_value());
/// } /// }
/// # Some(()) }
/// ``` /// ```
pub struct Exif { pub struct Exif {
// TIFF data. // TIFF data.

View File

@ -273,10 +273,12 @@ pub fn is_tiff(buf: &[u8]) -> bool {
/// ///
/// # Examples /// # Examples
/// ``` /// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use exif::DateTime; /// use exif::DateTime;
/// let dt = DateTime::from_ascii(b"2016:05:04 03:02:01").unwrap(); /// let dt = DateTime::from_ascii(b"2016:05:04 03:02:01")?;
/// assert_eq!(dt.year, 2016); /// assert_eq!(dt.year, 2016);
/// assert_eq!(dt.to_string(), "2016-05-04 03:02:01"); /// assert_eq!(dt.to_string(), "2016-05-04 03:02:01");
/// # Ok(()) }
/// ``` /// ```
#[derive(Debug)] #[derive(Debug)]
pub struct DateTime { pub struct DateTime {

View File

@ -38,6 +38,7 @@ use crate::value::Value;
/// # Examples /// # Examples
/// ///
/// ``` /// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use exif::{Field, In, Tag, Value}; /// use exif::{Field, In, Tag, Value};
/// use exif::experimental::Writer; /// use exif::experimental::Writer;
/// let image_desc = Field { /// let image_desc = Field {
@ -48,13 +49,14 @@ use crate::value::Value;
/// let mut writer = Writer::new(); /// let mut writer = Writer::new();
/// let mut buf = std::io::Cursor::new(Vec::new()); /// let mut buf = std::io::Cursor::new(Vec::new());
/// writer.push_field(&image_desc); /// writer.push_field(&image_desc);
/// writer.write(&mut buf, false).unwrap(); /// writer.write(&mut buf, false)?;
/// static expected: &[u8] = /// static expected: &[u8] =
/// b"\x4d\x4d\x00\x2a\x00\x00\x00\x08\ /// b"\x4d\x4d\x00\x2a\x00\x00\x00\x08\
/// \x00\x01\x01\x0e\x00\x02\x00\x00\x00\x07\x00\x00\x00\x1a\ /// \x00\x01\x01\x0e\x00\x02\x00\x00\x00\x07\x00\x00\x00\x1a\
/// \x00\x00\x00\x00\ /// \x00\x00\x00\x00\
/// Sample\0"; /// Sample\0";
/// assert_eq!(buf.into_inner(), expected); /// assert_eq!(buf.into_inner(), expected);
/// # Ok(()) }
/// ``` /// ```
#[derive(Debug)] #[derive(Debug)]
pub struct Writer<'a> { pub struct Writer<'a> {