13 Commits

Author SHA1 Message Date
3bc512ab48 chore(release): v0.7.5 (#33)
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
chore(release): 0.7.5

Co-authored-by: cuddle-please <bot@cuddle.sh>
Reviewed-on: https://git.front.kjuulh.io/kjuulh/mad/pulls/33
2025-07-24 22:44:46 +02:00
7d8071d41b feat: print big inner
All checks were successful
continuous-integration/drone/push Build is passing
2025-07-24 22:44:13 +02:00
1cc4138ec7 chore: more error correction
All checks were successful
continuous-integration/drone/push Build is passing
2025-07-24 22:36:39 +02:00
00517daaaa chore: correct error test to not be as verbose 2025-07-24 22:27:04 +02:00
b941dc9a76 chore(release): v0.7.4 (#32)
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
chore(release): 0.7.4

Co-authored-by: cuddle-please <bot@cuddle.sh>
Reviewed-on: https://git.front.kjuulh.io/kjuulh/mad/pulls/32
2025-07-24 22:23:00 +02:00
5da3c83c12 feat: cleanup aggregate error for single error
All checks were successful
continuous-integration/drone/push Build is passing
2025-07-24 22:22:12 +02:00
a16bee8e37 chore(release): v0.7.3 (#31)
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
chore(release): 0.7.3

Co-authored-by: cuddle-please <bot@cuddle.sh>
Reviewed-on: https://git.front.kjuulh.io/kjuulh/mad/pulls/31
2025-07-24 21:09:49 +02:00
a61f00a79d feat: automatic conversion from anyhow::Error and access to aggregate errors
All checks were successful
continuous-integration/drone/push Build is passing
2025-07-24 21:07:35 +02:00
2bd9bd7b8e fix(deps): update all dependencies (#30)
All checks were successful
continuous-integration/drone/push Build is passing
2025-07-21 05:37:49 +02:00
c79ff2fde0 chore(release): v0.7.2 (#14)
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
chore(release): 0.7.2

Co-authored-by: cuddle-please <bot@cuddle.sh>
Reviewed-on: https://git.front.kjuulh.io/kjuulh/mad/pulls/14
2025-06-25 09:54:55 +02:00
c29a84d15e chore
All checks were successful
continuous-integration/drone/push Build is passing
2025-06-25 09:48:35 +02:00
01274c1364 feat: add wait 2025-06-25 09:47:34 +02:00
9c3f2cb7f7 feat: add conditional, allows adding or waiting for close 2025-06-25 09:44:45 +02:00
7 changed files with 189 additions and 21 deletions

View File

@@ -6,6 +6,56 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased] ## [Unreleased]
## [0.7.5] - 2025-07-24
### Added
- print big inner
### Other
- more error correction
- correct error test to not be as verbose
## [0.7.4] - 2025-07-24
### Added
- cleanup aggregate error for single error
## [0.7.3] - 2025-07-24
### Added
- automatic conversion from anyhow::Error and access to aggregate errors
### Fixed
- *(deps)* update all dependencies (#30)
## [0.7.2] - 2025-06-25
### Added
- add wait
- add conditional, allows adding or waiting for close
### Fixed
- *(deps)* update rust crate async-trait to v0.1.86 (#28)
- *(deps)* update rust crate rand to 0.9.0 (#27)
- *(deps)* update rust crate thiserror to v2.0.11 (#26)
- *(deps)* update all dependencies (#25)
- *(deps)* update rust crate async-trait to v0.1.84 (#24)
- *(deps)* update rust crate thiserror to v2.0.9 (#22)
- *(deps)* update rust crate thiserror to v2.0.8 (#21)
- *(deps)* update rust crate thiserror to v2.0.7 (#20)
- *(deps)* update rust crate thiserror to v2.0.6 (#19)
- *(deps)* update rust crate thiserror to v2.0.5 (#18)
- *(deps)* update rust crate tokio-util to v0.7.13 (#17)
### Other
- chore
- *(deps)* update all dependencies (#29)
- *(deps)* update rust crate anyhow to v1.0.95 (#23)
- *(deps)* update all dependencies (#16)
- *(deps)* update rust crate tracing-subscriber to v0.3.19 (#15)
- *(deps)* update rust crate tracing to v0.1.41 (#13)
## [0.7.1] - 2024-11-24 ## [0.7.1] - 2024-11-24
### Fixed ### Fixed

36
Cargo.lock generated
View File

@@ -28,15 +28,15 @@ dependencies = [
[[package]] [[package]]
name = "anyhow" name = "anyhow"
version = "1.0.97" version = "1.0.98"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcfed56ad506cb2c684a14971b8861fdc3baaaae314b9e5f9bb532cbe3ba7a4f" checksum = "e16d2d3311acee920a9eb8d33b8cbc1787ce4a264e85f964c2404b969bdcd487"
[[package]] [[package]]
name = "async-trait" name = "async-trait"
version = "0.1.87" version = "0.1.88"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d556ec1359574147ec0c4fc5eb525f3f23263a592b1a9c07e0a75b427de55c97" checksum = "e539d3fca749fcee5236ab05e93a52867dd549cc157c8cb7f99595f3cedffdb5"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@@ -201,6 +201,17 @@ version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024"
[[package]]
name = "io-uring"
version = "0.7.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b86e202f00093dcba4275d4636b93ef9dd75d025ae560d2521b45ea28ab49013"
dependencies = [
"bitflags",
"cfg-if",
"libc",
]
[[package]] [[package]]
name = "lazy_static" name = "lazy_static"
version = "1.5.0" version = "1.5.0"
@@ -267,7 +278,7 @@ dependencies = [
[[package]] [[package]]
name = "notmad" name = "notmad"
version = "0.7.1" version = "0.7.4"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"async-trait", "async-trait",
@@ -377,13 +388,12 @@ dependencies = [
[[package]] [[package]]
name = "rand" name = "rand"
version = "0.9.0" version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3779b94aeb87e8bd4e834cee3650289ee9e0d5677f976ecdb6d219e5f4f6cd94" checksum = "6db2770f06117d490610c7488547d543617b21bfa07796d7a12f6f1bd53850d1"
dependencies = [ dependencies = [
"rand_chacha", "rand_chacha",
"rand_core", "rand_core",
"zerocopy 0.8.14",
] ]
[[package]] [[package]]
@@ -557,17 +567,19 @@ dependencies = [
[[package]] [[package]]
name = "tokio" name = "tokio"
version = "1.44.2" version = "1.46.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6b88822cbe49de4185e3a4cbf8321dd487cf5fe0c5c65695fef6346371e9c48" checksum = "0cc3a2344dafbe23a245241fe8b09735b521110d30fcefbbd5feb1797ca35d17"
dependencies = [ dependencies = [
"backtrace", "backtrace",
"bytes", "bytes",
"io-uring",
"libc", "libc",
"mio", "mio",
"parking_lot", "parking_lot",
"pin-project-lite", "pin-project-lite",
"signal-hook-registry", "signal-hook-registry",
"slab",
"socket2", "socket2",
"tokio-macros", "tokio-macros",
"windows-sys", "windows-sys",
@@ -586,9 +598,9 @@ dependencies = [
[[package]] [[package]]
name = "tokio-util" name = "tokio-util"
version = "0.7.13" version = "0.7.15"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d7fcaa8d55a2bdd6b83ace262b016eca0d79ee02818c5c1bcdf0305114081078" checksum = "66a539a9ad6d5d281510d5bd368c973d636c02dbf8a67300bfb6b950696ad7df"
dependencies = [ dependencies = [
"bytes", "bytes",
"futures-core", "futures-core",

View File

@@ -3,7 +3,7 @@ members = ["crates/*"]
resolver = "2" resolver = "2"
[workspace.package] [workspace.package]
version = "0.7.1" version = "0.7.5"
[workspace.dependencies] [workspace.dependencies]
mad = { path = "crates/mad" } mad = { path = "crates/mad" }

View File

@@ -4,8 +4,8 @@ version.workspace = true
description = "notmad is a life-cycle manager for long running rust operations" description = "notmad is a life-cycle manager for long running rust operations"
license = "MIT" license = "MIT"
repository = "https://github.com/kjuulh/mad" repository = "https://github.com/kjuulh/mad"
author = "kjuulh" authors = ["kjuulh"]
edition = "2021" edition = "2024"
[dependencies] [dependencies]
anyhow.workspace = true anyhow.workspace = true

View File

@@ -1,22 +1,26 @@
use futures::stream::FuturesUnordered; use futures::stream::FuturesUnordered;
use futures_util::StreamExt; use futures_util::StreamExt;
use std::{fmt::Display, sync::Arc}; use std::{fmt::Display, sync::Arc};
use tokio::signal::unix::{signal, SignalKind}; use tokio::signal::unix::{SignalKind, signal};
use tokio_util::sync::CancellationToken; use tokio_util::sync::CancellationToken;
use crate::waiter::Waiter;
mod waiter;
#[derive(thiserror::Error, Debug)] #[derive(thiserror::Error, Debug)]
pub enum MadError { pub enum MadError {
#[error("component failed: {0}")] #[error("component: {0:#?}")]
Inner(#[source] anyhow::Error), Inner(#[source] anyhow::Error),
#[error("component(s) failed: {run}")] #[error("component: {run:#?}")]
RunError { run: anyhow::Error }, RunError { run: anyhow::Error },
#[error("component(s) failed: {close}")] #[error("component(s) failed: {close}")]
CloseError { close: anyhow::Error }, CloseError { close: anyhow::Error },
#[error("component(s) failed: {0}")] #[error("component(s): {0}")]
AggregateError(AggregateError), AggregateError(AggregateError),
#[error("setup not defined")] #[error("setup not defined")]
@@ -26,13 +30,33 @@ pub enum MadError {
CloseNotDefined, CloseNotDefined,
} }
impl From<anyhow::Error> for MadError {
fn from(value: anyhow::Error) -> Self {
Self::Inner(value)
}
}
#[derive(Debug)] #[derive(Debug)]
pub struct AggregateError { pub struct AggregateError {
errors: Vec<MadError>, errors: Vec<MadError>,
} }
impl AggregateError {
pub fn get_errors(&self) -> &[MadError] {
&self.errors
}
}
impl Display for AggregateError { impl Display for AggregateError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if self.errors.is_empty() {
return Ok(());
}
if self.errors.len() == 1 {
return f.write_str(&self.errors.first().unwrap().to_string());
}
f.write_str("MadError::AggregateError: (")?; f.write_str("MadError::AggregateError: (")?;
for error in &self.errors { for error in &self.errors {
@@ -70,6 +94,23 @@ impl Mad {
self self
} }
pub fn add_conditional(&mut self, condition: bool, component: impl IntoComponent) -> &mut Self {
if condition {
self.components.push(component.into_component());
} else {
self.components
.push(Waiter::new(component.into_component()).into_component())
}
self
}
pub fn add_wait(&mut self) -> &mut Self {
self.components.push(Waiter::default().into_component());
self
}
pub fn add_fn<F, Fut>(&mut self, f: F) -> &mut Self pub fn add_fn<F, Fut>(&mut self, f: F) -> &mut Self
where where
F: Fn(CancellationToken) -> Fut + Send + Sync + 'static, F: Fn(CancellationToken) -> Fut + Send + Sync + 'static,
@@ -100,7 +141,7 @@ impl Mad {
(Err(run), Err(close)) => { (Err(run), Err(close)) => {
return Err(MadError::AggregateError(AggregateError { return Err(MadError::AggregateError(AggregateError {
errors: vec![run, close], errors: vec![run, close],
})) }));
} }
(Ok(_), Ok(_)) => {} (Ok(_), Ok(_)) => {}
(Ok(_), Err(close)) => return Err(close), (Ok(_), Err(close)) => return Err(close),

48
crates/mad/src/waiter.rs Normal file
View File

@@ -0,0 +1,48 @@
use std::sync::Arc;
use async_trait::async_trait;
use tokio_util::sync::CancellationToken;
use crate::{Component, MadError};
pub struct DefaultWaiter {}
#[async_trait]
impl Component for DefaultWaiter {
async fn run(&self, _cancellation_token: CancellationToken) -> Result<(), MadError> {
panic!("should never be called");
}
}
pub struct Waiter {
comp: Arc<dyn Component + Send + Sync + 'static>,
}
impl Default for Waiter {
fn default() -> Self {
Self {
comp: Arc::new(DefaultWaiter {}),
}
}
}
impl Waiter {
pub fn new(c: Arc<dyn Component + Send + Sync + 'static>) -> Self {
Self { comp: c }
}
}
#[async_trait]
impl Component for Waiter {
fn name(&self) -> Option<String> {
match self.comp.name() {
Some(name) => Some(format!("waiter/{name}")),
None => Some("waiter".into()),
}
}
async fn run(&self, cancellation_token: CancellationToken) -> Result<(), MadError> {
cancellation_token.cancelled().await;
Ok(())
}
}

View File

@@ -1,7 +1,7 @@
use std::sync::Arc; use std::sync::Arc;
use async_trait::async_trait; use async_trait::async_trait;
use notmad::{Component, Mad}; use notmad::{Component, Mad, MadError};
use rand::Rng; use rand::Rng;
use tokio::sync::Mutex; use tokio::sync::Mutex;
use tokio_util::sync::CancellationToken; use tokio_util::sync::CancellationToken;
@@ -137,3 +137,20 @@ async fn test_can_shutdown_gracefully() -> anyhow::Result<()> {
Ok(()) Ok(())
} }
#[test]
fn test_can_easily_transform_error() -> anyhow::Result<()> {
fn fallible() -> anyhow::Result<()> {
Ok(())
}
fn inner() -> Result<(), MadError> {
fallible()?;
Ok(())
}
inner()?;
Ok(())
}