Skip to content

Commit

Permalink
add thiserror
Browse files Browse the repository at this point in the history
  • Loading branch information
DaviRain-Su committed Jun 4, 2024
1 parent 15a5366 commit 670aab7
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 36 deletions.
56 changes: 56 additions & 0 deletions Cargo.lock

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

5 changes: 1 addition & 4 deletions rust/Cargo.toml
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,8 @@ libm = "0.2.5"
log = "0.4.17"
num-traits = "0.2.15"
sha2 = "0.10"
thiserror = "1.0.61"

[dev-dependencies]
hex-literal = "0.3.4"
rs_merkle = "1.2.0"

[features]
default = ["std"]
std = ["anyhow/std", "log/std", "sha2/std"]
9 changes: 0 additions & 9 deletions rust/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,3 @@ Introduction to algorithms thrid edition implement by rust programming
- Insert Sort algorithms
- Select Sort algorithms
- Merge Sort algorithms, but not only support ascending order


## support no-std feature

config setting:

```toml
algorithms-rs = { version = "0.1", default-features = false }
```
4 changes: 2 additions & 2 deletions rust/src/heap.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use alloc::vec::Vec;
use core::fmt::{Debug, Display};
use std::fmt::{Debug, Display};
use std::vec::Vec;

fn parent(i: usize) -> usize {
i / 2
Expand Down
2 changes: 0 additions & 2 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![cfg_attr(not(feature = "std"), no_std)]
//! # Introduction to algorithms
//! > thrid edition implement by rust programming
//!
Expand All @@ -15,7 +14,6 @@
//! - push queue tail element
//! - pop queue head element
//!
extern crate alloc;

/// heap sort module
pub mod heap;
Expand Down
49 changes: 31 additions & 18 deletions rust/src/queue.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
use alloc::format;
use alloc::vec;
use alloc::vec::Vec;
use std::vec;
use std::vec::Vec;
use thiserror::Error;

#[derive(Debug, Error)]
pub enum QueueError {
#[error("Custom Error(`0`)")]
Custom(String),
#[error("Queue overflow")]
Overflow,
#[error("Queue underflow")]
Underflow,
#[error("failed get value index on {0}")]
FailedGetValue(usize),
}

/// Queue data structure
#[derive(Debug)]
Expand Down Expand Up @@ -60,17 +72,14 @@ impl<T: Clone + Default> Queue<T> {
///
/// assert_eq!(queue.is_empty(), false);
/// ```
pub fn en_queue(&mut self, element: T) -> anyhow::Result<()> {
pub fn en_queue(&mut self, element: T) -> Result<(), QueueError> {
if self.head == (self.tail + 1) % self.len {
return Err(anyhow::anyhow!("overflow"));
return Err(QueueError::Overflow);
}
if let Some(value) = self.data.get_mut(self.tail) {
*value = element;
} else {
return Err(anyhow::anyhow!(format!(
"get index of {} element",
self.tail
)));
return Err(QueueError::FailedGetValue(self.tail));
}

if self.tail == (self.len - 1) {
Expand All @@ -97,9 +106,9 @@ impl<T: Clone + Default> Queue<T> {
///
/// assert_eq!(queue.is_empty(), true);
/// ```
pub fn de_queue(&mut self) -> anyhow::Result<T> {
pub fn de_queue(&mut self) -> Result<T, QueueError> {
if self.is_empty() {
return Err(anyhow::anyhow!("underflow"));
return Err(QueueError::Underflow);
}
let element = self.data.get(self.head);
if self.head == (self.len - 1) {
Expand All @@ -115,16 +124,20 @@ impl<T: Clone + Default> Queue<T> {
mod tests {
use super::*;

fn process_result<T>(result: Result<T, anyhow::Error>) {
fn process_result<T>(result: Result<T, QueueError>) {
match result {
Ok(_value) => {}
Err(err) => {
if err.to_string() == *"overflow" {
assert_eq!(err.to_string(), "overflow".to_string());
} else if err.to_string() == *"underflow" {
assert_eq!(err.to_string(), "underflow".to_string());
Err(err) => match err {
QueueError::Overflow => {
assert_eq!(err.to_string(), "Queue overflow".to_string());
}
QueueError::Underflow => {
assert_eq!(err.to_string(), "Queue underflow".to_string());
}
e => {
panic!("{e:?}")
}
}
},
}
}

Expand Down
2 changes: 1 addition & 1 deletion rust/src/stack.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use alloc::vec::Vec;
use std::vec::Vec;

/// # stack data structure
/// 在栈中,被删除的是最近插入的元素: 栈的实现是一种后进先出策略。
Expand Down

0 comments on commit 670aab7

Please sign in to comment.