err_rs/lib.rs
1/*
2 * Copyright (c) Peter Bjorklund. All rights reserved. https://github.com/piot/err-rs
3 * Licensed under the MIT License. See LICENSE in the project root for license information.
4 */
5#[derive(Copy, Clone, Eq, PartialEq, Debug, Ord, PartialOrd)]
6pub enum ErrorLevel {
7 Info, // Informative, can be ignored
8 Warning, // Should be logged, but recoverable
9 Critical, // Requires immediate attention, unrecoverable
10}
11
12pub trait ErrorLevelProvider {
13 fn error_level(&self) -> ErrorLevel;
14}
15
16/// Returns the most severe error level from a slice of error levels.
17pub fn most_severe_level(levels: &[ErrorLevel]) -> Option<ErrorLevel> {
18 levels.iter().copied().max()
19}
20
21/// Returns the most severe error level from a slice of error level providers.
22pub fn most_severe_error(levels: &[impl ErrorLevelProvider]) -> Option<ErrorLevel> {
23 levels.iter().map(|provider| provider.error_level()).max()
24}
25
26/// Returns the reference to the most severe error level provider from a slice of error level providers.
27pub fn most_severe_error_provider<'a, P>(levels: &[&'a P]) -> Option<&'a P>
28where
29 P: ErrorLevelProvider,
30{
31 levels
32 .iter()
33 .max_by_key(|provider| provider.error_level())
34 .copied() // Get the actual provider reference
35}