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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#![crate_name = "taglib"]
#![crate_type = "lib"]
extern crate libc;
extern crate taglib_sys as sys;
use libc::c_char;
use std::ffi::{CString, CStr};
use std::path::Path;
use sys as ll;
fn c_str_to_str(c_str: *const c_char) -> Option<String> {
if c_str.is_null() {
None
} else {
let bytes = unsafe { CStr::from_ptr(c_str).to_bytes() };
if bytes.is_empty() { None } else { Some(String::from_utf8_lossy(bytes).to_string()) }
}
}
fn u32_to_option(n: u32) -> Option<u32> {
if n == 0 { None } else { Some(n) }
}
pub struct File {
raw: *mut ll::TagLib_File,
}
#[allow(dead_code)]
pub struct Tag<'a> {
raw: *mut ll::TagLib_Tag,
file: &'a File,
}
#[allow(dead_code)]
pub struct AudioProperties<'a> {
raw: *const ll::TagLib_AudioProperties,
file: &'a File,
}
impl<'a> Tag<'a> {
pub fn title(&self) -> Option<String> {
let res = unsafe { ll::taglib_tag_title(self.raw) };
c_str_to_str(res)
}
pub fn set_title(&mut self, title: &str) {
let cs = CString::new(title).unwrap();
let s = cs.as_ptr();
unsafe {
ll::taglib_tag_set_title(self.raw, s);
}
}
pub fn artist(&self) -> Option<String> {
let res = unsafe { ll::taglib_tag_artist(self.raw) };
c_str_to_str(res)
}
pub fn set_artist(&mut self, artist: &str) {
let cs = CString::new(artist).unwrap();
let s = cs.as_ptr();
unsafe {
ll::taglib_tag_set_artist(self.raw, s);
}
}
pub fn album(&self) -> Option<String> {
let res = unsafe { ll::taglib_tag_album(self.raw) };
c_str_to_str(res)
}
pub fn set_album(&mut self, album: &str) {
let cs = CString::new(album).unwrap();
let s = cs.as_ptr();
unsafe {
ll::taglib_tag_set_album(self.raw, s);
}
}
pub fn comment(&self) -> Option<String> {
let res = unsafe { ll::taglib_tag_comment(self.raw) };
c_str_to_str(res)
}
pub fn set_comment(&mut self, comment: &str) {
let cs = CString::new(comment).unwrap();
let s = cs.as_ptr();
unsafe {
ll::taglib_tag_set_comment(self.raw, s);
}
}
pub fn genre(&self) -> Option<String> {
let res = unsafe { ll::taglib_tag_genre(self.raw) };
c_str_to_str(res)
}
pub fn set_genre(&mut self, genre: &str) {
let cs = CString::new(genre).unwrap();
let s = cs.as_ptr();
unsafe {
ll::taglib_tag_set_genre(self.raw, s);
}
}
pub fn year(&self) -> Option<u32> {
u32_to_option(unsafe { ll::taglib_tag_year(self.raw) as u32 })
}
pub fn set_year(&mut self, year: u32) {
unsafe {
ll::taglib_tag_set_year(self.raw, year);
}
}
pub fn track(&self) -> Option<u32> {
u32_to_option(unsafe { ll::taglib_tag_track(self.raw) as u32 })
}
pub fn set_track(&mut self, track: u32) {
unsafe {
ll::taglib_tag_set_track(self.raw, track);
}
}
}
impl<'a> AudioProperties<'a> {
pub fn length(&self) -> u32 {
unsafe { ll::taglib_audioproperties_length(self.raw) as u32 }
}
pub fn bitrate(&self) -> u32 {
unsafe { ll::taglib_audioproperties_bitrate(self.raw) as u32 }
}
pub fn samplerate(&self) -> u32 {
unsafe { ll::taglib_audioproperties_samplerate(self.raw) as u32 }
}
pub fn channels(&self) -> u32 {
unsafe { ll::taglib_audioproperties_channels(self.raw) as u32 }
}
}
#[derive(Copy, Clone, PartialEq)]
pub enum FileType {
MPEG = ll::TAGLIB_FILE_MPEG as isize,
OggVorbis = ll::TAGLIB_FILE_OGG_VORBIS as isize,
FLAC = ll::TAGLIB_FILE_FLAC as isize,
MPC = ll::TAGLIB_FILE_MPC as isize,
OggFlac = ll::TAGLIB_FILE_OGG_FLAC as isize,
WavPack = ll::TAGLIB_FILE_WAV_PACK as isize,
Speex = ll::TAGLIB_FILE_SPEEX as isize,
TrueAudio = ll::TAGLIB_FILE_TRUE_AUDIO as isize,
MP4 = ll::TAGLIB_FILE_MP4 as isize,
ASF = ll::TAGLIB_FILE_ASF as isize,
}
#[derive(Debug)]
pub enum FileError {
InvalidFile,
InvalidFileName,
NoAvailableTag,
NoAvailableAudioProperties,
}
impl Drop for File {
fn drop(&mut self) {
unsafe {
ll::taglib_file_free(self.raw);
}
}
}
impl File {
pub fn new<P: AsRef<Path>>(path: P) -> Result<File, FileError> {
let filename = path.as_ref().to_str().ok_or(FileError::InvalidFileName)?;
let filename_c = CString::new(filename).ok().ok_or(FileError::InvalidFileName)?;
let filename_c_ptr = filename_c.as_ptr();
let f = unsafe { ll::taglib_file_new(filename_c_ptr) };
if f.is_null() {
return Err(FileError::InvalidFile);
}
Ok(File { raw: f })
}
pub fn new_type(filename: &str, filetype: FileType) -> Result<File, FileError> {
let filename_c = match CString::new(filename) {
Ok(s) => s,
_ => return Err(FileError::InvalidFileName),
};
let filename_c_ptr = filename_c.as_ptr();
let f = unsafe { ll::taglib_file_new_type(filename_c_ptr, filetype as u32) };
if f.is_null() {
return Err(FileError::InvalidFile);
}
Ok(File { raw: f })
}
pub fn tag(&self) -> Result<Tag, FileError> {
let res = unsafe { ll::taglib_file_tag(self.raw) };
if res.is_null() {
Err(FileError::NoAvailableTag)
} else {
Ok(Tag {
raw: res,
file: self,
})
}
}
pub fn is_valid(&self) -> bool {
unsafe { ll::taglib_file_is_valid(self.raw) != 0 }
}
pub fn audioproperties(&self) -> Result<AudioProperties, FileError> {
let res = unsafe { ll::taglib_file_audioproperties(self.raw) };
if res.is_null() {
Err(FileError::NoAvailableAudioProperties)
} else {
Ok(AudioProperties {
raw: res,
file: self,
})
}
}
pub fn save(&self) -> bool {
unsafe { ll::taglib_file_save(self.raw) != 0 }
}
}
#[cfg(test)]
mod test {
const TEST_MP3: &'static str = "fixtures/test.mp3";
use super::*;
use std::fs;
use std::path::PathBuf;
#[test]
fn test_get_tag() {
let file = File::new(TEST_MP3).unwrap();
let tag = file.tag().unwrap();
assert_eq!(tag.artist().unwrap(), "Artist");
}
#[test]
fn test_get_pathbuf() {
let file = File::new(PathBuf::from(TEST_MP3)).unwrap();
let tag = file.tag().unwrap();
assert_eq!(tag.artist().unwrap(), "Artist");
}
#[test]
fn test_get_no_tag() {
let file = File::new(TEST_MP3).unwrap();
let tag = file.tag().unwrap();
assert_eq!(tag.album(), None);
}
#[test]
fn test_get_tag_new_type() {
let file = File::new_type(TEST_MP3, FileType::MPEG).unwrap();
let tag = file.tag().unwrap();
assert_eq!(tag.artist().unwrap(), "Artist");
}
#[test]
fn test_get_audioproperties() {
let file = File::new(TEST_MP3).unwrap();
let ap = file.audioproperties().unwrap();
assert_eq!(ap.length(), 0);
}
#[test]
fn test_set_tag() {
let temp_fn = "fixtures/temp.mp3";
fs::copy(TEST_MP3, temp_fn).unwrap();
let file = File::new(temp_fn).unwrap();
let mut tag = file.tag().unwrap();
tag.set_artist("Not Artist");
assert_eq!(tag.artist().unwrap(), "Not Artist");
file.save();
let file = File::new(temp_fn).unwrap();
let tag = file.tag().unwrap();
assert_eq!(tag.artist().unwrap(), "Not Artist");
fs::remove_file(temp_fn).unwrap();
}
}