diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /third_party/rust/webrtc-sdp/examples/file_parser.rs | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/webrtc-sdp/examples/file_parser.rs')
-rw-r--r-- | third_party/rust/webrtc-sdp/examples/file_parser.rs | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/third_party/rust/webrtc-sdp/examples/file_parser.rs b/third_party/rust/webrtc-sdp/examples/file_parser.rs new file mode 100644 index 0000000000..48851f35f1 --- /dev/null +++ b/third_party/rust/webrtc-sdp/examples/file_parser.rs @@ -0,0 +1,72 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +use std::fs::File; +use std::io::prelude::*; +use std::panic; +use std::path::Path; +extern crate webrtc_sdp; + +// Takes the filename of a file that contains SDP, and optionally the trailing +// flag --expect-failure. +// If --expect-failure is passed, then the program will exit with a successful +// exit code if the file fails to parse. +fn main() { + let mut args = std::env::args(); + let filename = match args.nth(1) { + None => { + eprintln!("Missing file name argument!"); + std::process::exit(1); + } + Some(x) => x, + }; + + let path = Path::new(filename.as_str()); + let display = path.display(); + + let mut file = match File::open(&path) { + Err(why) => panic!("Failed to open {}: {}", display, why), + Ok(file) => file, + }; + + let mut s = String::new(); + match file.read_to_string(&mut s) { + Err(why) => panic!("Couldn't read {}: {}", display, why), + Ok(s) => s, + }; + + // Hook up the panic handler if it is expected to fail to parse + let expect_failure = if let Some(x) = args.next() { + if x.to_lowercase() != "--expect-failure" { + eprintln!("Extra arguments passed!"); + std::process::exit(1); + } + panic::set_hook(Box::new(|_| { + println!("Exited with failure, as expected."); + std::process::exit(0); + })); + true + } else { + false + }; + + // Remove comment lines + let s = s + .lines() + .filter(|&l| !l.trim_start().starts_with(';')) + .collect::<Vec<&str>>() + .join("\r\n"); + + let res = webrtc_sdp::parse_sdp(&s, true); + match res { + Err(why) => panic!("Failed to parse SDP with error: {}", why), + Ok(sdp) => println!("Parsed SDP structure:\n{:#?}", sdp), + } + + if expect_failure { + eprintln!("Successfully parsed SDP that was expected to fail. You may need to update the example expectations."); + std::process::exit(1); + } + println!("Successfully parsed SDP"); +} |