chrome_remote_interface_model_tools/
check_features.rs

1use std::collections::BTreeMap;
2use std::fs;
3use std::path::Path;
4
5use serde::{Deserialize, Serialize};
6
7use crate::Annotatable;
8
9#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
10struct CargoToml {
11    features: BTreeMap<String, Vec<String>>,
12}
13
14fn expect<P>(path: P) -> anyhow::Result<CargoToml>
15where
16    P: AsRef<Path>,
17{
18    let mut features = BTreeMap::new();
19
20    let protocol_json = fs::read_to_string(path)?;
21    let protocol_json = serde_json::from_str::<crate::Protocol>(&protocol_json)?;
22
23    features.insert(
24        "default".into(),
25        vec!["Browser".into(), "Target".into(), "Page".into()],
26    );
27    features.insert("experimental".into(), vec![]);
28    for mut domain in protocol_json.domains {
29        let mut deps = if domain.annotation().experimental {
30            vec!["experimental".into()]
31        } else {
32            vec![]
33        };
34        deps.append(&mut domain.dependencies);
35        features.insert(domain.domain.clone(), deps);
36    }
37
38    Ok(CargoToml { features })
39}
40
41fn actual<P>(path: P) -> anyhow::Result<CargoToml>
42where
43    P: AsRef<Path>,
44{
45    let actual = fs::read_to_string(path)?;
46    Ok(toml::from_str::<CargoToml>(&actual)?)
47}
48
49pub fn check_features<P1, P2>(cargo_toml: P1, protocol_json: P2) -> anyhow::Result<()>
50where
51    P1: AsRef<Path>,
52    P2: AsRef<Path>,
53{
54    let actual = actual(cargo_toml)?;
55    let expect = expect(protocol_json)?;
56
57    if actual != expect {
58        anyhow::bail!(
59            r#"features not expected.
60expected:
61
62{}"#,
63            toml::to_string(&expect)?
64        );
65    }
66    Ok(())
67}