BlueGit

timfa posprint

Files

posprint / src / cli.rs

cli.rs

cli.rs 3.3 KiB Download Raw
1use std::collections::HashMap;
2use std::path::PathBuf;
3
4use clap::{Parser, Subcommand};
5
6use crate::pipeline;
7
8#[derive(Parser, Debug)]
9#[command(
10 name = "posprint",
11 about = "POS print document designer",
12 disable_version_flag = false
13)]
14pub struct Cli {
15 #[command(subcommand)]
16 pub command: Option<Commands>,
17
18 /// Open the print dialog only (no editor). Exits after printing or cancel.
19 #[arg(long)]
20 pub print: bool,
21
22 /// POS template file to open in the GUI editor
23 #[arg(value_name = "FILE")]
24 pub file: Option<PathBuf>,
25}
26
27#[derive(Subcommand, Debug)]
28pub enum Commands {
29 /// Show command-line printing help (also in the GUI Help tab)
30 #[command(name = "cli-help")]
31 CliHelp,
32 /// Print a .pos file to a USB-serial printer (all parameters required)
33 Print {
34 /// Path to the `.pos` template file
35 file: PathBuf,
36 /// Serial port (e.g. COM3 on Windows, /dev/ttyUSB0 on Linux)
37 #[arg(short, long)]
38 printer: String,
39 /// Template parameter as NAME=VALUE (repeat for each ${Name} in the template)
40 #[arg(long = "set", value_name = "NAME=VALUE")]
41 set: Vec<String>,
42 },
43}
44
45pub fn run(cli: Cli) -> i32 {
46 match cli.command {
47 Some(Commands::CliHelp) => {
48 println!("{}", crate::HELP_CLI_MARKDOWN);
49 0
50 }
51 Some(Commands::Print { file, printer, set }) => match run_print(&file, &printer, &set) {
52 Ok(()) => 0,
53 Err(e) => {
54 eprintln!("{e}");
55 1
56 }
57 },
58 None => {
59 eprintln!(
60 "No subcommand specified. Run `posprint --help` or start without arguments for the GUI."
61 );
62 1
63 }
64 }
65}
66
67fn run_print(file: &PathBuf, printer: &str, set: &[String]) -> Result<(), String> {
68 let params = parse_params(set)?;
69 pipeline::print_pos_file(file, printer, &params).map_err(|e| e.to_string())
70}
71
72pub fn parse_params(pairs: &[String]) -> Result<HashMap<String, String>, String> {
73 let mut map = HashMap::new();
74 for pair in pairs {
75 let Some((name, value)) = pair.split_once('=') else {
76 return Err(format!("invalid parameter '{pair}', expected NAME=VALUE"));
77 };
78 if name.is_empty() {
79 return Err("parameter name cannot be empty".into());
80 }
81 map.insert(name.to_string(), value.to_string());
82 }
83 Ok(map)
84}
85
86#[cfg(test)]
87mod tests {
88 use super::*;
89
90 #[test]
91 fn parses_set_pairs() {
92 let params = parse_params(&["A=1".into(), "B=two".into()]).unwrap();
93 assert_eq!(params.get("A").unwrap(), "1");
94 assert_eq!(params.get("B").unwrap(), "two");
95 }
96
97 #[test]
98 fn rejects_invalid_pair() {
99 assert!(parse_params(&["nope".into()]).is_err());
100 }
101
102 #[test]
103 fn accepts_print_flag_with_file() {
104 use clap::Parser;
105 let cli = Cli::parse_from(["posprint", "--print", "doc.pos"]);
106 assert!(cli.print);
107 assert_eq!(cli.file.as_ref().unwrap().to_str().unwrap(), "doc.pos");
108 }
109
110 #[test]
111 fn cli_help_subcommand_prints_docs() {
112 assert_eq!(run(Cli {
113 command: Some(Commands::CliHelp),
114 print: false,
115 file: None,
116 }), 0);
117 }
118}