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.
Analyzer<WarningsAsErrors>will return the successful result directly, or returnError::UnhandledWarningswhen a warning occurs. This is the default strategy.Analyzer<ReturnWarnings>will return a result wrapped in aMaybeWarnings.Analyzer<IgnoreWarnings>will return the successful result directly, and ignore any warnings that occur. This is not recommended unless you expect warnings to occur and don’t intend to handle them.
§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§
Required Methods§
Sourcefn ok<T>(val: T) -> WarningResult<T, Self>
fn ok<T>(val: T) -> WarningResult<T, Self>
Creates a successful result containing the given value and no warnings.
Sourcefn set_result<T>(result: &mut WarningResult<T, Self>, value: T)
fn set_result<T>(result: &mut WarningResult<T, Self>, value: T)
Updates the successful result, preserving errors and warnings.
Sourcefn add_warning<T>(result: &mut WarningResult<T, Self>, warning: Warning)
fn add_warning<T>(result: &mut WarningResult<T, Self>, warning: Warning)
Adds a new warning to the result.
Sourcefn merge<T, U, V>(
a: WarningResult<T, Self>,
b: WarningResult<U, Self>,
f: impl FnOnce(T, U) -> V,
) -> WarningResult<V, Self>
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".