diff --git a/crates/tools/lib/Cargo.toml b/crates/tools/lib/Cargo.toml index 6fa8140893..fb738fee45 100644 --- a/crates/tools/lib/Cargo.toml +++ b/crates/tools/lib/Cargo.toml @@ -6,5 +6,4 @@ publish = false [dependencies] metadata = { package = "windows-metadata", path = "../../libs/metadata" } -prettyplease = "0.1" syn = "1.0" diff --git a/crates/tools/lib/src/lib.rs b/crates/tools/lib/src/lib.rs index 9cb3ff1bc7..2580d156fa 100644 --- a/crates/tools/lib/src/lib.rs +++ b/crates/tools/lib/src/lib.rs @@ -2,17 +2,7 @@ use std::collections::*; use std::io::*; /// Formats the token string -pub fn format(namespace: &str, tokens: &mut String, use_rustfmt: bool) { - if use_rustfmt { - rustfmt(namespace, tokens); - } else { - let file = syn::parse_file(tokens).unwrap(); - *tokens = prettyplease::unparse(&file); - } -} - -/// Formats the token string with `rustfmt` -pub fn rustfmt(name: &str, tokens: &mut String) { +pub fn format(namespace: &str, tokens: &mut String) { let mut child = std::process::Command::new("rustfmt").stdin(std::process::Stdio::piped()).stdout(std::process::Stdio::piped()).stderr(std::process::Stdio::null()).spawn().expect("Failed to spawn `rustfmt`"); let mut stdin = child.stdin.take().expect("Failed to open stdin"); stdin.write_all(tokens.as_bytes()).unwrap(); @@ -22,7 +12,7 @@ pub fn rustfmt(name: &str, tokens: &mut String) { if output.status.success() { *tokens = String::from_utf8(output.stdout).expect("Failed to parse UTF-8"); } else { - println!("rustfmt failed for `{name}` with status {}\nError:\n{}", output.status, String::from_utf8_lossy(&output.stderr)); + println!("rustfmt failed for `{namespace}` with status {}\nError:\n{}", output.status, String::from_utf8_lossy(&output.stderr)); } } diff --git a/crates/tools/sys/src/main.rs b/crates/tools/sys/src/main.rs index 51ca467324..dee04e6930 100644 --- a/crates/tools/sys/src/main.rs +++ b/crates/tools/sys/src/main.rs @@ -35,12 +35,10 @@ const EXCLUDE_NAMESPACES: [&str; 28] = [ ]; fn main() { - let mut rustfmt = true; let mut expect_namespace = false; let mut namespace = String::new(); for arg in std::env::args() { match arg.as_str() { - "-p" => rustfmt = false, "-n" => expect_namespace = true, _ => { if expect_namespace { @@ -58,14 +56,14 @@ fn main() { let reader = &metadata::reader::Reader::new(&files); if !namespace.is_empty() { let tree = reader.tree(&namespace, &[]).expect("Namespace not found"); - gen_tree(reader, &output, &tree, rustfmt); + gen_tree(reader, &output, &tree); return; } let win32 = reader.tree("Windows.Win32", &EXCLUDE_NAMESPACES).expect("`Windows.Win32` namespace not found"); let wdk = reader.tree("Windows.Wdk", &EXCLUDE_NAMESPACES).expect("`Windows.Win32` namespace not found"); let root = metadata::reader::Tree { namespace: "Windows", nested: BTreeMap::from([("Win32", win32), ("Wdk", wdk)]) }; let trees = root.flatten(); - trees.par_iter().for_each(|tree| gen_tree(reader, &output, tree, rustfmt)); + trees.par_iter().for_each(|tree| gen_tree(reader, &output, tree)); output.pop(); output.push("Cargo.toml"); let mut file = std::fs::File::create(&output).unwrap(); @@ -113,7 +111,7 @@ default = [] } } -fn gen_tree(reader: &metadata::reader::Reader, output: &std::path::Path, tree: &metadata::reader::Tree, rustfmt: bool) { +fn gen_tree(reader: &metadata::reader::Reader, output: &std::path::Path, tree: &metadata::reader::Tree) { println!("{}", tree.namespace); let mut path = std::path::PathBuf::from(output); path.push(tree.namespace.replace('.', "/")); @@ -125,6 +123,6 @@ fn gen_tree(reader: &metadata::reader::Reader, output: &std::path::Path, tree: & gen.cfg = true; gen.doc = true; let mut tokens = bindgen::namespace(&gen, tree); - lib::format(tree.namespace, &mut tokens, rustfmt); + lib::format(tree.namespace, &mut tokens); std::fs::write(path.join("mod.rs"), tokens).unwrap(); } diff --git a/crates/tools/windows/src/main.rs b/crates/tools/windows/src/main.rs index 6294b26b0e..34a5f69041 100644 --- a/crates/tools/windows/src/main.rs +++ b/crates/tools/windows/src/main.rs @@ -5,12 +5,10 @@ use std::io::prelude::*; const EXCLUDE_NAMESPACES: [&str; 14] = ["Windows.AI.MachineLearning.Preview", "Windows.ApplicationModel.SocialInfo", "Windows.Devices.AllJoyn", "Windows.Devices.Perception", "Windows.Security.Authentication.Identity.Provider", "Windows.Services.Cortana", "Windows.System.Power.Diagnostics", "Windows.System.Preview", "Windows.UI.Xaml", "Windows.Win32.Interop", "Windows.Win32.System.Diagnostics.Debug.WebApp", "Windows.Win32.System.WinRT.Xaml", "Windows.Win32.Web.MsHtml", "Windows.Win32.UI.Xaml"]; fn main() { - let mut rustfmt = true; let mut expect_namespace = false; let mut namespace = String::new(); for arg in std::env::args() { match arg.as_str() { - "-p" => rustfmt = false, "-n" => expect_namespace = true, _ => { if expect_namespace { @@ -28,12 +26,12 @@ fn main() { let reader = &metadata::reader::Reader::new(&files); if !namespace.is_empty() { let tree = reader.tree(&namespace, &[]).expect("Namespace not found"); - gen_tree(reader, &output, &tree, rustfmt); + gen_tree(reader, &output, &tree); return; } let root = reader.tree("Windows", &EXCLUDE_NAMESPACES).expect("`Windows` namespace not found"); let trees = root.flatten(); - trees.par_iter().for_each(|tree| gen_tree(reader, &output, tree, rustfmt)); + trees.par_iter().for_each(|tree| gen_tree(reader, &output, tree)); output.pop(); output.push("Cargo.toml"); let mut file = std::fs::File::create(&output).unwrap(); @@ -88,7 +86,7 @@ implement = ["windows-implement", "windows-interface"] } } -fn gen_tree(reader: &metadata::reader::Reader, output: &std::path::Path, tree: &metadata::reader::Tree, rustfmt: bool) { +fn gen_tree(reader: &metadata::reader::Reader, output: &std::path::Path, tree: &metadata::reader::Tree) { println!("{}", tree.namespace); let mut path = std::path::PathBuf::from(output); path.push(tree.namespace.replace('.', "/")); @@ -100,9 +98,9 @@ fn gen_tree(reader: &metadata::reader::Reader, output: &std::path::Path, tree: & gen.doc = true; let mut tokens = bindgen::namespace(&gen, tree); tokens.push_str(r#"#[cfg(feature = "implement")] ::core::include!("impl.rs");"#); - lib::format(tree.namespace, &mut tokens, rustfmt); + lib::format(tree.namespace, &mut tokens); std::fs::write(path.join("mod.rs"), tokens).unwrap(); let mut tokens = bindgen::namespace_impl(&gen, tree); - lib::format(tree.namespace, &mut tokens, rustfmt); + lib::format(tree.namespace, &mut tokens); std::fs::write(path.join("impl.rs"), tokens).unwrap(); }