Skip to content

Commit 516269e

Browse files
committed
Squashed commit of the following:
commit aa9b119 Author: Jan Range <[email protected]> Date: Tue Apr 29 14:58:37 2025 +0200 Property macro (#12) * remove unused print statements * update deps * add `property` macros * generate setter and getter methods via `property` * encapsulate url for docs * use inline code fence for docs * remove newline * update docs * update docs * fix clippy issue
1 parent 172e06c commit 516269e

21 files changed

+1439
-924
lines changed

.cursorignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
target/
21
vendors/
32
.clippy_output.txt
43
tarpaulin-report.html

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"cmake.sourceDirectory": "/Users/janrange/Documents/playground/libsbml-sys/vendors/libsbml"
3+
}

Cargo.lock

Lines changed: 4 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ cxx = "1.0.140"
99
paste = "1.0.15"
1010
quick-xml = { version = "0.37.2", features = ["serialize"] }
1111
serde = { version = "1.0.217", features = ["derive"] }
12-
thiserror = "2.0.12"
1312

1413

1514
[build-dependencies]

build.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ fn main() -> miette::Result<()> {
8787
/// * `miette::Result<String>` - Build directory path on success, error on failure
8888
fn build_and_link_libsbml(dep_build: &str) -> miette::Result<String> {
8989
let dst = if cfg!(target_os = "windows") {
90-
println!("cargo:warning=Building libSBML for Windows");
9190
// In order to build for windows, we need to carefully tell CMake
9291
// where to find the libraries and headers for libexpat and zlib.
9392
// This is necessary because the libraries are not installed in the
@@ -121,7 +120,6 @@ fn build_and_link_libsbml(dep_build: &str) -> miette::Result<String> {
121120
.define("BUILD_SHARED_LIBS", "OFF")
122121
.build()
123122
} else {
124-
println!("cargo:warning=Building libSBML for MacOS/Linux");
125123
// When building for MacOS and Linux, we can just use the system libraries
126124
cmake::Config::new(LIBSBML_PATH)
127125
.profile("Release")
@@ -172,8 +170,6 @@ fn build_and_link_libsbml(dep_build: &str) -> miette::Result<String> {
172170
/// # Returns
173171
/// * `miette::Result<String>` - Build directory path on success, error on failure
174172
fn build_and_link_sbml_deps() -> miette::Result<String> {
175-
println!("cargo:warning=Building libSBML dependencies");
176-
177173
// Build the dependencies for libSBML
178174
// We hard-code to EXPAT and ZLIB for now, but in the future this should
179175
// be made more flexible.

src/compartment.rs

Lines changed: 75 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@
99
1010
use std::{cell::RefCell, pin::Pin, rc::Rc};
1111

12-
use autocxx::c_uint;
1312
use cxx::let_cxx_string;
1413

1514
use crate::{
16-
clone, inner, into_id, model::Model, pin_ptr, sbmlcxx, sbo_term, traits::fromptr::FromPtr,
17-
upcast_annotation,
15+
clone, inner, into_id, model::Model, optional_property, pin_ptr, required_property, sbmlcxx,
16+
sbo_term, traits::fromptr::FromPtr, upcast_annotation,
1817
};
1918

2019
/// A safe wrapper around the libSBML Compartment class.
@@ -61,145 +60,64 @@ impl<'a> Compartment<'a> {
6160
}
6261
}
6362

64-
/// Gets the compartment's identifier.
65-
///
66-
/// # Returns
67-
/// The compartment's ID as a String
68-
pub fn id(&self) -> String {
69-
self.inner.borrow().getId().to_str().unwrap().to_string()
70-
}
71-
72-
/// Sets the compartment's identifier.
73-
///
74-
/// # Arguments
75-
/// * `id` - The new identifier to set
76-
pub fn set_id(&self, id: &str) {
77-
let_cxx_string!(id = id);
78-
self.inner.borrow_mut().as_mut().setId(&id);
79-
}
80-
81-
/// Gets the compartment's name.
82-
///
83-
/// # Returns
84-
/// The compartment's name as a String
85-
pub fn name(&self) -> String {
86-
self.inner.borrow().getName().to_str().unwrap().to_string()
87-
}
88-
89-
/// Sets the compartment's name.
90-
///
91-
/// # Arguments
92-
/// * `name` - The new name to set
93-
pub fn set_name(&self, name: &str) {
94-
let_cxx_string!(name = name);
95-
self.inner.borrow_mut().as_mut().setName(&name);
96-
}
97-
98-
/// Gets the spatial dimensions of the compartment.
99-
///
100-
/// # Returns
101-
/// The number of spatial dimensions as a u32
102-
pub fn spatial_dimensions(&self) -> u32 {
103-
self.inner.borrow().getSpatialDimensions().0
104-
}
105-
106-
/// Sets the spatial dimensions of the compartment.
107-
///
108-
/// # Arguments
109-
/// * `spatial_dimensions` - The number of spatial dimensions to set (typically 0-3)
110-
pub fn set_spatial_dimensions(&self, spatial_dimensions: u32) {
111-
self.inner
112-
.borrow_mut()
113-
.as_mut()
114-
.setSpatialDimensions(c_uint::from(spatial_dimensions));
115-
}
116-
117-
/// Gets the unit of measurement for the compartment.
118-
///
119-
/// # Returns
120-
/// The unit of measurement as a String (e.g., "litre", "metre^3")
121-
pub fn unit(&self) -> String {
122-
self.inner.borrow().getUnits().to_str().unwrap().to_string()
123-
}
124-
125-
/// Sets the unit of measurement for the compartment.
126-
///
127-
/// # Arguments
128-
/// * `unit` - The unit of measurement to set (e.g., "litre", "metre^3")
129-
pub fn set_unit(&self, unit: &str) {
130-
let_cxx_string!(unit = unit);
131-
self.inner.borrow_mut().as_mut().setUnits(&unit);
132-
}
133-
134-
/// Gets the size of the compartment.
135-
///
136-
/// # Returns
137-
/// The size as a f64 in the units specified by the compartment's units attribute
138-
pub fn size(&self) -> f64 {
139-
self.inner.borrow().getSize()
140-
}
141-
142-
/// Sets the size of the compartment.
143-
///
144-
/// # Arguments
145-
/// * `size` - The size to set in the units specified by the compartment's units attribute
146-
pub fn set_size(&self, size: f64) {
147-
self.inner.borrow_mut().as_mut().setSize(size);
148-
}
149-
150-
/// Gets the volume of the compartment.
151-
///
152-
/// # Returns
153-
/// The volume as a f64 in the units specified by the compartment's units attribute
154-
pub fn volume(&self) -> f64 {
155-
self.inner.borrow().getVolume()
156-
}
157-
158-
/// Sets the volume of the compartment.
159-
///
160-
/// # Arguments
161-
/// * `volume` - The volume to set in the units specified by the compartment's units attribute
162-
pub fn set_volume(&self, volume: f64) {
163-
self.inner.borrow_mut().as_mut().setVolume(volume);
164-
}
165-
166-
/// Gets whether the compartment is constant.
167-
///
168-
/// # Returns
169-
/// true if the compartment's size is constant over time, false otherwise
170-
pub fn constant(&self) -> bool {
171-
self.inner.borrow().getConstant()
172-
}
173-
174-
/// Sets whether the compartment is constant.
175-
///
176-
/// # Arguments
177-
/// * `constant` - true if the compartment's size should be constant over time, false otherwise
178-
pub fn set_constant(&self, constant: bool) {
179-
self.inner.borrow_mut().as_mut().setConstant(constant);
180-
}
181-
182-
/// Gets the outside compartment reference.
183-
///
184-
/// # Returns
185-
/// The ID of the containing (outside) compartment as a String
186-
pub fn outside(&self) -> String {
187-
self.inner
188-
.borrow()
189-
.getOutside()
190-
.to_str()
191-
.unwrap()
192-
.to_string()
193-
}
194-
195-
/// Sets the outside compartment reference.
196-
///
197-
/// # Arguments
198-
/// * `outside` - The ID of the containing (outside) compartment to set
199-
pub fn set_outside(&self, outside: &str) {
200-
let_cxx_string!(outside = outside);
201-
self.inner.borrow_mut().as_mut().setOutside(&outside);
202-
}
63+
// Getter and setter methods for the id property
64+
required_property!(Compartment<'a>, id, String, getId, setId);
65+
66+
// Getter and setter methods for the name property
67+
optional_property!(Compartment<'a>, name, String, getName, setName, isSetName);
68+
69+
// Getter and setter methods for the spatial dimensions property
70+
optional_property!(
71+
Compartment<'a>,
72+
spatial_dimensions,
73+
u32,
74+
getSpatialDimensions,
75+
setSpatialDimensions,
76+
isSetSpatialDimensions
77+
);
78+
79+
// Getter and setter methods for the unit property
80+
optional_property!(
81+
Compartment<'a>,
82+
unit,
83+
String,
84+
getUnits,
85+
setUnits,
86+
isSetUnits
87+
);
88+
89+
// Getter and setter methods for the size property
90+
optional_property!(Compartment<'a>, size, f64, getSize, setSize, isSetSize);
91+
92+
// Getter and setter methods for the volume property
93+
optional_property!(
94+
Compartment<'a>,
95+
volume,
96+
f64,
97+
getVolume,
98+
setVolume,
99+
isSetVolume
100+
);
101+
102+
// Getter and setter methods for the constant property
103+
optional_property!(
104+
Compartment<'a>,
105+
constant,
106+
bool,
107+
getConstant,
108+
setConstant,
109+
isSetConstant
110+
);
111+
112+
// Getter and setter methods for the outside property
113+
optional_property!(
114+
Compartment<'a>,
115+
outside,
116+
String,
117+
getOutside,
118+
setOutside,
119+
isSetOutside
120+
);
203121

204122
// SBO Term Methods generated by the `sbo_term` macro
205123
sbo_term!(sbmlcxx::Compartment, sbmlcxx::SBase);
@@ -372,21 +290,21 @@ mod tests {
372290

373291
// Use all setters to set all properties
374292
compartment.set_name("test");
375-
compartment.set_spatial_dimensions(3);
293+
compartment.set_spatial_dimensions(3u32);
376294
compartment.set_unit("test");
377295
compartment.set_size(1.0);
378296
compartment.set_volume(1.0);
379297
compartment.set_outside("test");
380298
compartment.set_constant(true);
381299

382300
assert_eq!(compartment.id(), "test");
383-
assert_eq!(compartment.name(), "test");
384-
assert_eq!(compartment.spatial_dimensions(), 3);
385-
assert_eq!(compartment.unit(), "test");
386-
assert_eq!(compartment.size(), 1.0);
387-
assert_eq!(compartment.volume(), 1.0);
388-
assert_eq!(compartment.outside(), "test");
389-
assert!(compartment.constant());
301+
assert_eq!(compartment.name(), Some("test".to_string()));
302+
assert_eq!(compartment.spatial_dimensions(), Some(3));
303+
assert_eq!(compartment.unit(), Some("test".to_string()));
304+
assert_eq!(compartment.size(), Some(1.0));
305+
assert_eq!(compartment.volume(), Some(1.0));
306+
assert_eq!(compartment.outside(), Some("test".to_string()));
307+
assert_eq!(compartment.constant(), Some(true));
390308
}
391309

392310
#[test]
@@ -404,13 +322,13 @@ mod tests {
404322
.build();
405323

406324
assert_eq!(compartment.id(), "test");
407-
assert_eq!(compartment.name(), "test");
408-
assert_eq!(compartment.spatial_dimensions(), 3);
409-
assert_eq!(compartment.unit(), "test");
410-
assert_eq!(compartment.size(), 1.0);
411-
assert_eq!(compartment.volume(), 1.0);
412-
assert_eq!(compartment.outside(), "test");
413-
assert!(compartment.constant());
325+
assert_eq!(compartment.name(), Some("test".to_string()));
326+
assert_eq!(compartment.spatial_dimensions(), Some(3));
327+
assert_eq!(compartment.unit(), Some("test".to_string()));
328+
assert_eq!(compartment.size(), Some(1.0));
329+
assert_eq!(compartment.volume(), Some(1.0));
330+
assert_eq!(compartment.outside(), Some("test".to_string()));
331+
assert_eq!(compartment.constant(), Some(true));
414332
}
415333

416334
#[test]

0 commit comments

Comments
 (0)