Skip to content

Commit

Permalink
Add an example of how to implement a dummy package.
Browse files Browse the repository at this point in the history
  • Loading branch information
daemontus committed Jun 12, 2024
1 parent 9e14d07 commit 1167b9c
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
75 changes: 75 additions & 0 deletions src/dummy_extension.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use sbml_macros::{SBase, XmlWrapper};

use crate::{
core::Model,
xml::{RequiredChild, RequiredProperty, XmlElement, XmlSubtype, XmlSupertype, XmlWrapper},
};

const EXTENSION_URL: &str = "http://foo";

#[derive(Clone, Debug, XmlWrapper, SBase)]
pub struct ExtendedModel(XmlElement);

impl XmlSubtype<Model> for ExtendedModel {
fn try_cast_from_super(value: &Model) -> Option<Self> {
let doc = value.read_doc();
let declarations = value
.xml_element()
.raw_element()
.collect_namespace_prefixes(&doc, EXTENSION_URL);
if declarations.is_empty() {
// The extension is not declared for this model.
return None;
} else {
return unsafe { Some(ExtendedModel::unchecked_cast(value.clone())) };
}
}
}

impl XmlSupertype for Model {}

impl ExtendedModel {
pub fn extra_annotation(&self) -> RequiredChild<XmlElement> {
RequiredChild::new(self.xml_element(), "extraAnnotation", EXTENSION_URL)
}

pub fn extra_property(&self) -> RequiredProperty<String> {
RequiredProperty::new(self.xml_element(), "extraProperty")
}
}

#[cfg(test)]
mod tests {
use crate::{
dummy_extension::ExtendedModel,
xml::{OptionalXmlChild, RequiredXmlChild, RequiredXmlProperty, XmlSupertype, XmlWrapper},
Sbml,
};

#[test]
pub fn example_test() {
let doc = Sbml::read_str(r#"
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<sbml xmlns="http://www.sbml.org/sbml/level3/version2/core" level="3" version="2" xmlns:extra="http://foo" extra:required="false">
<model id="model-1" extraProperty="Some property text">
<extra:extraAnnotation>Some child text</extra:extraAnnotation>
</model>
</sbml>
"#).unwrap();

let model = doc.model().get().unwrap();
let e_model = model.try_downcast::<ExtendedModel>().unwrap();
assert_eq!(
e_model.extra_property().get(),
String::from("Some property text")
);
assert_eq!(
e_model
.extra_annotation()
.get()
.xml_element()
.text_content(),
String::from("Some child text")
);
}
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ pub(crate) mod constants;
#[cfg(test)]
pub mod test_suite;

pub mod dummy_extension;

/// The SBML container object
/// (Section 4.1; [specification](https://raw.githubusercontent.com/combine-org/combine-specifications/main/specifications/files/sbml.level-3.version-2.core.release-2.pdf)).
///
Expand Down

0 comments on commit 1167b9c

Please sign in to comment.