1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
use std::{
borrow::Cow,
fmt,
fmt::{Display, Formatter},
str::Chars,
};
use crate::{body::Body, fragment::Fragment};
/// The [`Subject`] from the [`CommitMessage`]
#[derive(Debug, PartialEq, Eq, Clone, Default)]
pub struct Subject<'a> {
text: Cow<'a, str>,
}
impl<'a> Subject<'a> {
/// Count characters in [`Subject`]
///
/// # Examples
///
/// ```
/// use mit_commit::Subject;
///
/// assert_eq!(Subject::from("hello, world!").len(), 13);
/// assert_eq!(Subject::from("goodbye").len(), 7)
/// ```
#[must_use]
pub fn len(&self) -> usize {
self.text.len()
}
/// Is the [`Subject`] empty
///
/// # Examples
///
/// ```
/// use mit_commit::Subject;
///
/// assert_eq!(Subject::from("hello, world!").is_empty(), false);
/// assert_eq!(Subject::from("").is_empty(), true)
/// ```
#[must_use]
pub fn is_empty(&self) -> bool {
self.text.is_empty()
}
/// Convert the [`Subject`] into chars
///
/// # Examples
///
/// ```
/// use mit_commit::Subject;
///
/// let subject = Subject::from("y\u{306}");
///
/// let mut chars = subject.chars();
///
/// assert_eq!(Some('y'), chars.next());
/// assert_eq!(Some('\u{0306}'), chars.next());
///
/// assert_eq!(None, chars.next());
/// ```
pub fn chars(&self) -> Chars<'_> {
self.text.chars()
}
}
impl<'a> From<&'a str> for Subject<'a> {
fn from(subject: &'a str) -> Self {
Self {
text: subject.into(),
}
}
}
impl<'a> From<String> for Subject<'a> {
fn from(subject: String) -> Self {
Self {
text: subject.into(),
}
}
}
impl<'a> From<Cow<'a, str>> for Subject<'a> {
fn from(subject: Cow<'a, str>) -> Self {
Self { text: subject }
}
}
impl<'a> From<Subject<'a>> for String {
fn from(subject: Subject<'_>) -> Self {
subject.text.into_owned()
}
}
impl<'a> Display for Subject<'a> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}", String::from(self.clone()))
}
}
impl<'a> From<Body<'a>> for Subject<'a> {
fn from(body: Body<'_>) -> Self {
Self::from(String::from(body))
}
}
impl<'a> From<Vec<Fragment<'a>>> for Subject<'a> {
fn from(ast: Vec<Fragment<'a>>) -> Self {
ast.iter()
.find_map(|values| {
if let Fragment::Body(body) = values {
Some(Self::from(body.clone()))
} else {
None
}
})
.unwrap_or_default()
}
}