Skip to main content

WarningHandling

Trait WarningHandling 

Source
pub trait WarningHandling {
    type OkType<T>;

    // Required methods
    fn ok<T>(val: T) -> WarningResult<T, Self>;
    fn set_result<T>(result: &mut WarningResult<T, Self>, value: T);
    fn add_warning<T>(result: &mut WarningResult<T, Self>, warning: Warning);
    fn merge<T, U, V>(
        a: WarningResult<T, Self>,
        b: WarningResult<U, Self>,
        f: impl FnOnce(T, U) -> V,
    ) -> WarningResult<V, Self>;
}
Expand description

Specifies how warnings from the analysis engine should be handled.

§Warning Handling

Analyzer can handle analysis engine warnings in several ways. The Ok result type of methods that can produce warnings will vary depending on which strategy is chosen.

§Examples

§WarningsAsErrors

async fn example(
    analyzer: &mut Analyzer,
    request: AnalysisRequest
) -> Result<()> {
    let result: AnalysisResult = analyzer
        .analyze(request)
        .await? // Warnings will cause this to fail with an error
        .unwrap();
    println!("{:.1}%", result.root_info.winrate * 100.0);
    Ok(())
}

§ReturnWarnings

async fn example(
    analyzer: &mut Analyzer<ReturnWarnings>,
    request: AnalysisRequest,
) -> Result<()> {
    let result: AnalysisResult = analyzer
        .analyze(request)
        .await?
        .inspect_warnings(|warnings| {
            for warning in warnings {
               println!("{warning:?}");
            }
        })
        .value
        .unwrap();
    println!("{:.1}%", result.root_info.winrate * 100.0);
    Ok(())
}

Warnings can also be converted back to errors:

async fn example(
    analyzer: &mut Analyzer<ReturnWarnings>,
    request: AnalysisRequest,
) -> Result<()> {
    let result: AnalysisResult = analyzer
        .analyze(request)
        .await?
        .into_result()? // Warnings will cause this to fail with an error
        .unwrap();
    println!("{:.1}%", result.root_info.winrate * 100.0);
    Ok(())
}

§IgnoreWarnings

async fn example(
    analyzer: &mut Analyzer<IgnoreWarnings>,
    request: AnalysisRequest
) -> Result<()> {
    let result: AnalysisResult = analyzer
        .analyze(request)
        .await? // Warnings will be ignored
        .unwrap();
    println!("{:.1}%", result.root_info.winrate * 100.0);
    Ok(())
}

Required Associated Types§

Source

type OkType<T>

The Ok result type.

Required Methods§

Source

fn ok<T>(val: T) -> WarningResult<T, Self>

Creates a successful result containing the given value and no warnings.

Source

fn set_result<T>(result: &mut WarningResult<T, Self>, value: T)

Updates the successful result, preserving errors and warnings.

Source

fn add_warning<T>(result: &mut WarningResult<T, Self>, warning: Warning)

Adds a new warning to the result.

Source

fn merge<T, U, V>( a: WarningResult<T, Self>, b: WarningResult<U, Self>, f: impl FnOnce(T, U) -> V, ) -> WarningResult<V, Self>

Applies a function to the successful values of two results, merging warnings and errors.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§