Skip to content

Commit c3c950c

Browse files
committed
Inline example code in book
This commit inlines some of the example code in the book. This makes it easier to read book chapters using any text viewer, as opposed to needing to use `mdbook`.
1 parent 75a1077 commit c3c950c

File tree

2 files changed

+77
-12
lines changed

2 files changed

+77
-12
lines changed

book/src/contributing/adding-compile-time-errors/README.md

+77-8
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,25 @@ guide them towards the right fix.
66
For example, if a user wrote the following bridge module:
77

88
```rust
9-
{{#include ../../../../crates/swift-bridge-macro/tests/ui/unrecognized-opaque-type-attribute.rs:mdbook-ui-test-example}}
9+
#[swift_bridge::bridge]
10+
mod ffi {
11+
extern "Rust" {
12+
#[swift_bridge(InvalidAttribute)]
13+
type SomeType;
14+
}
15+
}
16+
17+
pub struct SomeType;
1018
```
1119

1220
We would want to emit a compile time error along the lines of:
1321

1422
```sh
15-
{{#include ../../../../crates/swift-bridge-macro/tests/ui/unrecognized-opaque-type-attribute.stderr}}
23+
error: Unrecognized attribute "InvalidAttribute".
24+
--> tests/ui/unrecognized-opaque-type-attribute.rs:8:24
25+
|
26+
8 | #[swift_bridge(InvalidAttribute)]
27+
| ^^^^^^^^^^^^^^^^
1628
```
1729

1830
This chapter shows how to add support for compile time errors.
@@ -30,9 +42,31 @@ Here are a few example parse errors:
3042
```rust
3143
// via: crates/swift-bridge-ir/src/errors/parse_error.rs
3244

33-
{{#include ../../../../crates/swift-bridge-ir/src/errors/parse_error.rs:mdbook-parse-error-enum}}
34-
35-
// ...
45+
pub(crate) enum ParseError {
46+
ArgsIntoArgNotFound {
47+
func: ForeignItemFn,
48+
missing_arg: Ident,
49+
},
50+
/// `extern {}`
51+
AbiNameMissing {
52+
/// `extern {}`
53+
/// ------
54+
extern_token: Token![extern],
55+
},
56+
/// `extern "Foo" {}`
57+
AbiNameInvalid {
58+
/// `extern "Foo" {}`
59+
/// -----
60+
abi_name: LitStr,
61+
},
62+
/// `fn foo (&self)`
63+
/// ----
64+
AmbiguousSelf { self_: Receiver },
65+
/// fn foo (bar: &Bar);
66+
/// If Bar wasn't declared using a `type Bar` declaration.
67+
UndeclaredType { ty: Type },
68+
69+
// ... snip ...
3670
}
3771
```
3872

@@ -42,9 +76,44 @@ Here are a few examples:
4276
````rust
4377
// via: crates/swift-bridge-ir/src/errors/parse_error.rs
4478

45-
{{#include ../../../../crates/swift-bridge-ir/src/errors/parse_error.rs:mdbook-parse-error-message}}
46-
47-
// ...
79+
impl Into<syn::Error> for ParseError {
80+
fn into(self) -> Error {
81+
match self {
82+
ParseError::AbiNameMissing {
83+
extern_token: extern_ident,
84+
} => Error::new_spanned(
85+
extern_ident,
86+
format!(
87+
r#"extern modules must have their abi set to "Rust" or "Swift".
88+
```
89+
extern "Rust" {{ ... }}
90+
extern "Swift" {{ ... }}
91+
```
92+
"#
93+
),
94+
),
95+
ParseError::UndeclaredType { ty } => {
96+
let ty_name = ty.to_token_stream().to_string();
97+
let ty_name = ty_name.split_whitespace().last().unwrap();
98+
99+
let message = format!(
100+
r#"Type must be declared with `type {}`.
101+
"#,
102+
ty_name
103+
);
104+
Error::new_spanned(ty, message)
105+
}
106+
ParseError::DeclaredBuiltInType { ty } => {
107+
let message = format!(
108+
r#"Type {} is already supported
109+
"#,
110+
ty.to_token_stream().to_string()
111+
);
112+
Error::new_spanned(ty, message)
113+
}
114+
115+
// ... snip ...
116+
}
48117
}
49118
}
50119
````

crates/swift-bridge-ir/src/errors/parse_error.rs

-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use syn::{Error, FnArg, Item, Receiver};
44
use syn::{ForeignItemFn, ForeignItemType, LitStr};
55
use syn::{Token, Type};
66

7-
// <!-- ANCHOR: mdbook-parse-error-enum -->
87
pub(crate) enum ParseError {
98
ArgsIntoArgNotFound {
109
func: ForeignItemFn,
@@ -28,7 +27,6 @@ pub(crate) enum ParseError {
2827
/// fn foo (bar: &Bar);
2928
/// If Bar wasn't declared using a `type Bar` declaration.
3029
UndeclaredType { ty: Type },
31-
// <!-- ANCHOR_END: mdbook-parse-error-enum -->
3230
/// Declared a type that we already support.
3331
/// Example: `type u32`
3432
DeclaredBuiltInType { ty: ForeignItemType },
@@ -73,7 +71,6 @@ pub(crate) enum IdentifiableParseError {
7371
MissingReturnType { fn_ident: Ident },
7472
}
7573

76-
// <!-- ANCHOR: mdbook-parse-error-message -->
7774
impl Into<syn::Error> for ParseError {
7875
fn into(self) -> Error {
7976
match self {
@@ -122,7 +119,6 @@ self: &mut SomeType
122119
);
123120
Error::new_spanned(ty, message)
124121
}
125-
// <!-- ANCHOR_END: mdbook-parse-error-message -->
126122
ParseError::DeclaredBuiltInType { ty } => {
127123
let message = format!(
128124
r#"Type {} is already supported

0 commit comments

Comments
 (0)