use super::super::range::{Position, Range};
use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum LabelForm {
Canonical,
Stripped,
Shortcut,
Community,
}
impl Default for LabelForm {
fn default() -> Self {
Self::Canonical
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Label {
pub value: String,
pub location: Range,
pub form: LabelForm,
}
impl Label {
fn default_location() -> Range {
Range::new(0..0, Position::new(0, 0), Position::new(0, 0))
}
pub fn new(value: String) -> Self {
Self {
value,
location: Self::default_location(),
form: LabelForm::Canonical,
}
}
pub fn from_string(value: &str) -> Self {
Self {
value: value.to_string(),
location: Self::default_location(),
form: LabelForm::Canonical,
}
}
pub fn at(mut self, location: Range) -> Self {
self.location = location;
self
}
pub fn with_form(mut self, form: LabelForm) -> Self {
self.form = form;
self
}
}
impl fmt::Display for Label {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.value)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_label() {
let location = super::super::super::range::Range::new(
0..0,
super::super::super::range::Position::new(1, 0),
super::super::super::range::Position::new(1, 10),
);
let label = Label::new("test".to_string()).at(location.clone());
assert_eq!(label.location, location);
}
#[test]
fn label_defaults_form_to_canonical() {
assert_eq!(Label::new("x".into()).form, LabelForm::Canonical);
assert_eq!(Label::from_string("y").form, LabelForm::Canonical);
}
#[test]
fn with_form_tags_label() {
let l = Label::from_string("author").with_form(LabelForm::Shortcut);
assert_eq!(l.form, LabelForm::Shortcut);
assert_eq!(l.value, "author");
}
#[test]
fn label_form_default_is_canonical() {
assert_eq!(LabelForm::default(), LabelForm::Canonical);
}
}