aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 4cae988c67527ca568456116d07ceef4a35d5211 (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
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
mod configuration;
mod lyrics_engine;
mod sources;

use clap::Parser;
use std::io::{Error, Result};

use configuration::Configuration;
use lyrics_engine::print_lyrics;
use sources::{disable, enable, get_track, list, reset};

#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Arguments {
    // Positional Arguments
    /// Specify the artist.
    artist: Option<String>,

    /// Specify the artist.
    track_name: Option<String>,

    /// Show title of track if present
    #[arg(short = 't', long)]
    show_title: bool,

    /// Lists all sources
    #[arg(short, long)]
    list_sources: bool,

    /// Enables a source
    #[arg(short, long, value_name = "SOURCE")]
    enable_source: Option<String>,

    #[arg(short, long, value_name = "SOURCE")]
    disable_source: Option<String>,

    #[arg(short, long, value_name = "SOURCE")]
    reset_source: Option<String>,
}

pub struct Track {
    pub name: String,
    pub artist: String,
}

#[tokio::main]
async fn main() -> Result<()> {
    let result = run().await;

    if cfg!(debug_assertions) {
        result
    } else {
        match result {
            Ok(_) => Ok(()),
            Err(e) => {
                eprintln!("Error: {e}");
                std::process::exit(1);
            }
        }
    }
}

async fn run() -> Result<()> {
    let mut configuration = Configuration::new();
    let arguments = Arguments::parse();

    if arguments.list_sources {
        let sources = list();
        for source in sources {
            print!("{source}");
            if configuration.is_enabled(&source) {
                print!(" (enabled)");
            }
            println!();
        }
        return Ok(());
    }

    if let Some(source_name) = arguments.enable_source {
        if !configuration.is_enabled(&source_name) {
            enable(&source_name)?;
        }
        return configuration.enable_source(&source_name);
    }

    if let Some(source_name) = arguments.disable_source {
        if configuration.is_enabled(&source_name) {
            disable(&source_name)?;
        }
        return configuration.disable_source(&source_name);
    }

    if let Some(source_name) = arguments.reset_source {
        return reset(&source_name);
    }

    let current_track: Track;
    if let Some(artist) = arguments.artist {
        current_track = Track {
            name: arguments.track_name.unwrap_or("".to_string()),
            artist,
        };
    } else {
        current_track = get_track(&configuration)
            .ok_or_else(|| Error::other("No Artist/Song could be found :("))?
    }

    print_lyrics(current_track, arguments.show_title).await
}