forked from Serial-ATA/lofty-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tag_writer.rs
82 lines (63 loc) · 1.59 KB
/
tag_writer.rs
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
use lofty::{Accessor, Probe, Tag, TagExt, TaggedFileExt};
use structopt::StructOpt;
use std::path::PathBuf;
#[derive(Debug, StructOpt)]
#[structopt(name = "tag_writer", about = "A simple tag writer example")]
struct Opt {
#[structopt(short, long)]
title: Option<String>,
#[structopt(short, long)]
artist: Option<String>,
#[structopt(short = "A", long)]
album: Option<String>,
#[structopt(short, long)]
genre: Option<String>,
#[structopt(parse(from_os_str))]
path: PathBuf,
}
fn main() {
let opt = Opt::from_args();
let mut tagged_file = Probe::open(&opt.path)
.expect("ERROR: Bad path provided!")
.read()
.expect("ERROR: Failed to read file!");
let tag = match tagged_file.primary_tag_mut() {
Some(primary_tag) => primary_tag,
None => {
if let Some(first_tag) = tagged_file.first_tag_mut() {
first_tag
} else {
let tag_type = tagged_file.primary_tag_type();
eprintln!("WARN: No tags found, creating a new tag of type `{tag_type:?}`");
tagged_file.insert_tag(Tag::new(tag_type));
tagged_file.primary_tag_mut().unwrap()
}
},
};
if let Opt {
title: None,
artist: None,
album: None,
genre: None,
..
} = opt
{
eprintln!("ERROR: No options provided!");
std::process::exit(1);
}
if let Some(title) = opt.title {
tag.set_title(title)
}
if let Some(artist) = opt.artist {
tag.set_artist(artist)
}
if let Some(album) = opt.album {
tag.set_album(album)
}
if let Some(genre) = opt.genre {
tag.set_genre(genre)
}
tag.save_to_path(&opt.path)
.expect("ERROR: Failed to write the tag!");
println!("INFO: Tag successfully updated!");
}