MCPcopy Create free account
hub / github.com/Canop/codesort / run

Function run

src/cli/mod.rs:17–67  ·  view source on GitHub ↗

Run the cli application

()

Source from the content-addressed store, hash-verified

15
16/// Run the cli application
17pub fn run() -> CsResult<()> {
18 let args = Args::parse();
19
20 if args.help {
21 args.print_help();
22 return Ok(());
23 }
24
25 if args.version {
26 println!("codesort {}", env!("CARGO_PKG_VERSION"));
27 return Ok(());
28 }
29
30 let src = args.src.as_ref().or(args.file.as_ref());
31 let dst = args.dst.as_ref().or(args.file.as_ref());
32 let lang = args.lang();
33
34 // Read input
35 let list = if let Some(src) = src {
36 let file = fs::File::open(src)?;
37 let reader = BufReader::new(file);
38 LocList::read(reader, lang)?
39 } else {
40 let stdin = std::io::stdin();
41 let reader = stdin.lock();
42 LocList::read(reader, lang)?
43 };
44
45 // Focus the list to the area to sort
46 let focused = match (args.around, args.range) {
47 (Some(_), Some(_)) => {
48 return Err(CsError::RangeAndAround);
49 }
50 (Some(line), None) => list.focus_around_line_number(line)?,
51 (None, Some(range)) => list.focus(range)?,
52 _ => list.focus_all()?,
53 };
54
55 let sorted_list = focused.sort();
56
57 // Write output
58 if let Some(dst) = dst {
59 let file = fs::File::create(dst)?;
60 let mut writer = std::io::BufWriter::new(file);
61 write!(&mut writer, "{}", sorted_list)?;
62 } else {
63 print!("{}", sorted_list);
64 }
65
66 Ok(())
67}

Callers 1

mainFunction · 0.85

Calls 7

print_helpMethod · 0.80
langMethod · 0.80
focusMethod · 0.80
focus_allMethod · 0.80
sortMethod · 0.80
readFunction · 0.50

Tested by

no test coverage detected