use std::borrow::Cow;
use std::fmt;
use super::price::Price;
#[derive(Clone, Debug, PartialEq)]
pub enum Label {
Empty,
Cosmetic(String),
ExactPrice(Price),
NegotiablePrice(Price),
Unknown(String, String),
}
impl Default for Label {
fn default() -> Self {
Label::Empty
}
}
impl Label {
#[inline]
pub fn is_empty(&self) -> bool {
match *self { Label::Empty => true, _ => false }
}
pub fn price(&self) -> Option<&Price> {
match *self {
Label::ExactPrice(ref p) => Some(p),
Label::NegotiablePrice(ref p) => Some(p),
_ => None,
}
}
#[inline]
pub fn exact_price(&self) -> Option<&Price> {
match *self {
Label::ExactPrice(ref p) => Some(p),
_ => None,
}
}
#[inline]
pub fn negotiable_price(&self) -> Option<&Price> {
match *self {
Label::NegotiablePrice(ref p) => Some(p),
_ => None,
}
}
pub fn tag(&self) -> Option<&str> {
match *self {
Label::ExactPrice(_) => Some("price"),
Label::NegotiablePrice(_) => Some("b/o"),
Label::Unknown(ref t, _) => Some(t),
_ => None,
}
}
pub fn value(&self) -> Option<Cow<str>> {
match *self {
Label::ExactPrice(ref p) |
Label::NegotiablePrice(ref p) => Some(format!("{}", p).into()),
Label::Unknown(_, ref v) => Some(v.as_str().into()),
_ => None,
}
}
pub fn note(&self) -> Option<&str> {
match *self {
Label::Empty => Some(""),
Label::Cosmetic(ref s) => Some(s.as_str()),
_ => None,
}
}
}
impl fmt::Display for Label {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match *self {
Label::Empty => write!(fmt, ""),
Label::Cosmetic(ref s) => write!(fmt, "{}", s),
Label::ExactPrice(ref p) => write!(fmt, "~price {}", p),
Label::NegotiablePrice(ref p) => write!(fmt, "~b/o {}", p),
Label::Unknown(ref t, ref v) => write!(fmt, "~{} {}", t, v),
}
}
}