Add example usage doc to label enum

This commit is contained in:
Filip Tibell 2024-04-22 13:00:55 +02:00
parent b04c0c209d
commit 896121ab0d
No known key found for this signature in database

View file

@ -2,6 +2,24 @@ use std::fmt;
use console::{style, Color}; use console::{style, Color};
/**
Label enum used for consistent output formatting throughout Lune.
# Example usage
```rs
use lune_utils::fmt::Label;
println!("{} This is an info message", Label::Info);
// [INFO] This is an info message
println!("{} This is a warning message", Label::Warn);
// [WARN] This is a warning message
println!("{} This is an error message", Label::Error);
// [ERROR] This is an error message
```
*/
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub enum Label { pub enum Label {
Info, Info,
@ -11,14 +29,14 @@ pub enum Label {
impl Label { impl Label {
/** /**
Returns the name of the label in lowercase. Returns the name of the label in all uppercase.
*/ */
#[must_use] #[must_use]
pub fn name(&self) -> &str { pub fn name(&self) -> &str {
match self { match self {
Self::Info => "info", Self::Info => "INFO",
Self::Warn => "warn", Self::Warn => "WARN",
Self::Error => "error", Self::Error => "ERROR",
} }
} }
@ -41,7 +59,7 @@ impl fmt::Display for Label {
f, f,
"{}{}{}", "{}{}{}",
style("[").dim(), style("[").dim(),
style(self.name().to_ascii_uppercase()).fg(self.color()), style(self.name()).fg(self.color()),
style("]").dim() style("]").dim()
) )
} }