|
1 | | -use clap::Parser; |
2 | | -use html_parser::{Dom, Result}; |
3 | | -use std::{ |
4 | | - fs::File, |
5 | | - io::{self, Read}, |
6 | | - path::PathBuf, |
7 | | -}; |
8 | | - |
9 | | -#[derive(Debug, Parser)] |
10 | | -/// A simple and general purpose html/xhtml parser. |
11 | | -struct Opt { |
12 | | - #[arg(short, long)] |
13 | | - /// Pretty-print the output. |
14 | | - pretty_print: bool, |
15 | | - |
16 | | - #[arg(short, long)] |
17 | | - /// Debug the parser, this will print errors to the console. |
18 | | - debug: bool, |
19 | | - |
20 | | - /// Path to the file, or stdin (piped content). |
21 | | - /// |
22 | | - /// This argument can either be a path to the html-file that you would like to parse or the |
23 | | - /// result of stdin. Note: Content over stdin needs to be finite, for now, as it is collected |
24 | | - /// into a string and then processed by the parser. |
25 | | - input: Option<PathBuf>, |
| 1 | +fn main() -> html_parser::Result<()> { |
| 2 | + #[cfg(feature = "test")] |
| 3 | + { |
| 4 | + real::main() |
| 5 | + } |
| 6 | + |
| 7 | + #[cfg(not(feature = "test"))] |
| 8 | + panic!("this example requires the `test` feature to be enabled"); |
26 | 9 | } |
27 | 10 |
|
28 | | -fn main() -> Result<()> { |
29 | | - let opt = Opt::parse(); |
| 11 | +#[cfg(feature = "test")] |
| 12 | +mod real { |
| 13 | + use clap::Parser; |
| 14 | + use html_parser::{Dom, Result}; |
| 15 | + use std::{ |
| 16 | + fs::File, |
| 17 | + io::{self, Read}, |
| 18 | + path::PathBuf, |
| 19 | + }; |
30 | 20 |
|
31 | | - let mut content = String::with_capacity(100_000); |
| 21 | + #[derive(Debug, Parser)] |
| 22 | + /// A simple and general purpose html/xhtml parser. |
| 23 | + struct Opt { |
| 24 | + #[arg(short, long)] |
| 25 | + /// Pretty-print the output. |
| 26 | + pretty_print: bool, |
32 | 27 |
|
33 | | - // If input is provided then use that as a path |
34 | | - if let Some(path) = opt.input { |
35 | | - let mut file = File::open(path)?; |
36 | | - file.read_to_string(&mut content)?; |
| 28 | + #[arg(short, long)] |
| 29 | + /// Debug the parser, this will print errors to the console. |
| 30 | + debug: bool, |
37 | 31 |
|
38 | | - // Else read from stdin, this enables piping |
39 | | - // ex: `cat index.html | html_parser` |
40 | | - } else { |
41 | | - let stdin = io::stdin(); |
42 | | - let mut handle = stdin.lock(); |
43 | | - handle.read_to_string(&mut content)?; |
44 | | - }; |
| 32 | + /// Path to the file, or stdin (piped content). |
| 33 | + /// |
| 34 | + /// This argument can either be a path to the html-file that you would like to parse or the |
| 35 | + /// result of stdin. Note: Content over stdin needs to be finite, for now, as it is collected |
| 36 | + /// into a string and then processed by the parser. |
| 37 | + input: Option<PathBuf>, |
| 38 | + } |
| 39 | + |
| 40 | + pub(super) fn main() -> Result<()> { |
| 41 | + let opt = Opt::parse(); |
| 42 | + |
| 43 | + let mut content = String::with_capacity(100_000); |
45 | 44 |
|
46 | | - let dom = Dom::parse(&content)?; |
| 45 | + // If input is provided then use that as a path |
| 46 | + if let Some(path) = opt.input { |
| 47 | + let mut file = File::open(path)?; |
| 48 | + file.read_to_string(&mut content)?; |
47 | 49 |
|
48 | | - if opt.debug { |
49 | | - for error in &dom.errors { |
50 | | - println!("# {}", error); |
| 50 | + // Else read from stdin, this enables piping |
| 51 | + // ex: `cat index.html | html_parser` |
| 52 | + } else { |
| 53 | + let stdin = io::stdin(); |
| 54 | + let mut handle = stdin.lock(); |
| 55 | + handle.read_to_string(&mut content)?; |
| 56 | + }; |
| 57 | + |
| 58 | + let dom = Dom::parse(&content)?; |
| 59 | + |
| 60 | + if opt.debug { |
| 61 | + for error in &dom.errors { |
| 62 | + println!("# {}", error); |
| 63 | + } |
51 | 64 | } |
52 | | - } |
53 | 65 |
|
54 | | - if opt.pretty_print { |
55 | | - println!("{}", dom.to_json_pretty()?); |
56 | | - } else { |
57 | | - println!("{}", dom.to_json()?); |
58 | | - } |
| 66 | + if opt.pretty_print { |
| 67 | + println!("{}", dom.to_json_pretty()?); |
| 68 | + } else { |
| 69 | + println!("{}", dom.to_json()?); |
| 70 | + } |
59 | 71 |
|
60 | | - Ok(()) |
| 72 | + Ok(()) |
| 73 | + } |
61 | 74 | } |
0 commit comments