summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 3939e179ab089734062504c191ce6d5278fb2640 (plain)
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
use std::io;
use std::fs::File;
use argparse::{ArgumentParser, StoreTrue, Store};
use crate::options::{EncodingOptions, Options};

mod encoding;
mod error;
mod id3v2;
mod options;
mod util;

fn main() -> io::Result<()> {
    let options = {
        let mut options: Options = Default::default();

        {
            let mut ap = ArgumentParser::new();
            ap.set_description("ID3v2 tag editor/fixer. Incomplete/work-in-progress.");

            ap.refer(&mut options.write)
                .add_option(&["-w", "--write"], StoreTrue,
                            "Write updated information instead of just displaying. Unmodified or unknown tags are preserved as-is.");

            ap.refer(&mut options.latin1_as_utf8)
                .add_option(&["--assume-utf8"], StoreTrue,
                            "Assume that all strings specified in the MP3 as Latin-1 are really UTF-8.");

            ap.refer(&mut options.set_tags.album) .add_option(&["-A", "--album"],  Store, "Set/replace album");
            ap.refer(&mut options.set_tags.artist).add_option(&["-a", "--artist"], Store, "Set/replace artist");
            ap.refer(&mut options.set_tags.title) .add_option(&["-t", "--title"],  Store, "Set/replace title");
            ap.refer(&mut options.set_tags.track) .add_option(&["-T", "--track"],  Store, "Set/replace track number (num or num/num)");
            ap.refer(&mut options.set_tags.year)  .add_option(&["-y", "--year"],   Store, "Set/replace year");

            ap.refer(&mut options.file)
                .required()
                .add_argument("file", Store,
                              "File to operate on (probably a .mp3)");

            ap.parse_args_or_exit();
        }

        options
    };

    let mut f = File::open(options.file)?;
    let tag = id3v2::ID3v2::from_stream(&mut f)?;
    // println!("{:?}", tag);

    // TODO: apply changes from options here

    for frame in tag.frames {
        match frame.interpret(&EncodingOptions { latin1_as_utf8: options.latin1_as_utf8 })? {
            Some(id3v2::Frame::TALB(album))  => println!("Album : '{}'", album),
            Some(id3v2::Frame::TIT2(title))  => println!("Title : '{}'", title),
            Some(id3v2::Frame::TPE1(artist)) => println!("Artist: '{}'", artist),
            Some(id3v2::Frame::TYER(year))   => println!("Year  : '{}'", year),
            Some(id3v2::Frame::TRCK(track))  => println!("Track : '{}'", track),
            None => {
                println!("Unknown frame: {:?}", frame);
            }
        }
    }

    // TODO: if -w, then write tags to file (if it fits)

    Ok(())
}