To learn rust!
This is ID3
read and write library.
ID3
informationID3
informationID3
information to version 4other usecases See tests.
This can be used by adding rtag
to your dependencies in your project's Cargo.toml
toml
[dependencies]
rtag = "0.3"
and this to your crate root:
rust
extern crate rtag;
ID3
informationTo read a ID3
metadata, you use a MetadataReader and a Unit enum.
and the MetadataReader
is implementing the std::iter::Iterator trait,
you can use filter, map, fold.. and so on.
```rust // read a frame1 for m in MetadataReader::new("./test-resources/v1-v2.mp3").unwrap() { match m { Unit::FrameV1(frame) => { debug!("v1: {:?}", frame); asserteq!("Artist", frame.artist); asserteq!("!@#$", frame.comment); asserteq!("1", frame.track); asserteq!("137", frame.genre); } _ => (), } }
// filter a frame v3 having compression flag
let mut i = MetadataReader::new(path).unwrap().filter(|m| match m {
&Unit::FrameV2(FrameHeader::V23(ref header), ) => {
header.hasflag(FrameHeaderFlag::Compression)
}
_ => false,
});
// fold a frame v2 let newdata = MetadataReader::new(path) .unwrap() .fold(Vec::new(), |mut vec, unit| { if let Unit::FrameV2(framehead, framebody) = unit { let newframebody = if let FrameBody::TALB(ref frame) = framebody { let mut newframe = frame.clone(); newframe.text = "Album!".tostring(); FrameBody::TALB(newframe) } else { frame_body.clone() };
vec.push(Unit::FrameV2(frame_head, new_frame_body));
} else {
vec.push(unit);
}
vec
});
```
ID3
informationTo write a ID3
metadata, you pass FrameHeader and FrameBody to MetadataWriter via std::vec::Vec.
```rust let newdata = MetadataReader::new(path) .unwrap() .fold(Vec::new(), |mut vec, unit| { if let Unit::FrameV2(framehead, framebody) = unit { let newframebody = ... vec.push(Unit::FrameV2(framehead, newframebody)); }
vec
});
let _ = MetadataWriter::new(path).unwrap().write(new_data, false); ```
ID3
information to version 4To rewrite all the frames to version 4, it is same to above example but second parameter is true
.
Note: the frame v1 information is ignored and some frames that are ignored. - In 2.2 'CRM', 'PIC'. - In 2.3 'EQUA', 'IPLS', 'RVAD', 'TDAT', 'TIME', 'TORY', 'TRDA', 'TSIZ', 'TYER'
rust
// collect frames having version 2
let frames = MetadataReader::new(path).unwrap().collect::<Vec<Unit>>();
// rewrite to version 4
let _ = MetadataWriter::new(path).unwrap().write(frames, true);
// read a version 4
for unit in MetadataReader::new(path).unwrap() {
match unit {
Unit::FrameV2(FrameHeader::V24(head), frame_body) => {
...
},
_ => (),
}
}
To read value of frame without property name, FrameBody support to_map
and inside
.
```rust for unit in MetadataReader::new(path).unwrap() { match unit { Unit::FrameV2(, ref framebody) => {
// 1. using to_map();
let map = frame_body.to_map();
//{
// <key1:&str>: <value1:String>
// ...
//}
// 2. using inside
frame_body.inside(|key, value| {
// key<&str>, value<String>
...
true // if true, look inside next.
})
},
_ => (),
}
} ```