-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from NREL/f2-doc-fields
F2 doc fields
- Loading branch information
Showing
20 changed files
with
621 additions
and
396 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
(cd rust/ && cargo test) && \ | ||
(cd rust/fastsim-core/ && cargo test) && \ | ||
(cd rust/fastsim-cli/ && cargo test) && \ | ||
pip install -qe ".[dev]" && \ | ||
pytest -v python/fastsim/tests/ |
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "fastsim-core" | ||
version = "0.1.3" | ||
version = "0.1.4" | ||
edition = "2021" | ||
license = "Apache-2.0" | ||
authors = ["NREL/MTES/CIMS/MBAP Group <[email protected]>"] | ||
|
@@ -10,7 +10,10 @@ readme = "../../README.md" | |
repository = "https://github.com/NREL/fastsim" | ||
|
||
[dependencies] | ||
proc-macros = { package = "fastsim-proc-macros", version = "~0" } | ||
# uncomment next line for any development work in fastsim-proc-macros | ||
proc-macros = { package = "fastsim-proc-macros", path = "./fastsim-proc-macros" } | ||
# comment next line for any development work in fastsim-proc-macros | ||
# proc-macros = { package = "fastsim-proc-macros", version = "0.1.4" } | ||
pyo3 = { workspace = true, features = [ | ||
"extension-module", | ||
"anyhow", | ||
|
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
[package] | ||
authors = ["NREL/MTES/CIMS/MBAP Group <[email protected]>"] | ||
name = "fastsim-proc-macros" | ||
version = "0.1.3" | ||
version = "0.1.4" | ||
edition = "2021" | ||
license = "Apache-2.0" | ||
readme = "../../../README.md" | ||
|
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
160 changes: 160 additions & 0 deletions
160
rust/fastsim-core/fastsim-proc-macros/src/add_pyo3_api/pyo3_api_utils.rs
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,160 @@ | ||
macro_rules! impl_vec_get_set { | ||
($opts: ident, $fident: ident, $impl_block: ident, $contained_type: ty, $wrapper_type: expr, $has_orphaned: expr) => { | ||
if !$opts.skip_get { | ||
let get_name: TokenStream2 = format!("get_{}", $fident).parse().unwrap(); | ||
$impl_block.extend::<TokenStream2>(quote! { | ||
#[getter] | ||
pub fn #get_name(&self) -> PyResult<$wrapper_type> { | ||
Ok($wrapper_type::new(self.#$fident.clone())) | ||
} | ||
}); | ||
} | ||
if !$opts.skip_set { | ||
let set_name: TokenStream2 = format!("set_{}", $fident).parse().unwrap(); | ||
match stringify!($wrapper_type) { | ||
"Pyo3VecF64" => { | ||
if $has_orphaned { | ||
$impl_block.extend(quote! { | ||
#[setter] | ||
pub fn #set_name(&mut self, new_value: Vec<$contained_type>) -> PyResult<()> { | ||
if !self.orphaned { | ||
self.#$fident = new_value; | ||
Ok(()) | ||
} else { | ||
Err(PyAttributeError::new_err(crate::utils::NESTED_STRUCT_ERR)) | ||
} | ||
} | ||
}) | ||
} else { | ||
$impl_block.extend(quote! { | ||
#[setter] | ||
pub fn #set_name(&mut self, new_value: Vec<$contained_type>) -> PyResult<()> { | ||
self.#$fident = new_value; | ||
Ok(()) | ||
} | ||
}) | ||
} | ||
} | ||
_ => { | ||
if $has_orphaned { | ||
$impl_block.extend(quote! { | ||
#[setter] | ||
pub fn #set_name(&mut self, new_value: Vec<$contained_type>) -> PyResult<()> { | ||
if !self.orphaned { | ||
self.#$fident = Array1::from_vec(new_value); | ||
Ok(()) | ||
} else { | ||
Err(PyAttributeError::new_err(crate::utils::NESTED_STRUCT_ERR)) | ||
} | ||
} | ||
}) | ||
} else { | ||
$impl_block.extend(quote! { | ||
#[setter] | ||
pub fn #set_name(&mut self, new_value: Vec<$contained_type>) -> PyResult<()> { | ||
self.#$fident = Array1::from_vec(new_value); | ||
Ok(()) | ||
} | ||
}) | ||
} | ||
} | ||
} | ||
|
||
} | ||
}; | ||
} | ||
|
||
/// Generates pyo3 getter methods | ||
/// | ||
/// general match arguments: | ||
/// - type: type of variable (e.g. `f64`) | ||
/// - field: struct field | ||
/// - impl_block: TokenStream2 | ||
/// - opts: FieldOptions struct instance | ||
macro_rules! impl_get_body { | ||
( | ||
$type: ident, $field: ident, $impl_block: ident, $opts: ident | ||
) => { | ||
if !$opts.skip_get { | ||
let get_name: TokenStream2 = format!("get_{}", $field).parse().unwrap(); | ||
let get_block = if $opts.field_has_orphaned { | ||
quote! { | ||
#[getter] | ||
pub fn #get_name(&mut self) -> PyResult<#$type> { | ||
self.#$field.orphaned = true; | ||
Ok(self.#$field.clone()) | ||
} | ||
} | ||
} else { | ||
quote! { | ||
#[getter] | ||
pub fn #get_name(&self) -> PyResult<#$type> { | ||
Ok(self.#$field.clone()) | ||
} | ||
} | ||
}; | ||
$impl_block.extend::<TokenStream2>(get_block) | ||
} | ||
}; | ||
} | ||
|
||
/// Generates pyo3 setter methods | ||
/// | ||
/// general match arguments: | ||
/// - type: type of variable (e.g. `f64`) | ||
/// - field: struct field | ||
/// - impl_block: TokenStream2 | ||
/// - has_orphaned: bool, true if struct has `orphaned` field | ||
/// - opts: FieldOptions struct instance | ||
|
||
macro_rules! impl_set_body { | ||
( // for generic | ||
$type: ident, $field: ident, $impl_block: ident, $has_orphaned: expr, $opts: ident | ||
) => { | ||
if !$opts.skip_set { | ||
let set_name: TokenStream2 = format!("set_{}", $field).parse().unwrap(); | ||
let orphaned_set_block = if $has_orphaned && $opts.field_has_orphaned { | ||
quote! { | ||
if !self.orphaned { | ||
self.#$field = new_value; | ||
self.#$field.orphaned = false; | ||
Ok(()) | ||
} else { | ||
Err(PyAttributeError::new_err(crate::utils::NESTED_STRUCT_ERR)) | ||
} | ||
} | ||
} else if $has_orphaned { | ||
quote! { | ||
if !self.orphaned { | ||
self.#$field = new_value; | ||
Ok(()) | ||
} else { | ||
Err(PyAttributeError::new_err(crate::utils::NESTED_STRUCT_ERR)) | ||
} | ||
} | ||
} else { | ||
quote! { | ||
self.#$field = new_value; | ||
Ok(()) | ||
} | ||
}; | ||
|
||
$impl_block.extend::<TokenStream2>(quote! { | ||
#[setter] | ||
pub fn #set_name(&mut self, new_value: #$type) -> PyResult<()> { | ||
#orphaned_set_block | ||
} | ||
}); | ||
} | ||
}; | ||
} | ||
|
||
#[derive(Debug, Default, Clone)] | ||
pub struct FieldOptions { | ||
/// if true, getters are not generated for a field | ||
pub skip_get: bool, | ||
/// if true, setters are not generated for a field | ||
pub skip_set: bool, | ||
/// if true, current field is itself a struct with `orphaned` field | ||
pub field_has_orphaned: bool, | ||
} |
2 changes: 2 additions & 0 deletions
2
rust/fastsim-core/fastsim-proc-macros/src/approx_eq_derive.rs
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
Oops, something went wrong.