Skip to content

Commit

Permalink
AUTO : Forward from derives_refactoring_5 to alpha (#1397)
Browse files Browse the repository at this point in the history
global error tools cleaning
  • Loading branch information
wtools-bot authored Jul 2, 2024
1 parent 14e1bdf commit f29b6d0
Show file tree
Hide file tree
Showing 88 changed files with 976 additions and 1,098 deletions.
2 changes: 1 addition & 1 deletion module/alias/cargo_will/src/bin/cargo-will.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#[ allow( unused_imports ) ]
use::willbe::*;

fn main() -> Result< (), wtools::error::for_app::Error >
fn main() -> Result< (), wtools::error::untyped::Error >
{
let args = std::env::args().skip( 1 ).collect();
Ok( willbe::run( args )? )
Expand Down
2 changes: 1 addition & 1 deletion module/alias/cargo_will/src/bin/will.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#[ allow( unused_imports ) ]
use::willbe::*;

fn main() -> Result< (), wtools::error::for_app::Error >
fn main() -> Result< (), wtools::error::untyped::Error >
{
Ok( willbe::run( std::env::args().collect() )? )
}
2 changes: 1 addition & 1 deletion module/alias/cargo_will/src/bin/willbe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#[ allow( unused_imports ) ]
use::willbe::*;

fn main() -> Result< (), wtools::error::for_app::Error >
fn main() -> Result< (), wtools::error::untyped::Error >
{
Ok( willbe::run( std::env::args().collect() )? )
}
2 changes: 1 addition & 1 deletion module/alias/willbe2/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#[ allow( unused_imports ) ]
use ::willbe2::*;

// fn main() -> Result< (), wtools::error::for_app::Error >
// fn main() -> Result< (), wtools::error::untyped::Error >
// {
// Ok( willbe::run()? )
// }
Expand Down
2 changes: 1 addition & 1 deletion module/core/error_tools/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fn main()
}

#[ cfg( feature = "enabled" ) ]
fn f1() -> error_tools::Result< () >
fn f1() -> error_tools::untyped::Result< () >
{
let _read = std::fs::read_to_string( "Cargo.toml" )?;
Err( error_tools::BasicError::new( "Some error" ).into() )
Expand Down
2 changes: 1 addition & 1 deletion module/core/error_tools/examples/error_tools_trivial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn main()
}

#[ cfg( not( feature = "no_std" ) ) ]
fn f1() -> error_tools::Result< () >
fn f1() -> error_tools::untyped::Result< () >
{
let _read = std::fs::read_to_string( "Cargo.toml" )?;
Err( error_tools::BasicError::new( "Some error" ).into() )
Expand Down
108 changes: 104 additions & 4 deletions module/core/error_tools/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,100 @@
/// Internal namespace.
pub( crate ) mod private
{
pub use std::error::Error as ErrorInterface;
pub use std::error::Error as ErrorTrait;

/// This trait allows adding extra context or information to an error, creating a tuple of the additional
/// context and the original error. This is particularly useful for error handling when you want to include
/// more details in the error without losing the original error value.
///
/// The `ErrWith` trait provides methods to wrap an error with additional context, either by using a closure
/// that generates the context or by directly providing the context.
///
/// ```
pub trait ErrWith< ReportErr, ReportOk, E >
{
/// Takes a closure `f` that returns a value of type `ReportErr`, and uses it to wrap an error of type `(ReportErr, E)`
/// in the context of a `Result` of type `ReportOk`.
///
/// This method allows you to add additional context to an error by providing a closure that generates the context.
///
/// # Arguments
///
/// * `f` - A closure that returns the additional context of type `ReportErr`.
///
/// # Returns
///
/// A `Result` of type `ReportOk` if the original result is `Ok`, or a tuple `(ReportErr, E)` containing the additional
/// context and the original error if the original result is `Err`.
///
/// # Example
///
/// ```rust
/// use error_tools::ErrWith;
/// let result : Result< (), std::io::Error > = Err( std::io::Error::new( std::io::ErrorKind::Other, "an error occurred" ) );
/// let result_with_context : Result< (), ( &str, std::io::Error ) > = result.err_with( || "additional context" );
/// ```
fn err_with< F >( self, f : F ) -> std::result::Result< ReportOk, ( ReportErr, E ) >
where
F : FnOnce() -> ReportErr;

/// Takes a reference to a `ReportErr` value and uses it to wrap an error of type `(ReportErr, E)`
/// in the context of a `Result` of type `ReportOk`.
///
/// This method allows you to add additional context to an error by providing a reference to the context.
///
/// # Arguments
///
/// * `report` - A reference to the additional context of type `ReportErr`.
///
/// # Returns
///
/// A `Result` of type `ReportOk` if the original result is `Ok`, or a tuple `(ReportErr, E)` containing the additional
/// context and the original error if the original result is `Err`.
///
/// # Example
///
/// ```rust
/// use error_tools::ErrWith;
/// let result : Result< (), std::io::Error > = Err( std::io::Error::new( std::io::ErrorKind::Other, "an error occurred" ) );
/// let report = "additional context";
/// let result_with_report : Result< (), ( &str, std::io::Error ) > = result.err_with_report( &report );
/// ```
fn err_with_report( self, report : &ReportErr ) -> std::result::Result< ReportOk, ( ReportErr, E ) >
where
ReportErr : Clone;

}

impl< ReportErr, ReportOk, E, IntoError > ErrWith< ReportErr, ReportOk, E >
for std::result::Result< ReportOk, IntoError >
where
IntoError : Into< E >,
{

fn err_with< F >( self, f : F ) -> std::result::Result< ReportOk, ( ReportErr, E ) >
where
F : FnOnce() -> ReportErr,
{
self.map_err( | e | ( f(), e.into() ) )
}

#[ inline( always ) ]
fn err_with_report( self, report : &ReportErr ) -> std::result::Result< ReportOk, ( ReportErr, E ) >
where
ReportErr : Clone,
Self : Sized,
{
self.map_err( | e | ( report.clone(), e.into() ) )
}

}

/// A type alias for a `Result` that contains an error which is a tuple of a report and an original error.
///
/// This is useful when you want to report additional information along with an error. The `ResultWithReport` type
/// helps in defining such results more concisely.
pub type ResultWithReport< Report, Error > = Result< Report, ( Report, Error ) >;

///
/// Macro to generate an error descriptor.
Expand Down Expand Up @@ -90,7 +183,7 @@ pub( crate ) mod private
}
}

impl ErrorInterface for BasicError
impl ErrorTrait for BasicError
{
fn description( &self ) -> &str
{
Expand Down Expand Up @@ -139,17 +232,24 @@ pub mod orphan
pub mod exposed
{
use super::*;

pub use private::ErrWith;
pub use private::ResultWithReport;

#[ doc( inline ) ]
pub use super::prelude::*;
pub use super::prelude::*; // xxx
}

/// Prelude to use essentials: `use my_module::prelude::*`.
#[ allow( unused_imports ) ]
pub mod prelude
{
// xxx

pub use super::private::err;
pub use super::private::return_err;
pub use super::private::ErrorInterface;
pub use super::private::ErrorTrait;
pub use super::private::BasicError;

}
// xxx : review
1 change: 0 additions & 1 deletion module/core/error_tools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ pub mod assert;
pub mod error;

/// Namespace with dependencies.

#[ cfg( feature = "enabled" ) ]
pub mod dependency
{
Expand Down
30 changes: 18 additions & 12 deletions module/core/error_tools/src/untyped.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ pub mod protected
{
#[ doc( inline ) ]
pub use super::orphan::*;

#[ doc( inline ) ]
pub use ::anyhow::
{
Chain,
Context,
Error,
Ok,
Result,
};

}

/// Shared with parent namespace of the module
Expand All @@ -27,10 +38,13 @@ pub mod orphan
pub use super::exposed::*;

#[ doc( inline ) ]
#[ allow( unused_imports ) ]
pub use ::anyhow::*;

// xxx : qqq : be specific
pub use ::anyhow::
{
anyhow,
format_err,
ensure,
bail,
};

}

Expand All @@ -43,14 +57,6 @@ pub mod exposed
#[ doc( inline ) ]
pub use super::prelude::*;

#[ doc( inline ) ]
#[ allow( unused_imports ) ]
pub use ::anyhow::Result;

// #[ doc( inline ) ]
// #[ allow( unused_imports ) ]
// pub use ::anyhow::prelude::*;

}

/// Prelude to use essentials: `use my_module::prelude::*`.
Expand Down
6 changes: 3 additions & 3 deletions module/core/error_tools/tests/inc/basic_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ tests_impls!

fn use1()
{
use std::error::Error as ErrorInterface;
use std::error::Error as ErrorTrait;
use the_module::BasicError as Error;

// test.case( "basic" );
Expand All @@ -54,7 +54,7 @@ tests_impls!

fn use2()
{
use the_module::{ BasicError, ErrorInterface };
use the_module::{ BasicError, ErrorTrait };

// test.case( "basic" );

Expand Down Expand Up @@ -98,7 +98,7 @@ tests_impls!
fn sample()
{
#[ cfg( not( feature = "no_std" ) ) ]
fn f1() -> the_module::Result< () >
fn f1() -> the_module::untyped::Result< () >
{
let _read = std::fs::read_to_string( "Cargo.toml" )?;
Err( the_module::BasicError::new( "Some error" ).into() )
Expand Down
31 changes: 31 additions & 0 deletions module/core/error_tools/tests/inc/err_with_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#![ allow( unused_imports ) ]
use super::*;

#[ test ]
fn err_with()
{

use the_module::ErrWith;
let result : Result< (), std::io::Error > = Err( std::io::Error::new( std::io::ErrorKind::Other, "an error occurred" ) );
let got : Result< (), ( &str, std::io::Error ) > = result.err_with( || "additional context" );
let exp : Result< (), ( &str, std::io::Error ) > = Err( ( "additional context", std::io::Error::new( std::io::ErrorKind::Other, "an error occurred" ) ) );
assert_eq!( got.as_ref().unwrap_err().0, exp.as_ref().unwrap_err().0 );
assert!( got.is_err() );

}

//

#[ test ]
fn err_with_report()
{

use error_tools::ErrWith;
let result : Result< (), std::io::Error > = Err( std::io::Error::new( std::io::ErrorKind::Other, "an error occurred" ) );
let report = "additional context";
let got : Result< (), ( &str, std::io::Error ) > = result.err_with_report( &report );
let exp : Result< (), ( &str, std::io::Error ) > = Err( ( "additional context", std::io::Error::new( std::io::ErrorKind::Other, "an error occurred" ) ) );
assert_eq!( got.as_ref().unwrap_err().0, exp.as_ref().unwrap_err().0 );
assert!( got.is_err() );

}
5 changes: 3 additions & 2 deletions module/core/error_tools/tests/inc/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#[ allow( unused_imports ) ]
use super::*;

mod basic_test;
mod for_app_test;
mod assert_test;
mod basic_test;
mod err_with_test;
mod untyped_test;
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ tests_impls!
{
// test.case( "from parse usize error" );

let err = the_module::for_app::anyhow!( "err" );
a_id!( the_module::for_app::Error::is::< &str >( &err ), true );
let err = the_module::untyped::anyhow!( "err" );
a_id!( the_module::untyped::Error::is::< &str >( &err ), true );
a_id!( err.is::< &str >(), true );
a_id!( err.to_string(), "err" );
}
Expand Down
17 changes: 17 additions & 0 deletions module/core/mod_interface_meta/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,23 @@
// pub mod item_struct;
// }

// xxx : check
//
// - does not work
// exposed use
// {
// ::former::Former,
// ::former::Assign,
// };
//
// - work
//
// exposed use ::former::
// {
// Former,
// Assign,
// };

mod impls;
#[ allow( unused_imports ) ]
use impls::exposed::*;
Expand Down
4 changes: 2 additions & 2 deletions module/core/process_tools/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ pub( crate ) mod private
use duct::cmd;
use error_tools::
{
for_app::{ Error, Context, anyhow },
Result,
untyped::{ Error, Context, anyhow },
// Result,
};
use former::Former;
use iter_tools::iter::Itertools;
Expand Down
4 changes: 2 additions & 2 deletions module/move/unitore/src/action/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::path::PathBuf;

use crate::*;
use error_tools::{ for_app::Context, Result };
use error_tools::{ untyped::Context, Result };
use sled_adapter::FeedStorage;
use entity::
{
Expand Down Expand Up @@ -37,7 +37,7 @@ pub async fn config_add( mut storage : FeedStorage< SledStorage >, path : &PathB

if !path.exists()
{
return Err( error_tools::for_app::Error::msg( err_str ) );
return Err( error_tools::untyped::Error::msg( err_str ) );
}

let abs_path = path.canonicalize()?;
Expand Down
2 changes: 1 addition & 1 deletion module/move/unitore/src/feed_config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Reading and parsing of subscription configuration file.

use std::{ fs::OpenOptions, io::{ BufReader, Read } };
use error_tools::{ for_app::Context, Result };
use error_tools::{ untyped::Context, Result };
use serde::Deserialize;

/// Configuration for subscription to feed resource.
Expand Down
Loading

0 comments on commit f29b6d0

Please sign in to comment.