Skip to content

Commit

Permalink
error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr committed Sep 12, 2024
1 parent dd37c55 commit c5e09b5
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 31 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/clippy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ jobs:
run: cargo clippy -p test_async
- name: Clippy test_bcrypt
run: cargo clippy -p test_bcrypt
- name: Clippy test_bindgen
run: cargo clippy -p test_bindgen
- name: Clippy test_calling_convention
run: cargo clippy -p test_calling_convention
- name: Clippy test_cfg_generic
Expand Down
6 changes: 4 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ jobs:
run: cargo test -p test_async --target ${{ matrix.target }} ${{ matrix.etc }}
- name: Test test_bcrypt
run: cargo test -p test_bcrypt --target ${{ matrix.target }} ${{ matrix.etc }}
- name: Test test_bindgen
run: cargo test -p test_bindgen --target ${{ matrix.target }} ${{ matrix.etc }}
- name: Test test_calling_convention
run: cargo test -p test_calling_convention --target ${{ matrix.target }} ${{ matrix.etc }}
- name: Test test_cfg_generic
Expand Down Expand Up @@ -253,10 +255,10 @@ jobs:
run: cargo test -p test_registry --target ${{ matrix.target }} ${{ matrix.etc }}
- name: Test test_registry_default
run: cargo test -p test_registry_default --target ${{ matrix.target }} ${{ matrix.etc }}
- name: Test test_reserved
run: cargo test -p test_reserved --target ${{ matrix.target }} ${{ matrix.etc }}
- name: Clean
run: cargo clean
- name: Test test_reserved
run: cargo test -p test_reserved --target ${{ matrix.target }} ${{ matrix.etc }}
- name: Test test_resources
run: cargo test -p test_resources --target ${{ matrix.target }} ${{ matrix.etc }}
- name: Test test_result
Expand Down
4 changes: 0 additions & 4 deletions crates/libs/bindgen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ metadata = []
windows-metadata = { path = "../metadata", version = "0.58.0" }
rayon = "1.7"

[dependencies.syn]
version = "2.0"
features = ["full", "extra-traits"]

[dependencies.proc-macro2]
version = "1.0"
features = ["span-locations"]
Expand Down
26 changes: 1 addition & 25 deletions crates/libs/bindgen/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,13 @@ pub type Result<T> = std::result::Result<T, Error>;
pub struct Error {
message: String,
path: String,
span: Option<(usize, usize)>,
}

impl std::error::Error for Error {}

impl From<Error> for std::io::Error {
fn from(error: Error) -> Self {
std::io::Error::new(std::io::ErrorKind::Other, error.message.as_str())
}
}

impl From<syn::Error> for Error {
fn from(error: syn::Error) -> Self {
let start = error.span().start();
Self {
message: error.to_string(),
span: Some((start.line, start.column)),
..Self::default()
}
}
}

impl std::fmt::Display for Error {
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(fmt, "error: {}", self.message)?;
if !self.path.is_empty() {
if let Some((line, column)) = self.span {
writeln!(fmt, " --> {}:{line}:{column}", self.path)?;
} else {
writeln!(fmt, " --> {}", self.path)?;
}
writeln!(fmt, " --> {}", self.path)?;
}
Ok(())
}
Expand Down
12 changes: 12 additions & 0 deletions crates/tests/misc/bindgen/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "test_bindgen"
version = "0.0.0"
edition = "2021"
publish = false

[lib]
doc = false
doctest = false

[dependencies.windows-bindgen]
workspace = true
1 change: 1 addition & 0 deletions crates/tests/misc/bindgen/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

13 changes: 13 additions & 0 deletions crates/tests/misc/bindgen/tests/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#[test]
fn test() {
let e = windows_bindgen::bindgen(["--etc", "file_not_found.txt"]).unwrap_err();

assert_eq!(
format!("{e}"),
"error: failed to read lines\n --> file_not_found.txt\n"
);

let e = windows_bindgen::bindgen(["-etc"]).unwrap_err();

assert_eq!(format!("{e}"), "error: invalid option `-etc`\n");
}

0 comments on commit c5e09b5

Please sign in to comment.