Skip to content

Commit ad108be

Browse files
author
Pat Hickey
authored
Improve Display and Debug impls of Error (#58)
* expose public functions for the name (e.g. "INVAL") and docs corresponding to errno * regenerate lib_generated, use funcs in Error's display and debug impls * bump version to 0.10.2 * add documentation to generate lib_generated.rs
1 parent 23d4d4a commit ad108be

File tree

5 files changed

+120
-5
lines changed

5 files changed

+120
-5
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "wasi"
3-
version = "0.10.1+wasi-snapshot-preview1"
3+
version = "0.10.2+wasi-snapshot-preview1"
44
authors = ["The Cranelift Project Developers"]
55
license = "Apache-2.0 WITH LLVM-exception OR Apache-2.0 OR MIT"
66
description = "Experimental WASI API bindings for Rust"

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,24 @@ $ cargo wasi run
6464
Hello, World!
6565
```
6666

67+
# Development
68+
69+
The bulk of the `wasi` crate is generated by the `witx-bindgen` tool, which lives at
70+
`crates/witx-bindgen` and is part of the cargo workspace.
71+
72+
The `src/lib_generated.rs` file can be re-generated with the following
73+
command:
74+
75+
```
76+
cargo run -p witx-bindgen -- crates/witx-bindgen/WASI/phases/snapshot/witx/wasi_snapshot_preview1.witx > src/lib_generated.rs
77+
```
78+
79+
Note that this uses the WASI standard repository as a submodule. If you do not
80+
have this submodule present in your source tree, run:
81+
```
82+
git submodule update --init
83+
```
84+
6785
# License
6886

6987
This project is licensed under the Apache 2.0 license with the LLVM exception.

crates/witx-bindgen/src/lib.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,21 @@ fn render_enum(src: &mut String, name: &str, e: &EnumDatatype) {
172172
}
173173

174174
if name == "errno" {
175-
src.push_str("pub(crate) fn strerror(code: u16) -> &'static str {");
175+
src.push_str("pub fn errno_name(code: u16) -> &'static str {");
176+
src.push_str("match code {");
177+
for variant in e.variants.iter() {
178+
src.push_str(&name.to_shouty_snake_case());
179+
src.push_str("_");
180+
src.push_str(&variant.name.as_str().to_shouty_snake_case());
181+
src.push_str(" => \"");
182+
src.push_str(&variant.name.as_str().to_shouty_snake_case());
183+
src.push_str("\",");
184+
}
185+
src.push_str("_ => \"Unknown error.\",");
186+
src.push_str("}");
187+
src.push_str("}");
188+
189+
src.push_str("pub fn errno_docs(code: u16) -> &'static str {");
176190
src.push_str("match code {");
177191
for variant in e.variants.iter() {
178192
src.push_str(&name.to_shouty_snake_case());

src/error.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ impl fmt::Display for Error {
2929
write!(
3030
f,
3131
"{} (error {})",
32-
super::strerror(self.code.get()),
32+
super::errno_name(self.code.get()),
3333
self.code
3434
)?;
3535
Ok(())
@@ -40,7 +40,8 @@ impl fmt::Debug for Error {
4040
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4141
f.debug_struct("Error")
4242
.field("code", &self.code)
43-
.field("message", &super::strerror(self.code.get()))
43+
.field("name", &super::errno_name(self.code.get()))
44+
.field("message", &super::errno_docs(self.code.get()))
4445
.finish()
4546
}
4647
}

src/lib_generated.rs

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,89 @@ pub const ERRNO_TXTBSY: Errno = 74;
177177
pub const ERRNO_XDEV: Errno = 75;
178178
/// Extension: Capabilities insufficient.
179179
pub const ERRNO_NOTCAPABLE: Errno = 76;
180-
pub(crate) fn strerror(code: u16) -> &'static str {
180+
pub fn errno_name(code: u16) -> &'static str {
181+
match code {
182+
ERRNO_SUCCESS => "SUCCESS",
183+
ERRNO_2BIG => "2BIG",
184+
ERRNO_ACCES => "ACCES",
185+
ERRNO_ADDRINUSE => "ADDRINUSE",
186+
ERRNO_ADDRNOTAVAIL => "ADDRNOTAVAIL",
187+
ERRNO_AFNOSUPPORT => "AFNOSUPPORT",
188+
ERRNO_AGAIN => "AGAIN",
189+
ERRNO_ALREADY => "ALREADY",
190+
ERRNO_BADF => "BADF",
191+
ERRNO_BADMSG => "BADMSG",
192+
ERRNO_BUSY => "BUSY",
193+
ERRNO_CANCELED => "CANCELED",
194+
ERRNO_CHILD => "CHILD",
195+
ERRNO_CONNABORTED => "CONNABORTED",
196+
ERRNO_CONNREFUSED => "CONNREFUSED",
197+
ERRNO_CONNRESET => "CONNRESET",
198+
ERRNO_DEADLK => "DEADLK",
199+
ERRNO_DESTADDRREQ => "DESTADDRREQ",
200+
ERRNO_DOM => "DOM",
201+
ERRNO_DQUOT => "DQUOT",
202+
ERRNO_EXIST => "EXIST",
203+
ERRNO_FAULT => "FAULT",
204+
ERRNO_FBIG => "FBIG",
205+
ERRNO_HOSTUNREACH => "HOSTUNREACH",
206+
ERRNO_IDRM => "IDRM",
207+
ERRNO_ILSEQ => "ILSEQ",
208+
ERRNO_INPROGRESS => "INPROGRESS",
209+
ERRNO_INTR => "INTR",
210+
ERRNO_INVAL => "INVAL",
211+
ERRNO_IO => "IO",
212+
ERRNO_ISCONN => "ISCONN",
213+
ERRNO_ISDIR => "ISDIR",
214+
ERRNO_LOOP => "LOOP",
215+
ERRNO_MFILE => "MFILE",
216+
ERRNO_MLINK => "MLINK",
217+
ERRNO_MSGSIZE => "MSGSIZE",
218+
ERRNO_MULTIHOP => "MULTIHOP",
219+
ERRNO_NAMETOOLONG => "NAMETOOLONG",
220+
ERRNO_NETDOWN => "NETDOWN",
221+
ERRNO_NETRESET => "NETRESET",
222+
ERRNO_NETUNREACH => "NETUNREACH",
223+
ERRNO_NFILE => "NFILE",
224+
ERRNO_NOBUFS => "NOBUFS",
225+
ERRNO_NODEV => "NODEV",
226+
ERRNO_NOENT => "NOENT",
227+
ERRNO_NOEXEC => "NOEXEC",
228+
ERRNO_NOLCK => "NOLCK",
229+
ERRNO_NOLINK => "NOLINK",
230+
ERRNO_NOMEM => "NOMEM",
231+
ERRNO_NOMSG => "NOMSG",
232+
ERRNO_NOPROTOOPT => "NOPROTOOPT",
233+
ERRNO_NOSPC => "NOSPC",
234+
ERRNO_NOSYS => "NOSYS",
235+
ERRNO_NOTCONN => "NOTCONN",
236+
ERRNO_NOTDIR => "NOTDIR",
237+
ERRNO_NOTEMPTY => "NOTEMPTY",
238+
ERRNO_NOTRECOVERABLE => "NOTRECOVERABLE",
239+
ERRNO_NOTSOCK => "NOTSOCK",
240+
ERRNO_NOTSUP => "NOTSUP",
241+
ERRNO_NOTTY => "NOTTY",
242+
ERRNO_NXIO => "NXIO",
243+
ERRNO_OVERFLOW => "OVERFLOW",
244+
ERRNO_OWNERDEAD => "OWNERDEAD",
245+
ERRNO_PERM => "PERM",
246+
ERRNO_PIPE => "PIPE",
247+
ERRNO_PROTO => "PROTO",
248+
ERRNO_PROTONOSUPPORT => "PROTONOSUPPORT",
249+
ERRNO_PROTOTYPE => "PROTOTYPE",
250+
ERRNO_RANGE => "RANGE",
251+
ERRNO_ROFS => "ROFS",
252+
ERRNO_SPIPE => "SPIPE",
253+
ERRNO_SRCH => "SRCH",
254+
ERRNO_STALE => "STALE",
255+
ERRNO_TIMEDOUT => "TIMEDOUT",
256+
ERRNO_TXTBSY => "TXTBSY",
257+
ERRNO_XDEV => "XDEV",
258+
ERRNO_NOTCAPABLE => "NOTCAPABLE",
259+
_ => "Unknown error.",
260+
}
261+
}
262+
pub fn errno_docs(code: u16) -> &'static str {
181263
match code {
182264
ERRNO_SUCCESS => "No error occurred. System call completed successfully.",
183265
ERRNO_2BIG => "Argument list too long.",

0 commit comments

Comments
 (0)