Skip to content

Commit

Permalink
cppwinrt should consistently panic on failure (#3415)
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr authored Jan 6, 2025
1 parent d02c977 commit 5e8ce09
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 22 deletions.
5 changes: 1 addition & 4 deletions crates/libs/cppwinrt/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ version = "0.1"
Use `cppwinrt` function as needed:

```rust
match cppwinrt::cppwinrt(["-help"]) {
Ok(output) => println!("{output}"),
Err(error) => println!("{error}"),
};
println!("{}", cppwinrt::cppwinrt(["-help"]));
```

Source:
Expand Down
19 changes: 11 additions & 8 deletions crates/libs/cppwinrt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const VERSION: &str = "2.0.240405.15";
/// Calls the C++/WinRT compiler with the given arguments.
///
/// Use `cppwinrt["-help"]` for available options.
pub fn cppwinrt<I, S>(args: I) -> Result<String, String>
pub fn cppwinrt<I, S>(args: I) -> String
where
I: IntoIterator<Item = S>,
S: AsRef<std::ffi::OsStr>,
Expand All @@ -27,9 +27,9 @@ where
_ = std::fs::remove_file(path);

if output.status.success() {
Ok(String::from_utf8_lossy(&output.stdout).to_string())
String::from_utf8_lossy(&output.stdout).to_string()
} else {
Err(String::from_utf8_lossy(&output.stderr).to_string())
panic!("{}", String::from_utf8_lossy(&output.stderr))
}
}

Expand All @@ -38,11 +38,14 @@ mod tests {
use crate::*;

#[test]
fn test() {
let ok = cppwinrt(["-help"]).unwrap();
assert!(ok.contains(VERSION), "unexpected version");
#[should_panic(expected = "'-invalid' is not supported")]
fn invalid_arg() {
cppwinrt(["-invalid"]);
}

let err = cppwinrt(["-invalid"]).unwrap_err();
assert!(err.contains("'-invalid' is not supported"));
#[test]
fn unexpected_version() {
let ok = cppwinrt(["-help"]);
assert!(ok.contains(VERSION), "unexpected version");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ fn main() {
&format!("{}\\System32\\WinMetadata", env!("windir")),
"-out",
&include,
])
.unwrap();
]);

cc::Build::new()
.cpp(true)
Expand Down
3 changes: 1 addition & 2 deletions crates/tests/winrt/composable_client/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ fn main() {
"../../../libs/bindgen/default",
"-out",
&include,
])
.unwrap();
]);

cc::Build::new()
.cpp(true)
Expand Down
3 changes: 1 addition & 2 deletions crates/tests/winrt/constructors_client/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ fn main() {
"../../../libs/bindgen/default",
"-out",
&include,
])
.unwrap();
]);

cc::Build::new()
.cpp(true)
Expand Down
3 changes: 1 addition & 2 deletions crates/tests/winrt/noexcept/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ fn main() {
&format!("{}\\System32\\WinMetadata", env!("windir")),
"-out",
&include,
])
.unwrap();
]);

cc::Build::new()
.cpp(true)
Expand Down
3 changes: 1 addition & 2 deletions crates/tests/winrt/ref_params/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ fn main() {
&format!("{}\\System32\\WinMetadata", env!("windir")),
"-out",
&include,
])
.unwrap();
]);

cc::Build::new()
.cpp(true)
Expand Down

0 comments on commit 5e8ce09

Please sign in to comment.