Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
498 changes: 483 additions & 15 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ crate-type = ["staticlib", "rlib"]
autocxx = "0.28.0"
cxx = "1.0.140"
paste = "1.0.15"
quick-xml = { version = "0.37.2", features = ["serialize"] }
quick-xml = { version = "0.38.0", features = ["serialize"] }
serde = { version = "1.0.217", features = ["derive"] }
thiserror = "2.0.12"
zip = "4.0.0"

[build-dependencies]
autocxx-build = "0.28.0"
Expand All @@ -31,6 +32,7 @@ vcpkg = "0.2.15"
[dev-dependencies]
insta = "1.43.1"
pretty_assertions = "1.4.1"
tempfile = "3.20.0"

[lints.clippy]
needless-lifetimes = "allow"
Expand Down
4 changes: 2 additions & 2 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ fn setup_vcpkg() -> Result<vcpkg::Library, BuilderError> {
/// * `cargo_metadata` - A slice of strings containing cargo metadata directives
fn link_lib(cargo_metadata: &[String]) {
for metadata in cargo_metadata {
println!("{}", metadata);
println!("{metadata}");
}
}

Expand Down Expand Up @@ -169,7 +169,7 @@ fn from_pkg_config(pkg_config: &str) -> Result<(Vec<PathBuf>, Vec<String>), Stri

let mut cargo_metadata = Vec::new();
for lib in lib.libs {
cargo_metadata.push(format!("cargo:rustc-link-lib={}", lib));
cargo_metadata.push(format!("cargo:rustc-link-lib={lib}"));
}

Ok((lib.include_paths.clone(), cargo_metadata))
Expand Down
21 changes: 19 additions & 2 deletions examples/create.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use sbml::{prelude::*, unit::UnitKind};
use sbml::{combine::KnownFormats, prelude::*, unit::UnitKind};

fn main() -> Result<(), Box<dyn std::error::Error>> {
let doc = SBMLDocument::default();
Expand Down Expand Up @@ -56,7 +56,24 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let sbml_string = doc.to_xml_string();

// Print the SBML string
println!("{}", sbml_string);
println!("{sbml_string}");

// Save as a string to a file
std::fs::write("./model.xml", &sbml_string).expect("Failed to write file");

// Alternatively, save in a COMBINE archive
let mut archive = CombineArchive::new();
archive
.add_entry(
"./model.xml",
KnownFormats::SBML,
true,
sbml_string.as_bytes(),
)
.expect("Failed to add entry to archive");
archive
.save("./model.omex")
.expect("Failed to save archive");

Ok(())
}
49 changes: 49 additions & 0 deletions src/collections/compartments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,52 @@ upcast_annotation!(
sbmlcxx::ListOfCompartments,
sbmlcxx::SBase
);

#[cfg(test)]
mod tests {
use serde::{Deserialize, Serialize};

use crate::sbmldoc::SBMLDocument;

#[test]
fn test_list_of_compartments_annotation_serde() {
let doc = SBMLDocument::default();
let model = doc.create_model("test");

#[derive(Serialize, Deserialize)]
struct TestAnnotation {
test: String,
}

let annotation = TestAnnotation {
test: "Test".to_string(),
};

model
.set_compartments_annotation_serde(&annotation)
.unwrap();

let annotation: TestAnnotation = model.get_compartments_annotation_serde().unwrap();
assert_eq!(annotation.test, "Test");
}

#[test]
fn test_list_of_compartments_annotation() {
let doc = SBMLDocument::default();
let model = doc.create_model("test");

let annotation = "<test>Test</test>";
model
.set_compartments_annotation(annotation)
.expect("Failed to set annotation");

let annotation = model.get_compartments_annotation();
assert_eq!(
annotation
.replace("\n", "")
.replace("\r", "")
.replace(" ", ""),
"<annotation><test>Test</test></annotation>"
);
}
}
47 changes: 47 additions & 0 deletions src/collections/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,50 @@ upcast_annotation!(
sbmlcxx::ListOfParameters,
sbmlcxx::SBase
);

#[cfg(test)]
mod tests {
use serde::{Deserialize, Serialize};

use crate::sbmldoc::SBMLDocument;

#[test]
fn test_list_of_parameters_annotation_serde() {
let doc = SBMLDocument::default();
let model = doc.create_model("test");

#[derive(Serialize, Deserialize)]
struct TestAnnotation {
test: String,
}

let annotation = TestAnnotation {
test: "Test".to_string(),
};

model.set_parameters_annotation_serde(&annotation).unwrap();

let annotation: TestAnnotation = model.get_parameters_annotation_serde().unwrap();
assert_eq!(annotation.test, "Test");
}

#[test]
fn test_list_of_parameters_annotation() {
let doc = SBMLDocument::default();
let model = doc.create_model("test");

let annotation = "<test>Test</test>";
model
.set_parameters_annotation(annotation)
.expect("Failed to set annotation");

let annotation = model.get_parameters_annotation();
assert_eq!(
annotation
.replace("\n", "")
.replace("\r", "")
.replace(" ", ""),
"<annotation><test>Test</test></annotation>"
);
}
}
50 changes: 49 additions & 1 deletion src/collections/reactions.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{cell::RefCell, pin::Pin};

use crate::{inner, model::Model, pin_ptr, sbmlcxx, upcast_annotation};
use crate::{inner, model::Model, pin_ptr, sbase, sbmlcxx, upcast_annotation};

/// A safe wrapper around the libSBML ListOfReactions class.
///
Expand All @@ -24,8 +24,56 @@ impl<'a> ListOfReactions<'a> {

// Derive the inner type from the ListOfReactions type
inner!(sbmlcxx::ListOfReactions, ListOfReactions<'a>);
sbase!(ListOfReactions<'a>, sbmlcxx::ListOfReactions);
upcast_annotation!(
ListOfReactions<'a>,
sbmlcxx::ListOfReactions,
sbmlcxx::SBase
);

#[cfg(test)]
mod tests {
use serde::{Deserialize, Serialize};

use crate::sbmldoc::SBMLDocument;

#[test]
fn test_list_of_reactions_annotation_serde() {
let doc = SBMLDocument::default();
let model = doc.create_model("test");

#[derive(Serialize, Deserialize)]
struct TestAnnotation {
test: String,
}

let annotation = TestAnnotation {
test: "Test".to_string(),
};

model.set_reactions_annotation_serde(&annotation).unwrap();

let annotation: TestAnnotation = model.get_reactions_annotation_serde().unwrap();
assert_eq!(annotation.test, "Test");
}

#[test]
fn test_list_of_reactions_annotation() {
let doc = SBMLDocument::default();
let model = doc.create_model("test");

let annotation = "<test>Test</test>";
model
.set_reactions_annotation(annotation)
.expect("Failed to set annotation");

let annotation = model.get_reactions_annotation();
assert_eq!(
annotation
.replace("\n", "")
.replace("\r", "")
.replace(" ", ""),
"<annotation><test>Test</test></annotation>"
);
}
}
47 changes: 47 additions & 0 deletions src/collections/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,50 @@ impl<'a> ListOfRules<'a> {
// Derive the inner type from the ListOfRules type
inner!(sbmlcxx::ListOfRules, ListOfRules<'a>);
upcast_annotation!(ListOfRules<'a>, sbmlcxx::ListOfRules, sbmlcxx::SBase);

#[cfg(test)]
mod tests {
use serde::{Deserialize, Serialize};

use crate::sbmldoc::SBMLDocument;

#[test]
fn test_list_of_rules_annotation_serde() {
let doc = SBMLDocument::default();
let model = doc.create_model("test");

#[derive(Serialize, Deserialize)]
struct TestAnnotation {
test: String,
}

let annotation = TestAnnotation {
test: "Test".to_string(),
};

model.set_rate_rules_annotation_serde(&annotation).unwrap();

let annotation: TestAnnotation = model.get_rate_rules_annotation_serde().unwrap();
assert_eq!(annotation.test, "Test");
}

#[test]
fn test_list_of_rules_annotation() {
let doc = SBMLDocument::default();
let model = doc.create_model("test");

let annotation = "<test>Test</test>";
model
.set_rate_rules_annotation(annotation)
.expect("Failed to set annotation");

let annotation = model.get_rate_rules_annotation();
assert_eq!(
annotation
.replace("\n", "")
.replace("\r", "")
.replace(" ", ""),
"<annotation><test>Test</test></annotation>"
);
}
}
47 changes: 47 additions & 0 deletions src/collections/species.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,50 @@ impl<'a> ListOfSpecies<'a> {
// Derive the inner type from the ListOfSpecies type
inner!(sbmlcxx::ListOfSpecies, ListOfSpecies<'a>);
upcast_annotation!(ListOfSpecies<'a>, sbmlcxx::ListOfSpecies, sbmlcxx::SBase);

#[cfg(test)]
mod tests {
use serde::{Deserialize, Serialize};

use crate::sbmldoc::SBMLDocument;

#[test]
fn test_list_of_species_annotation_serde() {
let doc = SBMLDocument::default();
let model = doc.create_model("test");

#[derive(Serialize, Deserialize)]
struct TestAnnotation {
test: String,
}

let annotation = TestAnnotation {
test: "Test".to_string(),
};

model.set_species_annotation_serde(&annotation).unwrap();

let annotation: TestAnnotation = model.get_species_annotation_serde().unwrap();
assert_eq!(annotation.test, "Test");
}

#[test]
fn test_list_of_species_annotation() {
let doc = SBMLDocument::default();
let model = doc.create_model("test");

let annotation = "<test>Test</test>";
model
.set_species_annotation(annotation)
.expect("Failed to set annotation");

let annotation = model.get_species_annotation();
assert_eq!(
annotation
.replace("\n", "")
.replace("\r", "")
.replace(" ", ""),
"<annotation><test>Test</test></annotation>"
);
}
}
49 changes: 49 additions & 0 deletions src/collections/unitdefs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,52 @@ upcast_annotation!(
sbmlcxx::ListOfUnitDefinitions,
sbmlcxx::SBase
);

#[cfg(test)]
mod tests {
use serde::{Deserialize, Serialize};

use crate::sbmldoc::SBMLDocument;

#[test]
fn test_list_of_unitdefs_annotation_serde() {
let doc = SBMLDocument::default();
let model = doc.create_model("test");

#[derive(Serialize, Deserialize)]
struct TestAnnotation {
test: String,
}

let annotation = TestAnnotation {
test: "Test".to_string(),
};

model
.set_unit_definitions_annotation_serde(&annotation)
.unwrap();

let annotation: TestAnnotation = model.get_unit_definitions_annotation_serde().unwrap();
assert_eq!(annotation.test, "Test");
}

#[test]
fn test_list_of_unitdefs_annotation() {
let doc = SBMLDocument::default();
let model = doc.create_model("test");

let annotation = "<test>Test</test>";
model
.set_unit_definitions_annotation(annotation)
.expect("Failed to set annotation");

let annotation = model.get_unit_definitions_annotation();
assert_eq!(
annotation
.replace("\n", "")
.replace("\r", "")
.replace(" ", ""),
"<annotation><test>Test</test></annotation>"
);
}
}
Loading
Loading