Implement Color::try_from() (#439)

This commit is contained in:
Odin Dutton 2020-05-29 03:08:30 +10:00 committed by GitHub
parent c1e84f7185
commit 5511f85d50
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,4 +1,4 @@
use std::{convert::AsRef, str::FromStr};
use std::{convert::AsRef, convert::TryFrom, result::Result, str::FromStr};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
@ -90,16 +90,11 @@ pub enum Color {
AnsiValue(u8),
}
impl FromStr for Color {
type Err = ();
impl TryFrom<&str> for Color {
type Error = ();
/// Creates a `Color` from the string representation.
///
/// # Notes
///
/// * Returns `Color::White` in case of an unknown color.
/// * Does not return `Err` and you can safely unwrap.
fn from_str(src: &str) -> ::std::result::Result<Self, Self::Err> {
/// Try to create a `Color` from the string representation. This returns an error if the string does not match.
fn try_from(src: &str) -> Result<Self, Self::Error> {
let src = src.to_lowercase();
match src.as_ref() {
@ -119,11 +114,25 @@ impl FromStr for Color {
"dark_cyan" => Ok(Color::DarkCyan),
"white" => Ok(Color::White),
"grey" => Ok(Color::Grey),
_ => Ok(Color::White),
_ => Err(()),
}
}
}
impl FromStr for Color {
type Err = ();
/// Creates a `Color` from the string representation.
///
/// # Notes
///
/// * Returns `Color::White` in case of an unknown color.
/// * Does not return `Err` and you can safely unwrap.
fn from_str(src: &str) -> Result<Self, Self::Err> {
Ok(Color::try_from(src).unwrap_or(Color::White))
}
}
#[cfg(test)]
mod tests {
use super::Color;