forked from extendr/extendr
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extendr-macro: moved
ExtendrOptions
around as it was scattered..
- Loading branch information
Showing
6 changed files
with
93 additions
and
85 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,65 @@ | ||
use syn::{meta::ParseNestedMeta, Lit, LitBool}; | ||
|
||
#[derive(Debug, Default)] | ||
pub struct ExtendrOptions { | ||
pub use_try_from: bool, | ||
pub r_name: Option<String>, | ||
pub mod_name: Option<String>, | ||
pub use_rng: bool, | ||
/// For `#[extendr]` on `enum`s, there is a wrapper around `Vec<EnumType>` | ||
/// generated; Default to `VecEnumtype`. | ||
pub vec_name: Option<String>, | ||
} | ||
|
||
impl ExtendrOptions { | ||
/// Parse a set of attribute arguments for `#[extendr(opts...)]` | ||
/// | ||
/// Supported options: | ||
/// | ||
/// - `use_try_from = bool` which uses `TryFrom<Robj>` for argument conversions. | ||
/// - `r_name = "name"` which specifies the name of the wrapper on the R-side. | ||
/// - `use_rng = bool` ensures the RNG-state is pulled and pushed | ||
/// | ||
pub fn parse(&mut self, meta: ParseNestedMeta) -> syn::parse::Result<()> { | ||
fn help_message() -> ! { | ||
panic!("expected #[extendr(use_try_from = bool, r_name = \"name\", mod_name = \"r_mod_name\", use_rng = bool)]"); | ||
} | ||
|
||
let value = match meta.value() { | ||
Ok(value) => value, | ||
Err(_) => help_message(), | ||
}; | ||
|
||
if meta.path.is_ident("use_try_from") { | ||
if let Ok(LitBool { value, .. }) = value.parse() { | ||
self.use_try_from = value; | ||
Ok(()) | ||
} else { | ||
help_message(); | ||
} | ||
} else if meta.path.is_ident("r_name") { | ||
if let Ok(Lit::Str(litstr)) = value.parse() { | ||
self.r_name = Some(litstr.value()); | ||
Ok(()) | ||
} else { | ||
help_message(); | ||
} | ||
} else if meta.path.is_ident("mod_name") { | ||
if let Ok(Lit::Str(litstr)) = value.parse() { | ||
self.mod_name = Some(litstr.value()); | ||
Ok(()) | ||
} else { | ||
help_message(); | ||
} | ||
} else if meta.path.is_ident("use_rng") { | ||
if let Ok(LitBool { value, .. }) = value.parse() { | ||
self.use_rng = value; | ||
Ok(()) | ||
} else { | ||
help_message(); | ||
} | ||
} else { | ||
help_message(); | ||
} | ||
} | ||
} |
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