| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
|
| 5 |
use std::fs::File; |
| 6 |
use std::io::prelude::*; |
| 7 |
use std::panic; |
| 8 |
use std::path::Path; |
| 9 |
extern crate webrtc_sdp; |
| 10 |
|
| 11 |
// Takes the filename of a file that contains SDP, and optionally the trailing |
| 12 |
// flag --expect-failure. |
| 13 |
// If --expect-failure is passed, then the program will exit with a successful |
| 14 |
// exit code if the file fails to parse. |
| 15 |
fn main() { |
| 16 |
let mut args = std::env::args(); |
| 17 |
let filename = match args.nth(1) { |
| 18 |
None => { |
| 19 |
eprintln!("Missing file name argument!"); |
| 20 |
std::process::exit(1); |
| 21 |
} |
| 22 |
Some(x) => x, |
| 23 |
}; |
| 24 |
|
| 25 |
let path = Path::new(filename.as_str()); |
| 26 |
let display = path.display(); |
| 27 |
|
| 28 |
let mut file = match File::open(path) { |
| 29 |
Err(why) => panic!("Failed to open {}: {}", display, why), |
| 30 |
Ok(file) => file, |
| 31 |
}; |
| 32 |
|
| 33 |
let mut s = String::new(); |
| 34 |
match file.read_to_string(&mut s) { |
| 35 |
Err(why) => panic!("Couldn't read {}: {}", display, why), |
| 36 |
Ok(s) => s, |
| 37 |
}; |
| 38 |
|
| 39 |
// Hook up the panic handler if it is expected to fail to parse |
| 40 |
let expect_failure = if let Some(x) = args.next() { |
| 41 |
if x.to_lowercase() != "--expect-failure" { |
| 42 |
eprintln!("Extra arguments passed!"); |
| 43 |
std::process::exit(1); |
| 44 |
} |
| 45 |
panic::set_hook(Box::new(|_| { |
| 46 |
println!("Exited with failure, as expected."); |
| 47 |
std::process::exit(0); |
| 48 |
})); |
| 49 |
true |
| 50 |
} else { |
| 51 |
false |
| 52 |
}; |
| 53 |
|
| 54 |
// Remove comment lines |
| 55 |
let s = s |
| 56 |
.lines() |
| 57 |
.filter(|&l| !l.trim_start().starts_with(';')) |
| 58 |
.collect::<Vec<&str>>() |
| 59 |
.join("\r\n"); |
| 60 |
|
| 61 |
let res = webrtc_sdp::parse_sdp(&s, true); |
| 62 |
match res { |
| 63 |
Err(why) => panic!("Failed to parse SDP with error: {}", why), |
| 64 |
Ok(sdp) => println!("Parsed SDP structure:\n{sdp:#?}"), |
| 65 |
} |
| 66 |
|
| 67 |
if expect_failure { |
| 68 |
eprintln!("Successfully parsed SDP that was expected to fail. You may need to update the example expectations."); |
| 69 |
std::process::exit(1); |
| 70 |
} |
| 71 |
println!("Successfully parsed SDP"); |
| 72 |
} |