|
| 1 | +use std::fs::{read, write}; |
1 | 2 | use std::path::PathBuf; |
2 | 3 |
|
| 4 | +#[allow(unused_variables)] |
3 | 5 | fn main() { |
4 | 6 | let arguments::Arguments { |
5 | 7 | options, orphans, .. |
6 | 8 | } = arguments::parse(std::env::args()).expect("failed to parse arguments"); |
7 | 9 | #[allow(clippy::get_first)] |
8 | 10 | let source = match orphans.get(0) { |
9 | 11 | Some(value) => PathBuf::from(value), |
10 | | - _ => { |
11 | | - eprintln!("Error: a source should be given."); |
12 | | - std::process::exit(1); |
13 | | - } |
| 12 | + _ => usage(), |
14 | 13 | }; |
15 | 14 | let destination = match orphans.get(1) { |
16 | 15 | Some(value) => PathBuf::from(value), |
17 | | - _ => { |
18 | | - eprintln!("Error: a destination should be given."); |
19 | | - std::process::exit(1); |
20 | | - } |
| 16 | + _ => usage(), |
21 | 17 | }; |
22 | | - let source_extension = source.extension().and_then(|value| value.to_str()); |
23 | | - let destination_extension = destination.extension().and_then(|value| value.to_str()); |
24 | | - if source_extension |
25 | | - .map(|value| value == "woff") |
26 | | - .unwrap_or(false) |
27 | | - || destination_extension |
28 | | - .map(|value| value == "woff") |
29 | | - .unwrap_or(false) |
30 | | - { |
31 | | - woff::version1::convert(source, destination).expect("failed to transform"); |
32 | | - } else if source_extension |
33 | | - .map(|value| value == "woff2") |
34 | | - .unwrap_or(false) |
35 | | - || destination_extension |
36 | | - .map(|value| value == "woff2") |
37 | | - .unwrap_or(false) |
38 | | - { |
39 | | - woff::version2::convert( |
40 | | - source, |
41 | | - destination, |
42 | | - options.get::<String>("metadata"), |
43 | | - options.get::<usize>("quality"), |
44 | | - options.get::<bool>("transform"), |
45 | | - ) |
46 | | - .expect("failed to transform"); |
47 | | - } else { |
48 | | - eprintln!("Error: one file should end with either .woff or .woff2."); |
49 | | - std::process::exit(1); |
| 18 | + #[allow(unused_mut)] |
| 19 | + let mut data = read(&source).expect("failed to read the source"); |
| 20 | + match ( |
| 21 | + source.extension().and_then(|value| value.to_str()), |
| 22 | + destination.extension().and_then(|value| value.to_str()), |
| 23 | + ) { |
| 24 | + #[cfg(feature = "version1")] |
| 25 | + (_, Some("woff")) => { |
| 26 | + data = woff::version1::compress( |
| 27 | + &data, |
| 28 | + options.get::<usize>("major").unwrap_or(1), |
| 29 | + options.get::<usize>("minor").unwrap_or(0), |
| 30 | + ) |
| 31 | + .expect("failed to compress"); |
| 32 | + } |
| 33 | + #[cfg(feature = "version1")] |
| 34 | + (Some("woff"), _) => { |
| 35 | + data = woff::version1::decompress(&data).expect("failed to decompress"); |
| 36 | + } |
| 37 | + #[cfg(feature = "version2")] |
| 38 | + (_, Some("woff2")) => { |
| 39 | + data = woff::version2::compress( |
| 40 | + &data, |
| 41 | + options.get::<usize>("quality").unwrap_or(8), |
| 42 | + options.get::<String>("metadata").unwrap_or_default(), |
| 43 | + options.get::<bool>("transform").unwrap_or(true), |
| 44 | + ) |
| 45 | + .expect("failed to compress"); |
| 46 | + } |
| 47 | + #[cfg(feature = "version2")] |
| 48 | + (Some("woff2"), _) => { |
| 49 | + data = woff::version2::decompress(&data).expect("failed to decompress"); |
| 50 | + } |
| 51 | + _ => usage(), |
50 | 52 | } |
| 53 | + #[allow(unreachable_code)] |
| 54 | + write(destination, data).expect("failed to write to the destination"); |
| 55 | +} |
| 56 | + |
| 57 | +fn usage() -> ! { |
| 58 | + eprintln!( |
| 59 | + r#"Usage: <source> <destination> [options] |
| 60 | +
|
| 61 | +Either the source or destination should end with either .woff or .woff2. |
| 62 | +
|
| 63 | +Options for WOFF: |
| 64 | + --major <number> |
| 65 | + --minor <number> |
| 66 | +
|
| 67 | +Options for WOFF2: |
| 68 | + --quality <number> |
| 69 | + --metadata <string> |
| 70 | + --no-transform"# |
| 71 | + ); |
| 72 | + std::process::exit(1); |
51 | 73 | } |
0 commit comments