id3_cli/text_data/
comment.rs

1use id3::{frame, Frame};
2use pipe_trait::Pipe;
3use serde::{Deserialize, Serialize};
4
5/// Representation of the comment.
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize)]
7pub struct Comment<Language, Description, Content> {
8    /// Language code (ISO 639-2) of the comment.
9    pub language: Language,
10    /// Description of the comment.
11    pub description: Description,
12    /// Content of the comment.
13    pub content: Content,
14}
15
16impl<Language, Description, Content> Comment<Language, Description, Content> {
17    /// Convert all fields to string slices.
18    pub fn as_str_slices(&self) -> Comment<&'_ str, &'_ str, &'_ str>
19    where
20        Language: AsRef<str>,
21        Description: AsRef<str>,
22        Content: AsRef<str>,
23    {
24        Comment {
25            language: self.language.as_ref(),
26            description: self.description.as_ref(),
27            content: self.content.as_ref(),
28        }
29    }
30
31    /// Convert all fields to owned strings.
32    pub fn to_owned_strings(&self) -> Comment<String, String, String>
33    where
34        Language: ToString,
35        Description: ToString,
36        Content: ToString,
37    {
38        Comment {
39            language: self.language.to_string(),
40            description: self.description.to_string(),
41            content: self.content.to_string(),
42        }
43    }
44}
45
46impl From<frame::Comment> for Comment<String, String, String> {
47    fn from(comment: frame::Comment) -> Self {
48        Comment {
49            language: comment.lang,
50            description: comment.description,
51            content: comment.text,
52        }
53    }
54}
55
56impl<'a> From<&'a frame::Comment> for Comment<&'a str, &'a str, &'a str> {
57    fn from(comment: &'a frame::Comment) -> Self {
58        Comment {
59            language: &comment.lang,
60            description: &comment.description,
61            content: &comment.text,
62        }
63    }
64}
65
66impl<Language, Description, Content> From<Comment<Language, Description, Content>>
67    for frame::Comment
68where
69    Language: Into<String>,
70    Description: Into<String>,
71    Content: Into<String>,
72{
73    fn from(comment: Comment<Language, Description, Content>) -> Self {
74        frame::Comment {
75            lang: comment.language.into(),
76            description: comment.description.into(),
77            text: comment.content.into(),
78        }
79    }
80}
81
82impl<Language, Description, Content> From<Comment<Language, Description, Content>> for Frame
83where
84    Language: Into<String>,
85    Description: Into<String>,
86    Content: Into<String>,
87{
88    fn from(comment: Comment<Language, Description, Content>) -> Self {
89        comment.pipe(frame::Comment::from).into()
90    }
91}