summaryrefslogtreecommitdiffstats
path: root/third_party/rust/webrtc-sdp/examples/file_parser.rs
blob: 48851f35f1002ec342087b4c67d526c223161ac7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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");
}