Skip to main content

lectio_diei/
args.rs

1use clap::{Args, Parser, Subcommand, ValueEnum};
2use serde::{Deserialize, Serialize};
3
4#[derive(Parser)]
5#[command(version, about, long_about = None)]
6pub struct Arguments {
7    #[command(subcommand)]
8    pub command: Command,
9
10    #[command(flatten)]
11    pub common_args: CommonArguments,
12}
13
14#[derive(Args, Copy, Clone)]
15pub struct CommonArguments {
16    /// Disables colors
17    ///
18    /// Output for STDERR and STDOUT will not print with ANSI color codes. Useful if terminal does not support colors or redirecting to file
19    #[arg(long, global = true)]
20    pub no_color: bool,
21}
22
23#[derive(Subcommand)]
24pub enum Command {
25    /// Print the Reading to STDOUT
26    Display {
27        /// Date to retrieve (Uses today if not specified)
28        #[arg(short, long)]
29        date: Option<String>,
30
31        #[command(flatten)]
32        readings: DisplayReadingsArgs,
33
34        #[command(flatten)]
35        formatting: FormattingArgs,
36    },
37    /// Manage the database, including retrieving more readings
38    Db {
39        #[command(subcommand)]
40        command: DatabaseCommand,
41    },
42    /// View and change the config
43    //TODO set config settings
44    Config {
45        #[command(subcommand)]
46        command: ConfigCommand,
47    },
48}
49
50#[derive(Subcommand)]
51pub enum DatabaseCommand {
52    /// Removes specified date(s) from database if present.
53    ///
54    /// Writes number removed to STDOUT
55    Remove {
56        /// Dates to remove. Should be in MMddYY format
57        #[arg(trailing_var_arg(true), num_args(1..usize::MAX))]
58        dates: Vec<String>,
59    },
60    /// Gets a count of the rows in the db.
61    ///
62    /// Writes num to STDOUT
63    Count,
64    /// Adds entries from the web to the database
65    //TODO add arguments to override config
66    Update,
67    /// Shows all of the lectionary rows in the database
68    ///
69    /// Prints every row of the lectionary table, sorted by date, as "[date] [name]"
70    #[command(alias = "list")]
71    Show,
72    /// Deletes all data in the database
73    ///
74    /// Writes number of rows removed to STDOUT
75    Purge,
76    /// Deletes old entries from the database
77    ///
78    /// Uses values defined in the config
79    Clean {
80        #[arg[short, long]]
81        all: bool,
82    },
83    /// Equivalent of db clean + db update
84    Refresh,
85    /// Stores specified dates in to the database if they are not present
86    ///
87    /// Writes number of new entries to STDOUT
88    Store {
89        #[arg(trailing_var_arg(true), num_args(1..usize::MAX))]
90        dates: Vec<String>,
91    },
92}
93
94#[derive(Subcommand, Copy, Clone)]
95pub enum ConfigCommand {
96    /// Initializes the data at the default location
97    Init {
98        /// Overrides file if it exists
99        #[arg(short, long)]
100        force: bool,
101    },
102    /// Writes any missing values in to the config
103    Upgrade,
104    /// Writes the config to STDOUT
105    Show,
106}
107
108#[derive(Args, Copy, Clone)]
109#[group(required = false, multiple = false)]
110pub struct FormattingArgs {
111    /// Format the lines so that each has a given maximum length
112    ///
113    /// Does not affect alleluia or responsorial psalm which are always displayed with original linebreaks
114    #[arg(short = 'w', long)]
115    pub max_width: Option<u16>,
116
117    /// Use the original line breaks
118    #[arg(short, long)]
119    pub original_linebreaks: bool,
120}
121
122#[derive(Args)]
123#[group(required = false, multiple = false)]
124pub struct DisplayReadingsArgs {
125    /// Displays the readings in the specified order
126    #[arg(short, long, alias="reading", value_enum, num_args=1..)]
127    pub readings: Option<Vec<ReadingArg>>,
128
129    /// Displays all readings in default order
130    #[arg(short, long)]
131    pub all: bool,
132
133    /// Only display the name of the day
134    #[arg(long)]
135    pub day_only: bool,
136}
137
138#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum, Serialize, Deserialize)]
139#[serde(rename_all = "lowercase")]
140pub enum ReadingArg {
141    Reading1,
142    Psalm,
143    Reading2,
144    Gospel,
145    Alleluia,
146}
147
148#[cfg(test)]
149mod tests {
150    use super::*;
151    use clap::CommandFactory;
152
153    #[test]
154    fn arguments_works() {
155        Arguments::command().debug_assert();
156    }
157}