1use serde::Serialize;
4use std::{ffi::OsString, fmt::Display};
5
6#[derive(Debug, Clone, PartialEq, Eq, Serialize, Hash)]
9pub struct Title(pub String);
10
11impl Title {
12 pub fn as_os_string(&self) -> OsString {
14 OsString::from(self.0.replace(' ', "_"))
15 }
16}
17
18impl Display for Title {
19 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
20 f.write_str(&self.0)
21 }
22}
23
24impl From<OsString> for Title {
25 fn from(s: OsString) -> Self {
26 Self(s.to_string_lossy().replace('_', " "))
27 }
28}
29
30impl From<&str> for Title {
31 fn from(s: &str) -> Self {
32 Self(s.to_string())
33 }
34}
35
36impl AsRef<str> for Title {
37 #[inline(always)]
38 fn as_ref(&self) -> &str {
39 self.0.as_str()
40 }
41}
42
43#[cfg(test)]
44mod test_super {
45 use super::*;
46
47 #[test]
48 fn test_from_str_with_spaces() {
49 assert_eq!(OsString::from("foo_bar"), Title::from("foo bar").as_os_string());
50 }
51
52 #[test]
53 fn test_from_str_with_emojis() {
54 assert_eq!(OsString::from("foo_bar_😀"), Title::from("foo bar 😀").as_os_string());
55 }
56
57 #[test]
58 fn test_from_os_string() {
59 assert_eq!(
60 "Original title".to_string(),
61 Title::from(OsString::from("Original_title")).to_string()
62 );
63 }
64}