| 1 | use std::{ |
| 2 | fs::File, |
| 3 | io::{self, BufRead}, |
| 4 | path::Path, |
| 5 | }; |
| 6 | |
| 7 | use scala_native_demangle::{self, DemanglingConfig}; |
| 8 | |
| 9 | use clap::{Parser, Subcommand}; |
| 10 | |
| 11 | #[derive(Parser)] |
| 12 | #[command(author, version, about, long_about = None)] |
| 13 | #[command(propagate_version = true)] |
| 14 | struct Cli { |
| 15 | #[command(subcommand)] |
| 16 | command: Commands, |
| 17 | } |
| 18 | |
| 19 | #[derive(Subcommand)] |
| 20 | enum Commands { |
| 21 | /// Process a file of mangled identifiers, outputting results inline, separating mangled/unmangled names via ` = ` |
| 22 | File { |
| 23 | name: String, |
| 24 | #[arg(long)] |
| 25 | debug: bool, |
| 26 | }, |
| 27 | Id { |
| 28 | name: String, |
| 29 | #[arg(long)] |
| 30 | debug: bool, |
| 31 | }, |
| 32 | } |
| 33 | |
| 34 | fn main() { |
| 35 | let cli = Cli::parse(); |
| 36 | |
| 37 | match &cli.command { |
| 38 | Commands::File { name, debug } => { |
| 39 | if let Ok(lines) = read_lines(name) { |
| 40 | for line in lines { |
| 41 | if let Ok(ip) = line { |
| 42 | match scala_native_demangle::demangle( |
| 43 | &ip, |
| 44 | &DemanglingConfig { |
| 45 | debug: *debug, |
| 46 | ..Default::default() |
| 47 | }, |
| 48 | ) { |
| 49 | Ok(res) => println!("{} = {}", ip, res), |
| 50 | Err(e) => println!("{} ERROR {}", ip, e), |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | Commands::Id { name, debug } => { |
| 57 | println!("{}", name); |
| 58 | println!( |
| 59 | "{}", |
| 60 | scala_native_demangle::demangle( |
| 61 | name, |
| 62 | &DemanglingConfig { |
| 63 | debug: *debug, |
| 64 | ..Default::default() |
| 65 | } |
| 66 | ) |
| 67 | .unwrap() |
| 68 | ) |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // The output is wrapped in a Result to allow matching on errors |
| 74 | // Returns an Iterator to the Reader of the lines of the file. |
| 75 | fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>> |
| 76 | where |
| 77 | P: AsRef<Path>, |
| 78 | { |
| 79 | let file = File::open(filename)?; |
| 80 | Ok(io::BufReader::new(file).lines()) |
| 81 | } |