mtracker/
rate.rs

1use anyhow::Result;
2use clap::{Arg, ArgMatches, Command};
3
4use crate::arg_util;
5use crate::args;
6
7pub fn command() -> Command {
8    Command::new("rate")
9        .visible_aliases(["r"])
10        .about("Rate item")
11        .arg_required_else_help(true)
12        .arg(args::identifier())
13        .arg(
14            Arg::new("RATING")
15                .required(true)
16                .value_parser(clap::value_parser!(u8).range(0..=255))
17                .help("Rating (number)"),
18        )
19        .arg(args::year())
20}
21
22pub fn handle(matches: &ArgMatches) -> Result<()> {
23    let mut repo = arg_util::repo_from_matches(matches)?;
24    let handle = arg_util::handle_from_matches(matches)?.unwrap();
25    let rating = matches.get_one::<u8>("RATING");
26
27    let media = repo.get_or_create(&handle)?;
28
29    media.rating = rating.copied();
30    println!("Rated {handle}: {}", rating.unwrap());
31
32    if media.on_watchlist() {
33        media.remove_tag("watchlist")?;
34        println!("Removed from watchlist: {handle}");
35    }
36
37    repo.write()
38}