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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
use crate::{
    app::{
        field::{ArgsTable, Field, Text},
        Run,
    },
    error::{Error, FileReadFailure},
    text_data::{comment::Comment, picture_type::PictureType},
    text_format::TextFormat,
    utils::ModifyTags,
};
use clap::{Args, Subcommand};
use id3::{frame, Content, Tag, TagLike};
use pipe_trait::Pipe;
use std::{borrow::Cow, fs::read as read_file, path::PathBuf};

/// Subcommand of the `set` subcommand.
pub type Set = Field<SetArgsTable>;

impl Run for Text<SetArgsTable> {
    fn run(self) -> Result<(), Error> {
        fn set_text(args: SetText, set: impl FnOnce(&mut Tag, String)) -> Result<(), Error> {
            let SetText {
                no_backup,
                target_audio,
                value,
            } = args;
            ModifyTags::builder()
                .no_backup(no_backup)
                .target_audio(&target_audio)
                .build()
                .run(move |tag| set(tag, value))
        }

        match self {
            Text::Title(args) => set_text(args, Tag::set_title),
            Text::Artist(args) => set_text(args, Tag::set_artist),
            Text::Album(args) => set_text(args, Tag::set_album),
            Text::AlbumArtist(args) => set_text(args, Tag::set_album_artist),
            Text::Genre(SetGenre::Code(args)) => set_text(args, Tag::set_genre),
        }
    }
}

/// Table of [`Args`] types for [`Set`].
#[derive(Debug)]
pub struct SetArgsTable;
impl ArgsTable for SetArgsTable {
    type Text = SetText;
    type Genre = SetGenre;
    type Comment = SetComment;
    type Picture = SetPicture;
}

/// CLI arguments of `set <text-field>`.
#[derive(Debug, Args)]
#[clap(about = "")]
pub struct SetText {
    /// Don't create backup for the target audio file.
    #[clap(long)]
    pub no_backup: bool,
    /// Path to the target audio file.
    pub target_audio: PathBuf,
    /// New value to set.
    pub value: String,
}

/// Genre fields.
#[derive(Debug, Subcommand)]
pub enum SetGenre {
    #[clap(name = "genre-code")]
    Code(SetText),
}

/// CLI arguments of `set comment`.
#[derive(Debug, Args)]
#[clap(about = "")]
pub struct SetComment {
    /// Don't create backup for the target audio file.
    #[clap(long)]
    pub no_backup: bool,
    /// Comment language (ISO 639-2).
    #[clap(long)]
    pub language: Option<String>,
    /// Comment description.
    #[clap(long)]
    pub description: Option<String>,
    /// Format of the ejected comment (if any).
    #[clap(long, value_enum)]
    pub format: Option<TextFormat>,
    /// Path to the target audio file.
    pub target_audio: PathBuf,
    /// Content of the comment.
    pub content: String,
}

impl Run for SetComment {
    fn run(self) -> Result<(), Error> {
        let SetComment {
            no_backup,
            language,
            description,
            format,
            ref target_audio,
            content,
        } = self;
        let language = language.unwrap_or_else(|| "\0\0\0".to_string());
        let description = description.unwrap_or_default();

        let ejected_frame = ModifyTags {
            no_backup,
            target_audio,
        }
        .run(|tag| {
            tag.add_frame(Comment {
                language,
                description,
                content,
            })
        })?;

        let ejected_comment = match ejected_frame.as_ref().map(id3::Frame::content) {
            None => return Ok(()),
            Some(Content::Comment(comment)) => comment,
            Some(content) => panic!("Impossible! The ejected frame wasn't a comment: {content:?}"),
        };

        let output_text: Cow<str> = if let Some(format) = format {
            let output_object = Comment::from(ejected_comment);
            format.serialize(&output_object)?.pipe(Cow::Owned)
        } else {
            Cow::Borrowed(&ejected_comment.text)
        };

        println!("{output_text}");
        Ok(())
    }
}

/// CLI arguments of `set comment`.
#[derive(Debug, Args)]
#[clap(about = "")]
pub struct SetPicture {
    /// Don't create backup for the target audio file.
    #[clap(long)]
    pub no_backup: bool,
    /// Mime type of the picture.
    #[clap(long)]
    pub mime_type: Option<String>,
    /// Description of the picture.
    #[clap(long)]
    pub description: Option<String>,
    /// Path to the target audio file.
    pub target_audio: PathBuf,
    /// Path to the input picture file.
    pub target_picture: PathBuf,
    /// Type of picture.
    #[clap(value_enum)]
    pub picture_type: PictureType,
}

impl Run for SetPicture {
    fn run(self) -> Result<(), Error> {
        let SetPicture {
            no_backup,
            mime_type,
            description,
            ref target_audio,
            ref target_picture,
            picture_type,
        } = self;

        let data = read_file(target_picture).map_err(|error| FileReadFailure {
            file: target_picture.to_path_buf(),
            error,
        })?;

        let mime_type = mime_type
            .or_else(|| infer::get(&data)?.mime_type().to_string().pipe(Some))
            .unwrap_or_default();

        let description = description.unwrap_or_else(|| {
            target_picture
                .file_name()
                .map(|file_name| file_name.to_string_lossy())
                .map(|file_name| file_name.to_string())
                .unwrap_or_default()
        });

        let picture_type: frame::PictureType = picture_type.into();

        let frame = frame::Picture {
            data,
            description,
            mime_type,
            picture_type,
        };

        let ejected_picture = ModifyTags::builder()
            .target_audio(target_audio)
            .no_backup(no_backup)
            .build()
            .run(|tag| tag.add_frame(frame))?;

        if ejected_picture.is_some() {
            eprintln!("A picture had been replaced");
        } else {
            eprintln!("A picture had been added");
        }

        Ok(())
    }
}