Rust-taglib provides Rust bindings to the TagLib library. TagLib is a library for reading and editing meta data information of various audio formats. Recently the Rust wrapper supports the operations exposed by the simple C binding interface only.
Example #1: Reading some tags
The following example prints some meta data information from given file
// give an alias to crate name to shorten access
extern crate rust_taglib as taglib;
use std::path::Path;
fn main() {
// construct path to file we wish to gain information from
let file = Path::new("/some/file/reference");
// open file
let meta = taglib::Metadata::new_from_file(file).unwrap();
println!("Info for: {:?}",file.file_name().unwrap());
// print some meta data contents
println!("* Artist:t{}", meta.get_artist().unwrap());
println!("* Album:t{}", meta.get_album().unwrap());
println!("* Tilte:t{}", meta.get_title().unwrap());
println!("* Genre:t{}", meta.get_genre().unwrap());
}
The file does not need to be close explicitly instead all resources will be freed the value goes out of scope. This is achieved by implementing the Drop trait for the Metadata struct:
/// destructor to free resources
impl Drop for Metadata {
fn drop(&mut self) {
unsafe {
if self.fd.is_some() {
// free externally allocated strings
taglib_sys::taglib_tag_free_strings();
// free externally allocated resources
taglib_sys::taglib_file_free(self.fd.unwrap());
}
}
self.fd = None;
}
}
Example #2: Modifing a tag
Next we want to modify the content of some tag and persist it:
extern crate rust_taglib as taglib;
use std::path::Path;
fn main() {
let file = Path::new("/some/file/reference");
match Metadata::new_from_file(file) {
Ok(tag_data) => {
// load and print old title information
let title = tag_data.get_title().unwrap();
println!("Old title is {}", title)
// set title tag to a new value
tag_data.set_title("my changed title");
// finally save the changes back to file
tag_data.save();
}
Err(e) => panic!(e)
};
}
For bug reports or further suggestions file some issue on Bitbucket.