parsers: Add get_best_{body,subject}_owned methods for Message
This commit is contained in:
parent
50425617b5
commit
6e3f31ef46
2 changed files with 37 additions and 0 deletions
|
|
@ -51,6 +51,8 @@ XXXX-YY-ZZ RELEASER <admin@example.com>
|
|||
the size of these elements (!472).
|
||||
- Add Message::extract_valid_payload method to warn in case of failure
|
||||
and return simply Option<T> instead of Result<Option<T>> (!497)
|
||||
- Add Message::get_best_body_owned and Message::get_best_subject_owned
|
||||
for best developer experience when performance is not an issue (!497)
|
||||
|
||||
Version 0.21.0:
|
||||
2024-07-25 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
|
||||
|
|
|
|||
|
|
@ -183,6 +183,17 @@ impl Message {
|
|||
map.iter().map(|(lang, value)| (lang.clone(), value)).next()
|
||||
}
|
||||
|
||||
fn get_best_owned<T: ToOwned<Owned = T>>(
|
||||
map: &BTreeMap<Lang, T>,
|
||||
preferred_langs: Vec<&str>,
|
||||
) -> Option<(Lang, T)> {
|
||||
if let Some((lang, item)) = Self::get_best::<T>(map, preferred_langs) {
|
||||
Some((lang, item.to_owned()))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the best matching body from a list of languages.
|
||||
///
|
||||
/// For instance, if a message contains both an xml:lang='de', an xml:lang='fr' and an English
|
||||
|
|
@ -194,6 +205,11 @@ impl Message {
|
|||
Message::get_best::<Body>(&self.bodies, preferred_langs)
|
||||
}
|
||||
|
||||
/// Owned variant of [`Message::get_best_body`]
|
||||
pub fn get_best_body_owned(&self, preferred_langs: Vec<&str>) -> Option<(Lang, Body)> {
|
||||
Message::get_best_owned::<Body>(&self.bodies, preferred_langs)
|
||||
}
|
||||
|
||||
/// Returns the best matching subject from a list of languages.
|
||||
///
|
||||
/// For instance, if a message contains both an xml:lang='de', an xml:lang='fr' and an English
|
||||
|
|
@ -205,6 +221,11 @@ impl Message {
|
|||
Message::get_best::<Subject>(&self.subjects, preferred_langs)
|
||||
}
|
||||
|
||||
/// Owned variant of [`Message::get_best_subject`]
|
||||
pub fn get_best_subject_owned(&self, preferred_langs: Vec<&str>) -> Option<(Lang, Subject)> {
|
||||
Message::get_best_owned::<Subject>(&self.subjects, preferred_langs)
|
||||
}
|
||||
|
||||
/// Try to extract the given payload type from the message's payloads.
|
||||
///
|
||||
/// Returns the first matching payload element as parsed struct or its
|
||||
|
|
@ -478,6 +499,13 @@ mod tests {
|
|||
assert_eq!(subject, &Subject::from_str("Hello world!").unwrap());
|
||||
}
|
||||
|
||||
// Test owned variant.
|
||||
{
|
||||
let (lang, subject) = message.get_best_subject_owned(vec!["en"]).unwrap();
|
||||
assert_eq!(lang, "");
|
||||
assert_eq!(subject, Subject::from_str("Hello world!").unwrap());
|
||||
}
|
||||
|
||||
let elem2 = message.into();
|
||||
assert_eq!(elem1, elem2);
|
||||
}
|
||||
|
|
@ -518,6 +546,13 @@ mod tests {
|
|||
assert_eq!(body, &Body::from_str("Hello world!").unwrap());
|
||||
}
|
||||
|
||||
// Test owned variant.
|
||||
{
|
||||
let (lang, body) = message.get_best_body_owned(vec!["ja"]).unwrap();
|
||||
assert_eq!(lang, "");
|
||||
assert_eq!(body, Body::from_str("Hello world!").unwrap());
|
||||
}
|
||||
|
||||
let message = Message::new(None);
|
||||
|
||||
// Tests without a body.
|
||||
|
|
|
|||
Loading…
Reference in a new issue