subx_cli/cli/
detect_encoding_args.rs1use crate::cli::InputPathHandler;
32use crate::error::SubXError;
33use clap::Args;
34use std::path::PathBuf;
35
36#[derive(Args, Debug)]
38pub struct DetectEncodingArgs {
39 #[arg(short, long)]
41 pub verbose: bool,
42
43 #[arg(
45 short = 'i',
46 long = "input",
47 value_name = "PATH",
48 conflicts_with = "file_paths"
49 )]
50 pub input_paths: Vec<PathBuf>,
51
52 #[arg(short, long)]
54 pub recursive: bool,
55
56 #[arg(required = true, conflicts_with = "input_paths")]
58 pub file_paths: Vec<String>,
59
60 #[arg(long, default_value_t = false)]
62 pub no_extract: bool,
63}
64
65#[cfg(test)]
66mod tests {
67 use crate::cli::{Cli, Commands};
68 use clap::Parser;
69 use std::path::PathBuf;
70
71 #[test]
72 fn test_detect_encoding_args_file_paths() {
73 let cli = Cli::try_parse_from(["subx-cli", "detect-encoding", "a.srt", "b.ass"]).unwrap();
74 let args = match cli.command {
75 Commands::DetectEncoding(a) => a,
76 _ => panic!("Expected DetectEncoding command"),
77 };
78 assert!(args.input_paths.is_empty());
79 assert_eq!(
80 args.file_paths,
81 vec!["a.srt".to_string(), "b.ass".to_string()]
82 );
83 assert!(!args.recursive);
84 }
85
86 #[test]
87 fn test_detect_encoding_args_input_paths() {
88 let cli = Cli::try_parse_from([
89 "subx-cli",
90 "detect-encoding",
91 "-i",
92 "dir1",
93 "-i",
94 "dir2",
95 "--recursive",
96 "--verbose",
97 ])
98 .unwrap();
99 let args = match cli.command {
100 Commands::DetectEncoding(a) => a,
101 _ => panic!("Expected DetectEncoding command"),
102 };
103 assert!(args.file_paths.is_empty());
104 assert_eq!(
105 args.input_paths,
106 vec![PathBuf::from("dir1"), PathBuf::from("dir2")]
107 );
108 assert!(args.recursive);
109 assert!(args.verbose);
110 }
111
112 #[test]
113 fn test_detect_encoding_args_conflict_file_and_input() {
114 let res = Cli::try_parse_from(["subx-cli", "detect-encoding", "file.srt", "-i", "dir"]);
115 assert!(res.is_err());
116 }
117}
118
119impl DetectEncodingArgs {
120 pub fn get_input_handler(&self) -> Result<InputPathHandler, SubXError> {
122 let merged_paths = InputPathHandler::merge_paths_from_multiple_sources(
123 &[],
124 &self.input_paths,
125 &self.file_paths,
126 )?;
127
128 Ok(InputPathHandler::from_args(&merged_paths, self.recursive)?
129 .with_extensions(&["srt", "ass", "vtt", "ssa", "sub", "txt"])
130 .with_no_extract(self.no_extract))
131 }
132
133 pub fn get_file_paths(&self) -> Result<Vec<PathBuf>, SubXError> {
135 if !self.input_paths.is_empty() {
136 let handler = InputPathHandler::from_args(&self.input_paths, self.recursive)?
137 .with_extensions(&["srt", "ass", "vtt", "ssa", "sub", "txt"]);
138 return handler.collect_files().map(|cf| cf.into_paths());
139 }
140 if !self.file_paths.is_empty() {
141 return Ok(self.file_paths.iter().map(PathBuf::from).collect());
142 }
143 Err(SubXError::NoInputSpecified)
144 }
145}