-
-
Notifications
You must be signed in to change notification settings - Fork 697
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: optional named arguments for #[server] macro (#1904)
- Loading branch information
Showing
7 changed files
with
299 additions
and
18 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
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,96 @@ | ||
#[cfg(test)] | ||
use cfg_if::cfg_if; | ||
|
||
cfg_if! { | ||
if #[cfg(not(feature = "ssr"))] { | ||
use leptos::{server, server_fn::Encoding, ServerFnError}; | ||
|
||
#[test] | ||
fn server_default() { | ||
#[server] | ||
pub async fn my_server_action() -> Result<(), ServerFnError> { | ||
Ok(()) | ||
} | ||
assert_eq!(MyServerAction::PREFIX, "/api"); | ||
assert_eq!(&MyServerAction::URL[0..16], "my_server_action"); | ||
assert_eq!(MyServerAction::ENCODING, Encoding::Url); | ||
} | ||
|
||
#[test] | ||
fn server_full_legacy() { | ||
#[server(FooBar, "/foo/bar", "Cbor", "my_path")] | ||
pub async fn my_server_action() -> Result<(), ServerFnError> { | ||
Ok(()) | ||
} | ||
assert_eq!(FooBar::PREFIX, "/foo/bar"); | ||
assert_eq!(FooBar::URL, "my_path"); | ||
assert_eq!(FooBar::ENCODING, Encoding::Cbor); | ||
} | ||
|
||
#[test] | ||
fn server_all_keywords() { | ||
#[server(endpoint = "my_path", encoding = "Cbor", prefix = "/foo/bar", name = FooBar)] | ||
pub async fn my_server_action() -> Result<(), ServerFnError> { | ||
Ok(()) | ||
} | ||
assert_eq!(FooBar::PREFIX, "/foo/bar"); | ||
assert_eq!(FooBar::URL, "my_path"); | ||
assert_eq!(FooBar::ENCODING, Encoding::Cbor); | ||
} | ||
|
||
#[test] | ||
fn server_mix() { | ||
#[server(FooBar, endpoint = "my_path")] | ||
pub async fn my_server_action() -> Result<(), ServerFnError> { | ||
Ok(()) | ||
} | ||
assert_eq!(FooBar::PREFIX, "/api"); | ||
assert_eq!(FooBar::URL, "my_path"); | ||
assert_eq!(FooBar::ENCODING, Encoding::Url); | ||
} | ||
|
||
#[test] | ||
fn server_name() { | ||
#[server(name = FooBar)] | ||
pub async fn my_server_action() -> Result<(), ServerFnError> { | ||
Ok(()) | ||
} | ||
assert_eq!(FooBar::PREFIX, "/api"); | ||
assert_eq!(&FooBar::URL[0..16], "my_server_action"); | ||
assert_eq!(FooBar::ENCODING, Encoding::Url); | ||
} | ||
|
||
#[test] | ||
fn server_prefix() { | ||
#[server(prefix = "/foo/bar")] | ||
pub async fn my_server_action() -> Result<(), ServerFnError> { | ||
Ok(()) | ||
} | ||
assert_eq!(MyServerAction::PREFIX, "/foo/bar"); | ||
assert_eq!(&MyServerAction::URL[0..16], "my_server_action"); | ||
assert_eq!(MyServerAction::ENCODING, Encoding::Url); | ||
} | ||
|
||
#[test] | ||
fn server_encoding() { | ||
#[server(encoding = "GetJson")] | ||
pub async fn my_server_action() -> Result<(), ServerFnError> { | ||
Ok(()) | ||
} | ||
assert_eq!(MyServerAction::PREFIX, "/api"); | ||
assert_eq!(&MyServerAction::URL[0..16], "my_server_action"); | ||
assert_eq!(MyServerAction::ENCODING, Encoding::GetJSON); | ||
} | ||
|
||
#[test] | ||
fn server_endpoint() { | ||
#[server(endpoint = "/path/to/my/endpoint")] | ||
pub async fn my_server_action() -> Result<(), ServerFnError> { | ||
Ok(()) | ||
} | ||
assert_eq!(MyServerAction::PREFIX, "/api"); | ||
assert_eq!(MyServerAction::URL, "/path/to/my/endpoint"); | ||
assert_eq!(MyServerAction::ENCODING, Encoding::Url); | ||
} | ||
} | ||
} |
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,42 @@ | ||
use leptos::*; | ||
|
||
#[server(endpoint = "my_path", FooBar)] | ||
pub async fn positional_argument_follows_keyword_argument() -> Result<(), ServerFnError> { | ||
Ok(()) | ||
} | ||
|
||
#[server(endpoint = "first", endpoint = "second")] | ||
pub async fn keyword_argument_repeated() -> Result<(), ServerFnError> { | ||
Ok(()) | ||
} | ||
|
||
#[server(Foo, Bar)] | ||
pub async fn expected_string_literal() -> Result<(), ServerFnError> { | ||
Ok(()) | ||
} | ||
#[server(Foo, Bar, bazz)] | ||
pub async fn expected_string_literal_2() -> Result<(), ServerFnError> { | ||
Ok(()) | ||
} | ||
|
||
#[server("Foo")] | ||
pub async fn expected_identifier() -> Result<(), ServerFnError> { | ||
Ok(()) | ||
} | ||
|
||
#[server(Foo Bar)] | ||
pub async fn expected_comma() -> Result<(), ServerFnError> { | ||
Ok(()) | ||
} | ||
|
||
#[server(FooBar, "/foo/bar", "Cbor", "my_path", "extra")] | ||
pub async fn unexpected_extra_argument() -> Result<(), ServerFnError> { | ||
Ok(()) | ||
} | ||
|
||
#[server(encoding = "wrong")] | ||
pub async fn encoding_not_found() -> Result<(), ServerFnError> { | ||
Ok(()) | ||
} | ||
|
||
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,47 @@ | ||
error: positional argument follows keyword argument | ||
--> tests/ui/server.rs:3:32 | ||
| | ||
3 | #[server(endpoint = "my_path", FooBar)] | ||
| ^^^^^^ | ||
|
||
error: keyword argument repeated: endpoint | ||
--> tests/ui/server.rs:8:30 | ||
| | ||
8 | #[server(endpoint = "first", endpoint = "second")] | ||
| ^^^^^^^^ | ||
|
||
error: expected string literal | ||
--> tests/ui/server.rs:13:15 | ||
| | ||
13 | #[server(Foo, Bar)] | ||
| ^^^ | ||
|
||
error: expected string literal | ||
--> tests/ui/server.rs:17:15 | ||
| | ||
17 | #[server(Foo, Bar, bazz)] | ||
| ^^^ | ||
|
||
error: expected identifier | ||
--> tests/ui/server.rs:22:10 | ||
| | ||
22 | #[server("Foo")] | ||
| ^^^^^ | ||
|
||
error: expected `,` | ||
--> tests/ui/server.rs:27:14 | ||
| | ||
27 | #[server(Foo Bar)] | ||
| ^^^ | ||
|
||
error: unexpected extra argument | ||
--> tests/ui/server.rs:32:49 | ||
| | ||
32 | #[server(FooBar, "/foo/bar", "Cbor", "my_path", "extra")] | ||
| ^^^^^^^ | ||
|
||
error: Encoding Not Found | ||
--> tests/ui/server.rs:37:21 | ||
| | ||
37 | #[server(encoding = "wrong")] | ||
| ^^^^^^^ |