use strum::{Display, EnumString};
use crate::{layout::Alignment, text::Line};
#[derive(Debug, Default, Clone, Eq, PartialEq, Hash)]
pub struct Title<'a> {
pub content: Line<'a>,
pub alignment: Option<Alignment>,
pub position: Option<Position>,
}
#[derive(Debug, Default, Display, EnumString, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Position {
#[default]
Top,
Bottom,
}
impl<'a> Title<'a> {
#[must_use = "method moves the value of self and returns the modified value"]
pub fn content<T>(mut self, content: T) -> Self
where
T: Into<Line<'a>>,
{
self.content = content.into();
self
}
#[must_use = "method moves the value of self and returns the modified value"]
pub const fn alignment(mut self, alignment: Alignment) -> Self {
self.alignment = Some(alignment);
self
}
#[must_use = "method moves the value of self and returns the modified value"]
pub const fn position(mut self, position: Position) -> Self {
self.position = Some(position);
self
}
}
impl<'a, T> From<T> for Title<'a>
where
T: Into<Line<'a>>,
{
fn from(value: T) -> Self {
let content = value.into();
let alignment = content.alignment;
Self {
content,
alignment,
position: None,
}
}
}
#[cfg(test)]
mod tests {
use rstest::rstest;
use strum::ParseError;
use super::*;
#[test]
fn position_to_string() {
assert_eq!(Position::Top.to_string(), "Top");
assert_eq!(Position::Bottom.to_string(), "Bottom");
}
#[test]
fn position_from_str() {
assert_eq!("Top".parse::<Position>(), Ok(Position::Top));
assert_eq!("Bottom".parse::<Position>(), Ok(Position::Bottom));
assert_eq!("".parse::<Position>(), Err(ParseError::VariantNotFound));
}
#[test]
fn title_from_line() {
let title = Title::from(Line::raw("Title"));
assert_eq!(title.content, Line::from("Title"));
assert_eq!(title.alignment, None);
assert_eq!(title.position, None);
}
#[rstest]
#[case::left(Alignment::Left)]
#[case::center(Alignment::Center)]
#[case::right(Alignment::Right)]
fn title_from_line_with_alignment(#[case] alignment: Alignment) {
let line = Line::raw("Title").alignment(alignment);
let title = Title::from(line.clone());
assert_eq!(title.content, line);
assert_eq!(title.alignment, Some(alignment));
assert_eq!(title.position, None);
}
}