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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
use anyhow::anyhow;
use id3::TagLike;

pub fn read_from_tag(tag: &id3::Tag) -> serde_json::Value {
    // There could be many comments, but in my music library, it seems like it's common to just
    // have one with a "description" set to an empty string. So let's have a single "comment" field
    // that reads and writes there.
    let comment = tag.comments().
        find(|c| c.description.is_empty()).
        map(|c| remove_nul_byte(&c.text).to_string());

    if tag.version() == id3::Version::Id3v24 {
        serde_json::json!({
            "version": format!("{}", tag.version()),
            "data": {
                "title": tag.title().map(remove_nul_byte),
                "artist": tag.artist().map(remove_nul_byte),
                "album": tag.album().map(remove_nul_byte),
                "track": tag.track(),
                "date": tag.date_recorded().map(|ts| format!("{}", ts)),
                "genre": tag.genre().map(remove_nul_byte),
                "comment": comment,
            },
        })
    } else {
        serde_json::json!({
            "version": format!("{}", tag.version()),
            "data": {
                "title": tag.title().map(remove_nul_byte),
                "artist": tag.artist().map(remove_nul_byte),
                "album": tag.album().map(remove_nul_byte),
                "track": tag.track(),
                "year": tag.year(),
                "genre": tag.genre().map(remove_nul_byte),
                "comment": comment,
            },
        })
    }
}

pub fn write_to_tag(
    json_map: serde_json::Map<String, serde_json::Value>,
    tag: &mut id3::Tag,
    version: Option<id3::Version>,
) -> anyhow::Result<()> {
    let version = version.unwrap_or_else(|| tag.version());

    for (key, value) in json_map {
        match key.as_str() {
            "title" => {
                if let Some(title) = extract_string("title", &value)? {
                    tag.set_title(title);
                } else {
                    tag.remove_title();
                }
            },
            "artist" => {
                if let Some(artist) = extract_string("artist", &value)? {
                    tag.set_artist(artist);
                } else {
                    tag.remove_artist();
                }
            },
            "album" => {
                if let Some(album) = extract_string("album", &value)? {
                    tag.set_album(album);
                } else {
                    tag.remove_album();
                }
            },
            "track" => {
                if let Some(track) = extract_u32("track", &value)? {
                    tag.set_track(track);
                } else {
                    tag.remove_track();
                }
            },
            "year" if version < id3::Version::Id3v24 => {
                if let Some(year) = extract_u32("year", &value)? {
                    tag.set_year(year.try_into()?);
                } else {
                    tag.remove_year();
                }
            },
            "date" if version >= id3::Version::Id3v24 => {
                if let Some(date) = extract_string("date", &value)? {
                    tag.set_date_recorded(date.parse()?);
                } else {
                    tag.remove_date_recorded();
                }
            },
            "genre" => {
                if let Some(genre) = extract_string("genre", &value)? {
                    tag.set_genre(genre);
                } else {
                    tag.remove_genre();
                }
            },
            "comment" => {
                let mut comment_frames = tag.remove("COMM");
                let existing_index = comment_frames.iter().
                    position(|c| c.content().comment().unwrap().description.is_empty());
                let new_comment_body = extract_string("comment", &value)?;

                match (existing_index, new_comment_body) {
                    (Some(index), None) => {
                        comment_frames.remove(index);
                    },
                    (Some(index), Some(text)) => {
                        let existing_comment = comment_frames[index].content().comment().unwrap();
                        let mut new_comment = existing_comment.clone();
                        new_comment.text = text;

                        let new_frame = id3::Frame::with_content("COMM", id3::Content::Comment(new_comment));
                        comment_frames[index] = new_frame;
                    },
                    (None, Some(text)) => {
                        let new_comment = id3::frame::Comment {
                            lang: String::new(),
                            description: String::new(),
                            text,
                        };
                        let new_frame = id3::Frame::with_content("COMM", id3::Content::Comment(new_comment));

                        comment_frames.push(new_frame);
                    }
                    (None, None) => continue,
                }

                for frame in comment_frames {
                    tag.add_frame(frame);
                }
            },
            _ => (),
        }
    }

    Ok(())
}


fn extract_string(label: &str, json_value: &serde_json::Value) -> anyhow::Result<Option<String>> {
    match json_value {
        serde_json::Value::Null          => Ok(None),
        serde_json::Value::String(value) => Ok(Some(value.clone())),
        _ => Err(anyhow!("Invalid string value for \"{}\": {:?}", label, json_value)),
    }
}

fn extract_u32(label: &str, json_value: &serde_json::Value) -> anyhow::Result<Option<u32>> {
    let invalid_number = || anyhow!("Invalid numeric value for \"{}\": {:?}", label, json_value);

    match json_value {
        serde_json::Value::Null => Ok(None),
        serde_json::Value::String(value) => Ok(Some(value.parse()?)),
        serde_json::Value::Number(number) => {
            let value = number.as_u64().ok_or_else(invalid_number)?.try_into()?;
            Ok(Some(value))
        },
        _ => Err(invalid_number()),
    }
}

fn remove_nul_byte(input: &str) -> &str {
    input.trim_end_matches('\u{0000}')
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_extract_string() {
        let json = serde_json::json!("String!");
        let value = extract_string("_", &json).unwrap();
        assert_eq!(value, Some(String::from("String!")));

        let json = serde_json::json!({ "key": "String!" });
        let value = extract_string("key", &json.get("key").unwrap()).unwrap();
        assert_eq!(value, Some(String::from("String!")));

        let json = serde_json::json!({ "key": None::<String> });
        let value = extract_string("key", &json.get("key").unwrap()).unwrap();
        assert_eq!(value, None);

        let json = serde_json::json!({ "key": 13 });
        assert!(extract_string("key", &json.get("key").unwrap()).is_err());

        let json = serde_json::json!({ "key": ["String!"] });
        assert!(extract_string("key", &json.get("key").unwrap()).is_err());
    }

    #[test]
    fn test_extract_u32() {
        let json = serde_json::json!(42);
        let value = extract_u32("_", &json).unwrap();
        assert_eq!(value, Some(42));

        let json = serde_json::json!(None::<u64>);
        let value = extract_u32("_", &json).unwrap();
        assert_eq!(value, None);

        let json = serde_json::json!({ "key": "13" });
        let value = extract_u32("key", &json.get("key").unwrap()).unwrap();
        assert_eq!(value, Some(13));

        let json = serde_json::json!({ "key": "String!" });
        assert!(extract_u32("key", &json.get("key").unwrap()).is_err());

        let json = serde_json::json!({ "key": ["String!"] });
        assert!(extract_u32("key", &json.get("key").unwrap()).is_err());

        let json = serde_json::json!({ "key": u64::MAX });
        assert!(extract_u32("key", &json.get("key").unwrap()).is_err());
    }
}