- replace anyhow with thiserror
This commit is contained in:
43
src/lib.rs
43
src/lib.rs
@@ -1,7 +1,7 @@
|
||||
#[cfg(feature = "python")]
|
||||
mod py;
|
||||
mod error;
|
||||
|
||||
use anyhow::{Result, anyhow};
|
||||
use chrono::Utc;
|
||||
use colorcet::ColorMap;
|
||||
use colorgrad::{Gradient, LinearGradient};
|
||||
@@ -23,6 +23,7 @@ use std::{
|
||||
};
|
||||
use zstd::zstd_safe::CompressionLevel;
|
||||
use zstd::{DEFAULT_COMPRESSION_LEVEL, stream::Encoder};
|
||||
use crate::error::Error;
|
||||
|
||||
const TAG_SIZE: usize = 20;
|
||||
const OFFSET_SIZE: usize = 8;
|
||||
@@ -59,7 +60,7 @@ impl IFD {
|
||||
}
|
||||
}
|
||||
|
||||
fn write(&mut self, ijtifffile: &mut IJTiffFile, where_to_write_offset: u64) -> Result<u64> {
|
||||
fn write(&mut self, ijtifffile: &mut IJTiffFile, where_to_write_offset: u64) -> Result<u64, Error> {
|
||||
let mut tags = self.tags.drain().collect::<Vec<_>>();
|
||||
tags.sort();
|
||||
ijtifffile.file.seek(SeekFrom::End(0))?;
|
||||
@@ -322,7 +323,7 @@ impl Tag {
|
||||
c as u64
|
||||
}
|
||||
|
||||
fn write_tag(&mut self, ijtifffile: &mut IJTiffFile) -> Result<()> {
|
||||
fn write_tag(&mut self, ijtifffile: &mut IJTiffFile) -> Result<(), Error> {
|
||||
self.offset = ijtifffile.file.stream_position()?;
|
||||
ijtifffile.file.write_all(&self.code.to_le_bytes())?;
|
||||
ijtifffile.file.write_all(&self.ttype.to_le_bytes())?;
|
||||
@@ -338,7 +339,7 @@ impl Tag {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn write_data(&self, ijtifffile: &mut IJTiffFile) -> Result<()> {
|
||||
fn write_data(&self, ijtifffile: &mut IJTiffFile) -> Result<(), Error> {
|
||||
if self.bytes.len() > OFFSET_SIZE {
|
||||
ijtifffile.file.seek(SeekFrom::End(0))?;
|
||||
let offset = ijtifffile.write(&self.bytes)?;
|
||||
@@ -493,7 +494,7 @@ impl CompressedFrame {
|
||||
slice: (usize, usize, usize, usize),
|
||||
tile_width: usize,
|
||||
tile_length: usize,
|
||||
) -> Result<W>
|
||||
) -> Result<W, Error>
|
||||
where
|
||||
W: Write,
|
||||
T: Bytes,
|
||||
@@ -524,7 +525,7 @@ impl CompressedFrame {
|
||||
slice: (usize, usize, usize, usize),
|
||||
tile_width: usize,
|
||||
tile_length: usize,
|
||||
) -> Result<Vec<u8>>
|
||||
) -> Result<Vec<u8>, Error>
|
||||
where
|
||||
T: Bytes,
|
||||
{
|
||||
@@ -539,7 +540,7 @@ impl CompressedFrame {
|
||||
tile_width: usize,
|
||||
tile_length: usize,
|
||||
compression_level: i32,
|
||||
) -> Result<Vec<u8>>
|
||||
) -> Result<Vec<u8>, Error>
|
||||
where
|
||||
T: Bytes,
|
||||
{
|
||||
@@ -678,7 +679,7 @@ impl Drop for IJTiffFile {
|
||||
impl IJTiffFile {
|
||||
/// create new tifffile from path, use it's save() method to save frames
|
||||
/// the file is finalized when it goes out of scope
|
||||
pub fn new<P: AsRef<Path>>(path: P) -> Result<Self> {
|
||||
pub fn new<P: AsRef<Path>>(path: P) -> Result<Self, Error> {
|
||||
let mut file = OpenOptions::new()
|
||||
.create(true)
|
||||
.truncate(true)
|
||||
@@ -711,7 +712,8 @@ impl IJTiffFile {
|
||||
}
|
||||
|
||||
/// set colors from css color names and #C01085
|
||||
pub fn set_colors<S: AsRef<str>>(&mut self, colors: &[S]) -> Result<()> {
|
||||
/// (see [css_color](https://crates.io/crates/css-color))
|
||||
pub fn set_colors<S: AsRef<str>>(&mut self, colors: &[S]) -> Result<(), Error> {
|
||||
self.colors = Colors::Colors(
|
||||
colors
|
||||
.iter()
|
||||
@@ -735,17 +737,18 @@ impl IJTiffFile {
|
||||
(255.0 * c.green).round() as u8,
|
||||
(255.0 * c.blue).round() as u8,
|
||||
]),
|
||||
Err(_) => Err(anyhow!("could not parse color: {}", c)),
|
||||
Err(_) => Err(Error::ColorParse(c.to_string())),
|
||||
}
|
||||
})
|
||||
.collect::<Result<Vec<_>>>()?,
|
||||
.collect::<Result<Vec<_>, Error>>()?,
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn set_colormap<S: AsRef<str>>(&mut self, name: S) -> Result<()> {
|
||||
/// set colormap from named colormap (see [colorcet](https://crates.io/crates/colorcet))
|
||||
pub fn set_colormap<S: AsRef<str>>(&mut self, name: S) -> Result<(), Error> {
|
||||
let name = name.as_ref();
|
||||
let colormap: LinearGradient = name.parse::<ColorMap>()?.try_into()?;
|
||||
let colormap: LinearGradient = name.parse::<ColorMap>()?.try_into().map_err(|_| Error::Conversion)?;
|
||||
let mut colormap = colormap
|
||||
.colors(256)
|
||||
.into_iter()
|
||||
@@ -832,7 +835,7 @@ impl IJTiffFile {
|
||||
hasher.finish()
|
||||
}
|
||||
|
||||
fn hash_check(&mut self, bytes: &Vec<u8>, offset: u64) -> Result<bool> {
|
||||
fn hash_check(&mut self, bytes: &Vec<u8>, offset: u64) -> Result<bool, Error> {
|
||||
let current_offset = self.file.stream_position()?;
|
||||
self.file.seek(SeekFrom::Start(offset))?;
|
||||
let mut buffer = vec![0; bytes.len()];
|
||||
@@ -842,7 +845,7 @@ impl IJTiffFile {
|
||||
Ok(same)
|
||||
}
|
||||
|
||||
fn write(&mut self, bytes: &Vec<u8>) -> Result<u64> {
|
||||
fn write(&mut self, bytes: &Vec<u8>) -> Result<u64, Error> {
|
||||
let hash = IJTiffFile::hash(&bytes);
|
||||
if self.hashes.contains_key(&hash)
|
||||
&& self.hash_check(bytes, *self.hashes.get(&hash).unwrap())?
|
||||
@@ -860,7 +863,7 @@ impl IJTiffFile {
|
||||
}
|
||||
|
||||
/// save a 2d array to the tiff file at channel c, slice z, and time t
|
||||
pub fn save<'a, A, T>(&mut self, frame: A, c: usize, z: usize, t: usize) -> Result<()>
|
||||
pub fn save<'a, A, T>(&mut self, frame: A, c: usize, z: usize, t: usize) -> Result<(), Error>
|
||||
where
|
||||
A: AsArray<'a, T, Ix2>,
|
||||
T: Bytes + Clone + Send + Sync + 'static,
|
||||
@@ -882,7 +885,7 @@ impl IJTiffFile {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn collect_threads(&mut self, block: bool) -> Result<()> {
|
||||
fn collect_threads(&mut self, block: bool) -> Result<(), Error> {
|
||||
for (c, z, t) in self.threads.keys().cloned().collect::<Vec<_>>() {
|
||||
if block || self.threads[&(c, z, t)].is_finished() {
|
||||
if let Some(thread) = self.threads.remove(&(c, z, t)) {
|
||||
@@ -893,7 +896,7 @@ impl IJTiffFile {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn write_frame(&mut self, frame: CompressedFrame, c: usize, z: usize, t: usize) -> Result<()> {
|
||||
fn write_frame(&mut self, frame: CompressedFrame, c: usize, z: usize, t: usize) -> Result<(), Error> {
|
||||
let mut offsets = Vec::new();
|
||||
let mut bytecounts = Vec::new();
|
||||
for tile in frame.bytes {
|
||||
@@ -940,7 +943,7 @@ impl IJTiffFile {
|
||||
c
|
||||
}
|
||||
|
||||
fn close(&mut self) -> Result<()> {
|
||||
fn close(&mut self) -> Result<(), Error> {
|
||||
self.collect_threads(true)?;
|
||||
let mut c_size = 1;
|
||||
let mut z_size = 1;
|
||||
@@ -1074,7 +1077,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
/// An example of generating julia fractals.
|
||||
fn julia_test() -> Result<()> {
|
||||
fn julia_test() -> Result<(), Error> {
|
||||
let imgx = 800;
|
||||
let imgy = 600;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user