#![allow(missing_docs)]
use rlg::log_level::{LogLevel, ParseLogLevelError};
use std::convert::TryInto;
use std::str::FromStr;
pub(crate) fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("🦀 RustLogs Log Level Examples 🦀\n");
log_level_display_example();
log_level_parsing_example()?;
log_level_try_into_example()?;
log_level_includes_example();
log_level_case_insensitivity_example()?;
log_level_error_handling_example();
log_level_numeric_conversion_example()?;
println!("\n🎉 All examples completed successfully!");
Ok(())
}
fn log_level_display_example() {
println!("🦀 **Log Level Display Example**");
println!("---------------------------------------------");
let levels = [
LogLevel::ALL,
LogLevel::DEBUG,
LogLevel::INFO,
LogLevel::ERROR,
LogLevel::FATAL,
LogLevel::NONE,
LogLevel::TRACE,
LogLevel::VERBOSE,
LogLevel::WARN,
LogLevel::CRITICAL,
LogLevel::DISABLED,
];
for level in &levels {
println!(" Log level: {}", level);
}
}
fn log_level_parsing_example() -> Result<(), Box<dyn std::error::Error>>
{
println!("\n🦀 **Log Level Parsing Example**");
println!("---------------------------------------------");
let valid_levels = ["DEBUG", "INFO", "ERROR", "WARN", "CRITICAL"];
let invalid_level = "Invalid";
for &level_str in &valid_levels {
let parsed_level = LogLevel::from_str(level_str)?;
println!(
" Parsed log level: {} -> {:?}",
level_str, parsed_level
);
}
match LogLevel::from_str(invalid_level) {
Err(e) => println!(
" ✅ Correctly failed to parse invalid log level: {}",
e
),
_ => {
println!(" ❌ Unexpected success for invalid log level")
}
}
Ok(())
}
fn log_level_try_into_example() -> Result<(), Box<dyn std::error::Error>>
{
println!("\n🦀 **Log Level TryInto Example**");
println!("---------------------------------------------");
let valid_levels = vec![
"ALL".to_string(),
"DEBUG".to_string(),
"INFO".to_string(),
"ERROR".to_string(),
];
for level in valid_levels {
let log_level: LogLevel = level.clone().try_into()?;
println!(
" Successfully converted '{}' to {:?}",
level, log_level
);
}
let invalid_level: Result<LogLevel, _> =
"Invalid".to_string().try_into();
if let Err(e) = invalid_level {
println!(
" ✅ Correctly failed to convert invalid log level: {}",
e
);
}
Ok(())
}
fn log_level_includes_example() {
println!("\n🦀 **Log Level Includes Example**");
println!("---------------------------------------------");
println!(" ALL: {}", LogLevel::ALL.to_numeric());
println!(" ERROR: {}", LogLevel::ERROR.to_numeric());
assert!(
LogLevel::ALL.includes(LogLevel::ERROR),
"ALL should include ERROR"
);
assert!(
LogLevel::ERROR.includes(LogLevel::DEBUG),
"ERROR should include DEBUG"
);
assert!(
!LogLevel::DEBUG.includes(LogLevel::ERROR),
"DEBUG should not include ERROR"
);
assert!(
LogLevel::WARN.includes(LogLevel::DEBUG),
"WARN should include DEBUG"
);
println!(" ✅ Log level includes checks passed.");
}
fn log_level_case_insensitivity_example()
-> Result<(), Box<dyn std::error::Error>> {
println!("\n🦀 **Log Level Case Insensitivity Example**");
println!("---------------------------------------------");
assert_eq!(LogLevel::from_str("debug")?, LogLevel::DEBUG);
assert_eq!(LogLevel::from_str("INFO")?, LogLevel::INFO);
assert_eq!(LogLevel::from_str("Trace")?, LogLevel::TRACE);
println!(" ✅ Case insensitivity checks passed.");
Ok(())
}
fn log_level_error_handling_example() {
println!("\n🦀 **Log Level Error Handling Example**");
println!("---------------------------------------------");
let invalid_level = "Invalid";
match LogLevel::from_str(invalid_level) {
Err(e) => println!(
" ✅ Correctly handled invalid log level: {}",
e
),
_ => {
println!(" ❌ Unexpected success for invalid log level")
}
}
let error = ParseLogLevelError::new(invalid_level);
println!(" ✅ ParseLogLevelError: {}", error);
}
fn log_level_numeric_conversion_example()
-> Result<(), Box<dyn std::error::Error>> {
println!("\n🦀 **Log Level Numeric Conversion Example**");
println!("---------------------------------------------");
assert_eq!(LogLevel::ALL.to_numeric(), 0);
assert_eq!(LogLevel::INFO.to_numeric(), 6);
assert_eq!(LogLevel::ERROR.to_numeric(), 8);
assert_eq!(LogLevel::from_numeric(0), Some(LogLevel::ALL));
assert_eq!(LogLevel::from_numeric(8), Some(LogLevel::ERROR));
assert_eq!(LogLevel::from_numeric(11), None);
println!(" ✅ Log level numeric conversions passed.");
Ok(())
}