Skip to content

Commit

Permalink
Merge pull request #11 from koopa1338/main
Browse files Browse the repository at this point in the history
Replace deprecated failure with anyhow and thiserror crate
  • Loading branch information
pmagaz authored Feb 28, 2021
2 parents 173d0c0 + eec90e4 commit ffeb923
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 72 deletions.
15 changes: 8 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ path = "src/lib.rs"

[dependencies]
uuid = { version = "0.8.1", features = ["serde", "v4"] }
failure = "0.1.8"
anyhow = "1.0.38"
thiserror = "1.0.24"
tokio = { version = "0.2", features = ["macros","fs","stream","sync","rt-util"] }
serde = { version = "1.0", features = ["derive"] }
futures = "0.3.8"
Expand Down
66 changes: 24 additions & 42 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,82 +1,64 @@
use failure::{Backtrace, Context, Fail};
use std::fmt::{self, Display};
use thiserror::Error;
use uuid::Uuid;

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

#[derive(Copy, Clone, Eq, PartialEq, Debug, Fail)]
#[derive(Copy, Clone, Eq, PartialEq, Debug, Error)]
pub enum RedDbErrorKind {
//STORAGE
#[fail(display = "Data corrupted!")]
#[error("Data corrupted!")]
DataCorruption,
#[fail(display = "Data compacted corrupted!")]
#[error("Data compacted corrupted!")]
Compact,
#[fail(display = "Could not compact storage")]
#[error("Could not compact storage")]
Storagepersist,
#[fail(display = "Could not flush data into storage")]
#[error("Could not flush data into storage")]
FlushData,
#[fail(display = "Could not flush data")]
#[error("Could not flush data")]
AppendData,
#[fail(display = "Could not append data ")]
#[error("Could not append data ")]
StorageInit,
#[fail(display = "Could not init storage")]
#[error("Could not init storage")]
StorageData,
#[fail(display = "Could not read storage data")]
#[error("Could not read storage data")]
ReadContent,
#[fail(display = "Could not load storage content")]
#[error("Could not load storage content")]
ContentLoad,
#[fail(display = "Could not persist data into storage")]
#[error("Could not persist data into storage")]
Datapersist,
// uuids
#[fail(display = "Could not find _id {}", _id)]
#[error("Could not find _id {_id}")]
NotFound { _id: Uuid },
#[fail(display = "Could not delete _id")]
#[error("Could not delete _id")]
Deletekey,
#[fail(display = "Could not unlock mutex")]
#[error("Could not unlock mutex")]
Mutex,
#[fail(display = "Database poisoned!")]
#[error("Database poisoned!")]
Poisoned,
#[fail(display = "data poisoned!")]
#[error("data poisoned!")]
PoisonedValue,
// SERDE
#[fail(display = "Could not deserialize data")]
#[error("Could not deserialize data")]
Deserialization,
#[fail(display = "Could not serialize data")]
#[error("Could not serialize data")]
Serialization,
}

#[derive(Debug)]
#[derive(Debug, Error)]
pub struct RedDbError {
err: Context<RedDbErrorKind>,
err: RedDbErrorKind,
}

impl RedDbError {
pub fn kind(&self) -> RedDbErrorKind {
*self.err.get_context()
self.err
}
}

impl From<RedDbErrorKind> for RedDbError {
fn from(kind: RedDbErrorKind) -> RedDbError {
RedDbError {
err: Context::new(kind),
}
}
}

impl From<Context<RedDbErrorKind>> for RedDbError {
fn from(err: Context<RedDbErrorKind>) -> RedDbError {
RedDbError { err }
}
}

impl Fail for RedDbError {
fn cause(&self) -> Option<&dyn Fail> {
self.err.cause()
}

fn backtrace(&self) -> Option<&Backtrace> {
self.err.backtrace()
RedDbError { err: kind }
}
}

Expand Down
15 changes: 7 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use failure::ResultExt;
use futures::stream::{self, StreamExt};
use futures::TryStreamExt;
use std::collections::HashMap;
Expand Down Expand Up @@ -126,7 +125,7 @@ where
self.storage
.persist(&[doc.to_owned()])
.await
.context(RedDbErrorKind::Datapersist)?;
.map_err(|_| RedDbErrorKind::Datapersist)?;
Ok(doc)
}

Expand All @@ -142,7 +141,7 @@ where
self.storage
.persist(&docs)
.await
.context(RedDbErrorKind::Datapersist)?;
.map_err(|_| RedDbErrorKind::Datapersist)?;

Ok(docs)
}
Expand Down Expand Up @@ -183,7 +182,7 @@ where
self.storage
.persist(&[doc])
.await
.context(RedDbErrorKind::Datapersist)?;
.map_err(|_| RedDbErrorKind::Datapersist)?;

Ok(true)
} else {
Expand Down Expand Up @@ -278,7 +277,7 @@ where
self.storage
.persist(&docs)
.await
.context(RedDbErrorKind::Datapersist)?;
.map_err(|_| RedDbErrorKind::Datapersist)?;

Ok(result)
}
Expand All @@ -297,7 +296,7 @@ where
self.storage
.persist(&docs)
.await
.context(RedDbErrorKind::Datapersist)?;
.map_err(|_| RedDbErrorKind::Datapersist)?;

Ok(docs.len())
}
Expand All @@ -309,7 +308,7 @@ where
Ok(self
.serializer
.serialize(value)
.context(RedDbErrorKind::Serialization)?)
.map_err(|_| RedDbErrorKind::Serialization)?)
}

fn deserialize<T>(&self, value: &[u8]) -> Result<T>
Expand All @@ -319,7 +318,7 @@ where
Ok(self
.serializer
.deserialize(value)
.context(RedDbErrorKind::Deserialization)?)
.map_err(|_| RedDbErrorKind::Deserialization)?)
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/serializer/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use failure::Error;
use anyhow::{Error, Result};
use serde::{Deserialize, Serialize};
use std::default::Default;
use std::result::Result;

mod bin;
mod json;
Expand Down
21 changes: 10 additions & 11 deletions src/storage/file.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use async_trait::async_trait;
use core::fmt::Debug;
use failure::ResultExt;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

Expand Down Expand Up @@ -70,7 +69,7 @@ where
let document: Document<T> = self
.serializer
.deserialize(byte_str)
.context(RedDbErrorKind::DataCorruption)
.map_err(|_| RedDbErrorKind::DataCorruption)
.unwrap();
let id = document._id;
let st = document._st;
Expand All @@ -85,7 +84,7 @@ where

self.compact_data::<T>(&map)
.await
.context(RedDbErrorKind::Compact)?;
.map_err(|_| RedDbErrorKind::Compact)?;

Ok(map)
}
Expand All @@ -101,7 +100,7 @@ where

self.append(&serialized)
.await
.context(RedDbErrorKind::AppendData)?;
.map_err(|_| RedDbErrorKind::AppendData)?;

Ok(())
}
Expand All @@ -121,12 +120,12 @@ where
let data: T = self
.serializer
.deserialize(&*data)
.context(RedDbErrorKind::DataCorruption)
.map_err(|_| RedDbErrorKind::DataCorruption)
.unwrap();

self.serializer
.serialize(&Document::new(*id, data, Status::In))
.context(RedDbErrorKind::DataCorruption)
.map_err(|_| RedDbErrorKind::DataCorruption)
.unwrap()
})
.collect();
Expand All @@ -144,23 +143,23 @@ where
async fn flush_data<P: AsRef<Path>>(&self, path: P, data: &[u8]) -> Result<()> {
let mut storage = File::create(path)
.await
.context(RedDbErrorKind::DataCorruption)?;
.map_err(|_| RedDbErrorKind::DataCorruption)?;
storage
.set_len(0)
.await
.context(RedDbErrorKind::FlushData)?;
.map_err(|_| RedDbErrorKind::FlushData)?;
storage
.seek(SeekFrom::Start(0))
.await
.context(RedDbErrorKind::FlushData)?;
.map_err(|_| RedDbErrorKind::FlushData)?;
storage
.write_all(&data)
.await
.context(RedDbErrorKind::FlushData)?;
.map_err(|_| RedDbErrorKind::FlushData)?;
storage
.sync_all()
.await
.context(RedDbErrorKind::FlushData)?;
.map_err(|_| RedDbErrorKind::FlushData)?;
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::io::{BufRead, BufReader};
use std::path::Path;
use uuid::Uuid;

type Result<T, E = Error> = std::result::Result<T, E>;
type Result<T, E = Error> = anyhow::Result<T, E>;

async fn setup() -> Result<()> {
if Path::new(".db.yaml").exists() {
Expand Down

0 comments on commit ffeb923

Please sign in to comment.