1use crate::config::schema::ArgumentType;
16use crate::error::{DynamicCliError, ParseError, Result};
17use std::time::Duration;
18
19pub fn format_numbered_list<T: std::fmt::Display>(items: &[T]) -> String {
36 items
37 .iter()
38 .enumerate()
39 .map(|(i, item)| format!(" {}. {}", i + 1, item))
40 .collect::<Vec<_>>()
41 .join("\n")
42}
43
44pub fn format_table(headers: &[&str], rows: &[Vec<&str>]) -> String {
62 let mut output = String::new();
63
64 output.push_str(&headers.join(" | "));
66 output.push('\n');
67 output.push_str(&"-".repeat(headers.iter().map(|h| h.len() + 3).sum()));
68 output.push('\n');
69
70 for row in rows {
72 output.push_str(&row.join(" | "));
73 output.push('\n');
74 }
75
76 output
77}
78
79pub fn format_bytes(bytes: u64) -> String {
93 const UNITS: &[&str] = &["B", "KB", "MB", "GB", "TB"];
94
95 if bytes == 0 {
96 return "0 B".to_string();
97 }
98
99 let mut size = bytes as f64;
100 let mut unit_idx = 0;
101
102 while size >= 1024.0 && unit_idx < UNITS.len() - 1 {
103 size /= 1024.0;
104 unit_idx += 1;
105 }
106
107 if unit_idx == 0 {
108 format!("{} {}", bytes, UNITS[0])
109 } else {
110 format!("{:.2} {}", size, UNITS[unit_idx])
111 }
112}
113
114pub fn format_duration(duration: Duration) -> String {
129 let total_secs = duration.as_secs();
130
131 if total_secs == 0 {
132 return "0s".to_string();
133 }
134
135 let hours = total_secs / 3600;
136 let minutes = (total_secs % 3600) / 60;
137 let seconds = total_secs % 60;
138
139 let mut parts = Vec::new();
140
141 if hours > 0 {
142 parts.push(format!("{}h", hours));
143 }
144 if minutes > 0 {
145 parts.push(format!("{}m", minutes));
146 }
147 if seconds > 0 || parts.is_empty() {
148 parts.push(format!("{}s", seconds));
149 }
150
151 parts.join(" ")
152}
153
154pub fn is_blank(s: &str) -> bool {
171 s.trim().is_empty()
172}
173
174pub fn normalize(s: &str) -> String {
184 s.trim().to_lowercase()
185}
186
187pub fn truncate(s: &str, max_len: usize) -> String {
198 if s.len() <= max_len {
199 s.to_string()
200 } else {
201 format!("{}...", &s[..max_len.saturating_sub(3)])
202 }
203}
204
205pub fn is_valid_email(s: &str) -> bool {
220 let parts: Vec<&str> = s.split('@').collect();
222
223 if parts.len() != 2 {
224 return false;
225 }
226
227 let local = parts[0];
228 let domain = parts[1];
229
230 !local.is_empty() && !domain.is_empty() && domain.contains('.')
231}
232
233pub fn parse_int(value: &str, field_name: &str) -> Result<i64> {
250 value.parse::<i64>().map_err(|_| {
251 DynamicCliError::Parse(ParseError::TypeParseError {
252 arg_name: field_name.to_string(),
253 expected_type: "integer".to_string(),
254 value: value.to_string(),
255 details: Some("must be a valid integer".to_string()),
256 })
257 })
258}
259
260pub fn parse_float(value: &str, field_name: &str) -> Result<f64> {
271 value.parse::<f64>().map_err(|_| {
272 DynamicCliError::Parse(ParseError::TypeParseError {
273 arg_name: field_name.to_string(),
274 expected_type: "float".to_string(),
275 value: value.to_string(),
276 details: Some("must be a valid floating-point number".to_string()),
277 })
278 })
279}
280
281pub fn parse_bool(value: &str) -> Result<bool> {
300 match value.trim().to_lowercase().as_str() {
301 "true" | "yes" | "1" | "on" => Ok(true),
302 "false" | "no" | "0" | "off" => Ok(false),
303 _ => Err(DynamicCliError::Parse(ParseError::TypeParseError {
304 arg_name: "value".to_string(),
305 expected_type: "bool".to_string(),
306 value: value.to_string(),
307 details: Some("must be one of: true, false, yes, no, 1, 0, on, off".to_string()),
308 })),
309 }
310}
311
312pub fn detect_type(value: &str) -> ArgumentType {
336 if parse_bool(value).is_ok() {
338 return ArgumentType::Bool;
339 }
340
341 if value.parse::<i64>().is_ok() {
343 return ArgumentType::Integer;
344 }
345
346 if value.contains('.') && value.parse::<f64>().is_ok() {
348 return ArgumentType::Float;
349 }
350
351 if value.starts_with('/')
353 || value.starts_with("./")
354 || value.starts_with("../")
355 || value.contains('\\')
356 {
357 return ArgumentType::Path;
358 }
359
360 ArgumentType::String
362}
363
364pub fn normalize_path(path: &str) -> String {
380 path.replace('\\', "/")
381}
382
383pub fn get_extension(path: &str) -> Option<String> {
395 let path = std::path::Path::new(path);
396 path.extension()
397 .and_then(|ext| ext.to_str())
398 .map(|ext| ext.to_lowercase())
399}
400
401pub fn has_extension(path: &str, extensions: &[&str]) -> bool {
412 if let Some(ext) = get_extension(path) {
413 extensions.iter().any(|&e| e.to_lowercase() == ext)
414 } else {
415 false
416 }
417}
418
419#[cfg(test)]
424pub mod test_helpers {
425 use crate::config::schema::*;
426 use crate::context::ExecutionContext;
427 use std::any::Any;
428
429 pub fn create_test_config(prompt: &str, commands: Vec<&str>) -> CommandsConfig {
431 CommandsConfig {
432 metadata: Metadata {
433 version: "1.0.0".to_string(),
434 prompt: prompt.to_string(),
435 prompt_suffix: " > ".to_string(),
436 },
437 commands: commands
438 .into_iter()
439 .map(|name| create_test_command(name, false))
440 .collect(),
441 global_options: vec![],
442 }
443 }
444
445 pub fn create_test_command(name: &str, required: bool) -> CommandDefinition {
447 CommandDefinition {
448 name: name.to_string(),
449 aliases: vec![],
450 description: format!("Test command: {}", name),
451 required,
452 arguments: vec![],
453 options: vec![],
454 implementation: format!("{}_handler", name),
455 }
456 }
457
458 #[derive(Default, Debug)]
460 pub struct TestContext {
461 pub executed: Vec<String>,
462 }
463
464 impl ExecutionContext for TestContext {
465 fn as_any(&self) -> &dyn Any {
466 self
467 }
468
469 fn as_any_mut(&mut self) -> &mut dyn Any {
470 self
471 }
472 }
473}
474
475#[cfg(test)]
480mod tests {
481 use super::*;
482 use crate::context::ExecutionContext;
483
484 #[test]
489 fn test_format_numbered_list_empty() {
490 let items: Vec<&str> = vec![];
491 assert_eq!(format_numbered_list(&items), "");
492 }
493
494 #[test]
495 fn test_format_numbered_list_single() {
496 let items = vec!["apple"];
497 assert_eq!(format_numbered_list(&items), " 1. apple");
498 }
499
500 #[test]
501 fn test_format_numbered_list_multiple() {
502 let items = vec!["apple", "banana", "cherry"];
503 let result = format_numbered_list(&items);
504 assert!(result.contains("1. apple"));
505 assert!(result.contains("2. banana"));
506 assert!(result.contains("3. cherry"));
507 }
508
509 #[test]
510 fn test_format_table_simple() {
511 let headers = vec!["Name", "Age"];
512 let rows = vec![vec!["Alice", "30"], vec!["Bob", "25"]];
513 let table = format_table(&headers, &rows);
514
515 assert!(table.contains("Name"));
516 assert!(table.contains("Alice"));
517 assert!(table.contains("30"));
518 }
519
520 #[test]
521 fn test_format_bytes_zero() {
522 assert_eq!(format_bytes(0), "0 B");
523 }
524
525 #[test]
526 fn test_format_bytes_various_sizes() {
527 assert_eq!(format_bytes(512), "512 B");
528 assert_eq!(format_bytes(1024), "1.00 KB");
529 assert_eq!(format_bytes(1_048_576), "1.00 MB");
530 assert_eq!(format_bytes(1_073_741_824), "1.00 GB");
531 }
532
533 #[test]
534 fn test_format_duration_zero() {
535 assert_eq!(format_duration(Duration::from_secs(0)), "0s");
536 }
537
538 #[test]
539 fn test_format_duration_various() {
540 assert_eq!(format_duration(Duration::from_secs(45)), "45s");
541 assert_eq!(format_duration(Duration::from_secs(90)), "1m 30s");
542 assert_eq!(format_duration(Duration::from_secs(3665)), "1h 1m 5s");
543 }
544
545 #[test]
550 fn test_is_blank_various() {
551 assert!(is_blank(""));
552 assert!(is_blank(" "));
553 assert!(is_blank("\t\n"));
554 assert!(!is_blank("hello"));
555 assert!(!is_blank(" hello "));
556 }
557
558 #[test]
559 fn test_normalize() {
560 assert_eq!(normalize(" Hello World "), "hello world");
561 assert_eq!(normalize("UPPERCASE"), "uppercase");
562 assert_eq!(normalize("MixedCase"), "mixedcase");
563 }
564
565 #[test]
566 fn test_truncate_long_string() {
567 assert_eq!(truncate("Hello World", 8), "Hello...");
568 }
569
570 #[test]
571 fn test_truncate_short_string() {
572 assert_eq!(truncate("Hi", 10), "Hi");
573 assert_eq!(truncate("Exact", 5), "Exact");
574 }
575
576 #[test]
577 fn test_is_valid_email_valid() {
578 assert!(is_valid_email("user@example.com"));
579 assert!(is_valid_email("name.surname@domain.co.uk"));
580 }
581
582 #[test]
583 fn test_is_valid_email_invalid() {
584 assert!(!is_valid_email("invalid"));
585 assert!(!is_valid_email("@example.com"));
586 assert!(!is_valid_email("user@"));
587 assert!(!is_valid_email("no-at-sign.com"));
588 }
589
590 #[test]
595 fn test_parse_int_valid() {
596 assert_eq!(parse_int("42", "count").unwrap(), 42);
597 assert_eq!(parse_int("-10", "offset").unwrap(), -10);
598 assert_eq!(parse_int("0", "zero").unwrap(), 0);
599 }
600
601 #[test]
602 fn test_parse_int_invalid() {
603 assert!(parse_int("abc", "count").is_err());
604 assert!(parse_int("3.14", "count").is_err());
605 assert!(parse_int("", "count").is_err());
606 }
607
608 #[test]
609 fn test_parse_float_valid() {
610 assert_eq!(parse_float("3.14", "pi").unwrap(), 3.14);
611 assert_eq!(parse_float("42", "value").unwrap(), 42.0);
612 assert_eq!(parse_float("-1.5", "neg").unwrap(), -1.5);
613 }
614
615 #[test]
616 fn test_parse_bool_various() {
617 assert_eq!(parse_bool("true").unwrap(), true);
618 assert_eq!(parse_bool("YES").unwrap(), true);
619 assert_eq!(parse_bool("1").unwrap(), true);
620 assert_eq!(parse_bool("on").unwrap(), true);
621
622 assert_eq!(parse_bool("false").unwrap(), false);
623 assert_eq!(parse_bool("no").unwrap(), false);
624 assert_eq!(parse_bool("0").unwrap(), false);
625 assert_eq!(parse_bool("off").unwrap(), false);
626
627 assert!(parse_bool("maybe").is_err());
628 }
629
630 #[test]
631 fn test_detect_type_integer() {
632 assert_eq!(detect_type("42"), ArgumentType::Integer);
633 assert_eq!(detect_type("-10"), ArgumentType::Integer);
634 }
635
636 #[test]
637 fn test_detect_type_float() {
638 assert_eq!(detect_type("3.14"), ArgumentType::Float);
639 assert_eq!(detect_type("-1.5"), ArgumentType::Float);
640 }
641
642 #[test]
643 fn test_detect_type_bool() {
644 assert_eq!(detect_type("true"), ArgumentType::Bool);
645 assert_eq!(detect_type("false"), ArgumentType::Bool);
646 assert_eq!(detect_type("yes"), ArgumentType::Bool);
647 }
648
649 #[test]
650 fn test_detect_type_path() {
651 assert_eq!(detect_type("/usr/bin"), ArgumentType::Path);
652 assert_eq!(detect_type("./file"), ArgumentType::Path);
653 assert_eq!(detect_type("..\\path"), ArgumentType::Path);
654 }
655
656 #[test]
661 fn test_normalize_path_windows() {
662 assert_eq!(normalize_path("path\\to\\file"), "path/to/file");
663 }
664
665 #[test]
666 fn test_normalize_path_unix() {
667 assert_eq!(normalize_path("path/to/file"), "path/to/file");
668 }
669
670 #[test]
671 fn test_get_extension_valid() {
672 assert_eq!(get_extension("file.TXT"), Some("txt".to_string()));
673 assert_eq!(get_extension("data.csv"), Some("csv".to_string()));
674 }
675
676 #[test]
677 fn test_get_extension_none() {
678 assert_eq!(get_extension("no_extension"), None);
679 assert_eq!(get_extension(".hidden"), None);
680 }
681
682 #[test]
683 fn test_has_extension_match() {
684 assert!(has_extension("data.csv", &["csv", "tsv"]));
685 assert!(has_extension("config.YAML", &["yaml", "yml"]));
686 }
687
688 #[test]
689 fn test_has_extension_no_match() {
690 assert!(!has_extension("data.txt", &["csv", "json"]));
691 assert!(!has_extension("no_ext", &["txt"]));
692 }
693
694 #[test]
699 fn test_create_test_config() {
700 let config = test_helpers::create_test_config("test", vec!["cmd1", "cmd2"]);
701 assert_eq!(config.metadata.prompt, "test");
702 assert_eq!(config.commands.len(), 2);
703 }
704
705 #[test]
706 fn test_create_test_command() {
707 let cmd = test_helpers::create_test_command("test", true);
708 assert_eq!(cmd.name, "test");
709 assert!(cmd.required);
710 }
711
712 #[test]
713 fn test_test_context_downcast() {
714 let mut ctx = test_helpers::TestContext::default();
715 ctx.executed.push("test".to_string());
716
717 let ctx_ref = &ctx as &dyn ExecutionContext;
718 let downcast = crate::context::downcast_ref::<test_helpers::TestContext>(ctx_ref);
719 assert!(downcast.is_some());
720 assert_eq!(downcast.unwrap().executed.len(), 1);
721 }
722}