-
Notifications
You must be signed in to change notification settings - Fork 943
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(swarm-derive): better error reporting for invalid attributes
This PR refactors the error reporting away from panicking to returning `syn::Result` and adds two unit tests for the parsing of attributes that users interact with. Pull-Request: #3922.
- Loading branch information
1 parent
0bc724a
commit adcc10b
Showing
6 changed files
with
82 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use syn::{Expr, ExprLit, Lit}; | ||
|
||
pub(crate) trait RequireStrLit { | ||
fn require_str_lit(&self) -> syn::Result<String>; | ||
} | ||
|
||
impl RequireStrLit for Expr { | ||
fn require_str_lit(&self) -> syn::Result<String> { | ||
match self { | ||
Expr::Lit(ExprLit { | ||
lit: Lit::Str(str), .. | ||
}) => Ok(str.value()), | ||
_ => Err(syn::Error::new_spanned(self, "expected a string literal")), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
use libp2p_ping as ping; | ||
|
||
#[derive(libp2p_swarm::NetworkBehaviour)] | ||
#[behaviour(prelude = libp2p_swarm::derive_prelude)] | ||
struct Foo { | ||
ping: ping::Behaviour, | ||
} | ||
|
||
fn main() { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
error: expected a string literal | ||
--> tests/ui/fail/prelude_not_string.rs:4:23 | ||
| | ||
4 | #[behaviour(prelude = libp2p_swarm::derive_prelude)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
use libp2p_ping as ping; | ||
|
||
#[derive(libp2p_swarm::NetworkBehaviour)] | ||
#[behaviour(out_event = FooEvent, prelude = "libp2p_swarm::derive_prelude")] | ||
struct Foo { | ||
ping: ping::Behaviour, | ||
} | ||
|
||
struct FooEvent; | ||
|
||
impl From<ping::Event> for FooEvent { | ||
fn from(_: ping::Event) -> Self { | ||
unimplemented!() | ||
} | ||
} | ||
|
||
fn main() { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
error: expected a string literal | ||
--> tests/ui/fail/to_swarm_not_string.rs:4:25 | ||
| | ||
4 | #[behaviour(out_event = FooEvent, prelude = "libp2p_swarm::derive_prelude")] | ||
| ^^^^^^^^ |