From 9f62a82b0bf277ce06b51821d6692bba998201a5 Mon Sep 17 00:00:00 2001 From: Defelo Date: Tue, 23 Sep 2025 08:50:08 +0200 Subject: [PATCH] radicle: Fix `cob::common::Title::new` The title should be trimmed before checking whether it is empty. Also added a few tests. --- crates/radicle/src/cob/common.rs | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/crates/radicle/src/cob/common.rs b/crates/radicle/src/cob/common.rs index 639b8e40..7bb7289b 100644 --- a/crates/radicle/src/cob/common.rs +++ b/crates/radicle/src/cob/common.rs @@ -43,7 +43,7 @@ impl Deref for Timestamp { } } -#[derive(Error, Debug)] +#[derive(Error, Debug, PartialEq, Eq)] pub enum TitleError { #[error("empty title")] EmptyTitle, @@ -70,11 +70,14 @@ impl Title { /// characters pub fn new(title: &str) -> Result { if title.contains('\n') || title.contains('\r') { - Err(TitleError::InvalidTitle) - } else if title.is_empty() { + return Err(TitleError::InvalidTitle); + } + + let title = title.trim(); + if title.is_empty() { Err(TitleError::EmptyTitle) } else { - Ok(Self(title.trim().to_string())) + Ok(Self(title.into())) } } } @@ -493,6 +496,16 @@ mod test { use super::*; + #[test] + fn test_title() { + assert_eq!(Title::new(""), Err(TitleError::EmptyTitle)); + assert_eq!(Title::new(" "), Err(TitleError::EmptyTitle)); + assert_eq!(Title::new("\t"), Err(TitleError::EmptyTitle)); + assert_eq!(Title::new("foo\nbar"), Err(TitleError::InvalidTitle)); + assert_eq!(Title::new("foobar\n"), Err(TitleError::InvalidTitle)); + assert_eq!(Title::new(" valid title ").unwrap().0, "valid title"); + } + #[test] fn test_color() { let c = Color::from_str("#ffccaa").unwrap();