Skip to content
This repository has been archived by the owner on Sep 16, 2024. It is now read-only.

Commit

Permalink
Define errors for various use cases (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
gwendalF authored Oct 2, 2023
1 parent 1b3a451 commit 4dc4709
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 135 deletions.
7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ lazy_static = "1.4.0"
canonical-path = "2.0.2"
pathdiff = "0.2.1"
image = "0.24.2"
pdfium-render = { git = "https://github.com/ajrcarey/pdfium-render", rev = "d2559c1",features = ["thread_safe", "sync"] }
pdfium-render = { git = "https://github.com/ajrcarey/pdfium-render", rev = "d2559c1", features = [
"thread_safe",
"sync",
] }
libloading = "0.7.3"
serde_json = "1.0.82"
serde = { version = "1.0.138", features = ["derive"] }
Expand All @@ -28,6 +31,7 @@ zip = "0.6.2"
tokio = { version = "1", features = ["full"] }
itertools = "0.10.5"
once_cell = "1.16.0"
thiserror = "1"

[dev-dependencies]
tempdir = "0.3.7"
Expand All @@ -40,4 +44,3 @@ libloading = "0.7.3"
tar = "0.4.38"
target-lexicon = "0.12.4"
ureq = "2.4.0"

33 changes: 33 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use std::str::Utf8Error;

use thiserror::Error;

pub type Result<T> = std::result::Result<T, ArklibError>;

#[derive(Error, Debug)]
pub enum ArklibError {
#[error("IO error")]
Io(#[from] std::io::Error),
#[error("Path error: {0}")]
Path(String),
#[error("There is some collision: {0}")]
Collision(String),
#[error("Parsing error")]
Parse,
#[error("Networking error")]
Network,
#[error(transparent)]
Other(#[from] anyhow::Error),
}

impl From<reqwest::Error> for ArklibError {
fn from(_: reqwest::Error) -> Self {
Self::Network
}
}

impl From<Utf8Error> for ArklibError {
fn from(_: Utf8Error) -> Self {
Self::Parse
}
}
39 changes: 20 additions & 19 deletions src/id.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use anyhow::Error;
use anyhow::anyhow;
use crc32fast::Hasher;
use log;
use serde::{Deserialize, Serialize};
use std::fmt::{self, Display, Formatter};
use std::io::Read;
Expand All @@ -7,8 +9,7 @@ use std::path::Path;
use std::str::FromStr;
use std::{fs, num::TryFromIntError};

use crc32fast::Hasher;
use log;
use crate::{ArklibError, Result};

#[derive(
Eq,
Expand All @@ -34,14 +35,12 @@ impl Display for ResourceId {
}

impl FromStr for ResourceId {
type Err = Error;
type Err = ArklibError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let (l, r) = s
.split_once('-')
.ok_or(Error::msg("Couldn't split ID"))?;
let data_size: u64 = l.parse()?;
let crc32: u32 = r.parse()?;
fn from_str(s: &str) -> Result<Self> {
let (l, r) = s.split_once('-').ok_or(ArklibError::Parse)?;
let data_size: u64 = l.parse().map_err(|_| ArklibError::Parse)?;
let crc32: u32 = r.parse().map_err(|_| ArklibError::Parse)?;

Ok(ResourceId { data_size, crc32 })
}
Expand All @@ -51,7 +50,7 @@ impl ResourceId {
pub fn compute<P: AsRef<Path>>(
data_size: u64,
file_path: P,
) -> Result<Self, Error> {
) -> Result<Self> {
log::trace!(
"[compute] file {} with size {} mb",
file_path.as_ref().display(),
Expand All @@ -66,16 +65,18 @@ impl ResourceId {
ResourceId::compute_reader(data_size, &mut reader)
}

pub fn compute_bytes(bytes: &[u8]) -> Result<Self, Error> {
let data_size = bytes.len().try_into()?; //.unwrap();
pub fn compute_bytes(bytes: &[u8]) -> Result<Self> {
let data_size = bytes.len().try_into().map_err(|_| {
ArklibError::Other(anyhow!("Can't convert usize to u64"))
})?; //.unwrap();
let mut reader = BufReader::with_capacity(BUFFER_CAPACITY, bytes);
ResourceId::compute_reader(data_size, &mut reader)
}

pub fn compute_reader<R: Read>(
data_size: u64,
reader: &mut BufReader<R>,
) -> Result<Self, Error> {
) -> Result<Self> {
assert!(reader.buffer().is_empty());

log::trace!(
Expand All @@ -92,16 +93,16 @@ impl ResourceId {
}
hasher.update(reader.buffer());
reader.consume(bytes_read_iteration);
bytes_read += u32::try_from(bytes_read_iteration)?;
bytes_read +=
u32::try_from(bytes_read_iteration).map_err(|_| {
ArklibError::Other(anyhow!("Can't convert usize to u32"))
})?;
}

let crc32: u32 = hasher.finalize().into();
log::trace!("[compute] {} bytes has been read", bytes_read);
log::trace!("[compute] checksum: {:#02x}", crc32);
assert_eq!(
bytes_read,
(data_size.try_into() as Result<u32, TryFromIntError>).unwrap()
);
assert_eq!(std::convert::Into::<u64>::into(bytes_read), data_size);

Ok(ResourceId { data_size, crc32 })
}
Expand Down
Loading

0 comments on commit 4dc4709

Please sign in to comment.