use crate::domain::error::{Error, Result};
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct MenuItem {
pub name: String,
pub description: String,
pub key: Option<char>,
}
impl MenuItem {
#[must_use]
pub const fn with_key(mut self, key: char) -> Self {
self.key = Some(key);
self
}
#[must_use]
pub const fn name(&self) -> &str {
self.name.as_str()
}
#[must_use]
pub const fn description(&self) -> &str {
self.description.as_str()
}
}
impl TryFrom<(&str, &str)> for MenuItem {
type Error = Error;
fn try_from((name, description): (&str, &str)) -> Result<Self> {
Self::parse_str(name, description)
}
}
impl TryFrom<(String, String)> for MenuItem {
type Error = Error;
fn try_from((name, description): (String, String)) -> Result<Self> {
Self::parse_str(&name, &description)
}
}
impl MenuItem {
fn parse_str(name: &str, description: &str) -> Result<Self> {
if name.is_empty() {
return Err(Error::EmptyName);
}
if name.contains('\n') {
return Err(Error::NameHasNewline(name.to_owned()));
}
Ok(Self {
name: name.to_owned(),
description: description.to_owned(),
key: None,
})
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct AnnotatedItem {
pub name: String,
pub description: String,
pub annotation: Option<String>,
pub key: Option<char>,
}
impl AnnotatedItem {
#[must_use]
pub const fn with_key(mut self, key: char) -> Self {
self.key = Some(key);
self
}
}
impl TryFrom<(&str, &str, Option<&str>)> for AnnotatedItem {
type Error = Error;
fn try_from((name, description, annotation): (&str, &str, Option<&str>)) -> Result<Self> {
if name.is_empty() {
return Err(Error::EmptyName);
}
if name.contains('\n') {
return Err(Error::NameHasNewline(name.to_owned()));
}
let annotation = annotation
.map(str::to_owned)
.map(|s| {
if s.contains('\n') {
Err(Error::AnnotationHasNewline(s))
} else {
Ok(s)
}
})
.transpose()?;
Ok(Self {
name: name.to_owned(),
description: description.to_owned(),
annotation,
key: None,
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parses_valid_pair() -> Result<()> {
let item = MenuItem::try_from(("/clear", "Clear conversation history"))?;
assert_eq!(item.name, "/clear");
assert_eq!(item.description, "Clear conversation history");
assert_eq!(item.key, None);
Ok(())
}
#[test]
fn rejects_empty_name() {
let result = MenuItem::try_from(("", "x"));
assert!(matches!(result, Err(Error::EmptyName)));
}
#[test]
fn rejects_newline_in_name() {
let result = MenuItem::try_from(("/bad\nname", "x"));
assert!(matches!(result, Err(Error::NameHasNewline(_))));
}
#[test]
fn with_key_attaches_hotkey() -> Result<()> {
let item = MenuItem::try_from(("yes", "approve"))?.with_key('y');
assert_eq!(item.key, Some('y'));
Ok(())
}
#[test]
fn annotated_accepts_no_annotation() -> Result<()> {
let item = AnnotatedItem::try_from(("name", "desc", None))?;
assert_eq!(item.annotation, None);
Ok(())
}
#[test]
fn annotated_rejects_newline_in_annotation() {
let result = AnnotatedItem::try_from(("name", "desc", Some("bad\nannotation")));
assert!(matches!(result, Err(Error::AnnotationHasNewline(_))));
}
}