rustic_rs/commands/
docs.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
//! `docs` subcommand

use abscissa_core::{status_err, Application, Command, Runnable, Shutdown};
use anyhow::Result;
use clap::Subcommand;

use crate::{
    application::constants::{RUSTIC_CONFIG_DOCS_URL, RUSTIC_DEV_DOCS_URL, RUSTIC_DOCS_URL},
    RUSTIC_APP,
};

#[derive(Command, Debug, Clone, Copy, Default, Subcommand, Runnable)]
enum DocsTypeSubcommand {
    #[default]
    /// Show the user documentation
    User,
    /// Show the development documentation
    Dev,
    /// Show the configuration documentation
    Config,
}

/// Opens the documentation in the default browser.
#[derive(Clone, Command, Default, Debug, clap::Parser)]
pub struct DocsCmd {
    #[clap(subcommand)]
    cmd: Option<DocsTypeSubcommand>,
}

impl Runnable for DocsCmd {
    fn run(&self) {
        if let Err(err) = self.inner_run() {
            status_err!("{}", err);
            RUSTIC_APP.shutdown(Shutdown::Crash);
        };
    }
}

impl DocsCmd {
    fn inner_run(&self) -> Result<()> {
        let user_string = match self.cmd {
            // Default to user docs if no subcommand is provided
            Some(DocsTypeSubcommand::User) | None => {
                open::that(RUSTIC_DOCS_URL)?;
                format!("Opening the user documentation at {RUSTIC_DOCS_URL}")
            }
            Some(DocsTypeSubcommand::Dev) => {
                open::that(RUSTIC_DEV_DOCS_URL)?;
                format!("Opening the development documentation at {RUSTIC_DEV_DOCS_URL}")
            }
            Some(DocsTypeSubcommand::Config) => {
                open::that(RUSTIC_CONFIG_DOCS_URL)?;
                format!("Opening the configuration documentation at {RUSTIC_CONFIG_DOCS_URL}")
            }
        };

        println!("{user_string}");

        Ok(())
    }
}