to_mb/to_mb.rs
1// cue_sheet
2// Copyright (C) 2017 Leonardo Schwarz <mail@leoschwarz.com>
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17//! Convert a cue sheet into a tracklist that can be parsed by the MusicBrainz tracklist parser for
18//! easy importing of metadata.
19//!
20//! Note there is one caveat that by only dealing with the data from the cuefile and not the actual
21//! source files, this currently results in the last track of the list having an unknown length.
22//! This could be fixed (TODO) in the future by providing an option in the Tracklist parser, to
23//! also query the specified file lengths, but of course this won't always be applicable.
24
25extern crate cue_sheet;
26
27use cue_sheet::errors::Error;
28use cue_sheet::tracklist::Tracklist;
29
30use std::env;
31use std::fs::File;
32use std::io::Read;
33
34fn perform_conversion(source: &str) -> Result<(), Error> {
35 let mut tracklist = Tracklist::parse(source)?;
36 // TODO support multi-cds
37 assert_eq!(tracklist.files.len(), 1);
38
39 let file = tracklist.files.remove(0);
40 for ref t in file.tracks {
41 let duration = match t.duration.clone() {
42 Some(time) => time.to_string_2(),
43 None => "??:??".to_string(),
44 };
45 println!(
46 "{:02} {} - {} {}",
47 t.number,
48 t.title.as_ref().unwrap(),
49 t.performer
50 .clone()
51 .ok_or_else(|| Error::from("Not all tracks have a specified performer."))?,
52 duration
53 );
54 }
55
56 Ok(())
57}
58
59fn main() {
60 if let Some(path) = env::args().nth(1) {
61 // Try reading the file provided by the path.
62 let mut file = File::open(path).expect("Failed reading file.");
63 let mut content = String::new();
64 file.read_to_string(&mut content).unwrap();
65
66 perform_conversion(content.as_str()).expect("Conversion failed.");
67 } else {
68 println!(
69 "provide a path to a .cue file to be converted into a MusicBrainz compatible tracklist."
70 )
71 }
72}