Skip to content

Commit

Permalink
freyja_main! usability improvements (#116)
Browse files Browse the repository at this point in the history
- re-export the `freyja_common` crate so that users don't need to manually reference it (I tried to do this with `tokio` too but couldn't get it to work)
- add some validation to the macro to ensure you don't write something functionally useless
  • Loading branch information
wilyle authored Jan 23, 2024
1 parent 2c0b058 commit f324c42
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
3 changes: 2 additions & 1 deletion freyja/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
// Licensed under the MIT license.
// SPDX-License-Identifier: MIT

// Re-export this macro for convenience so users don't need to manually import the proc_macros crate
// Re-export these items for convenience so users don't need to manually import them
pub use freyja_common;
pub use proc_macros::freyja_main;

mod cartographer;
Expand Down
2 changes: 1 addition & 1 deletion proc_macros/src/freyja_main/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub(crate) fn generate(ir: FreyjaMainOutput) -> TokenStream {
quote! {
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
use freyja_common::data_adapter::DataAdapterFactory;
use freyja::freyja_common::data_adapter::DataAdapterFactory;
let factories: Vec<Box<dyn DataAdapterFactory + Send + Sync>> = vec![
#(Box::new(
#data_adapter_factory_types::create_new()
Expand Down
17 changes: 16 additions & 1 deletion proc_macros/src/freyja_main/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ impl Parse for FreyjaMainArgs {
Punctuated::<Ident, Token![,]>::parse_terminated(&data_adapter_content)
.unwrap()
.into_iter()
.collect();
.collect::<Vec<_>>();

if data_adapter_factory_types.is_empty() {
panic!("At least one DataAdapterFactory is required");
}

let trailing_comma_result = if !input.is_empty() {
Some(input.parse::<Token![,]>())
Expand Down Expand Up @@ -144,4 +148,15 @@ mod freyja_main_parse_tests {
let result = catch_unwind(|| parse(input));
assert!(result.is_err());
}

#[test]
fn parse_panics_with_empty_factory_list() {
let foo_ident = format_ident!("Foo");
let bar_ident = format_ident!("Bar");
let baz_ident = format_ident!("Baz");

let input = quote! { #foo_ident, #bar_ident, #baz_ident, [], };
let result = catch_unwind(|| parse(input));
assert!(result.is_err());
}
}

0 comments on commit f324c42

Please sign in to comment.