Skip to content

Commit

Permalink
Merge pull request #8 from lschmierer/element_model
Browse files Browse the repository at this point in the history
  • Loading branch information
lschmierer authored Apr 16, 2023
2 parents c989c61 + d37046f commit d8b1a46
Show file tree
Hide file tree
Showing 908 changed files with 860,582 additions and 760,268 deletions.
1 change: 0 additions & 1 deletion .github/workflows/benchmark_pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ env:

jobs:
benchmark:
name: benchmark
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand Down
43 changes: 43 additions & 0 deletions .github/workflows/codegen_version.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Codegen Version

on: [pull_request]

env:
CARGO_TERM_COLOR: always

jobs:
version_bumped:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
ref: main
- uses: SebRollen/[email protected]
id: main_version
with:
file: fhirbolt-codegen/Cargo.toml
field: package.version

- uses: actions/checkout@v3
- uses: SebRollen/[email protected]
id: this_version
with:
file: fhirbolt-codegen/Cargo.toml
field: package.version

- name: Check fhirbolt-codegen version updated if generated code changed
run: |
if git diff --quiet HEAD origin/main -- **/generated/ ; then
if [ ${{steps.main_version.outputs.value}} != ${{steps.this_version.outputs.value}} ]; then
exit 0
else
exit 1
fi
fi
- name: Check generated code has correct version (fhirbolt-codegen was run)
run: |
if grep -q v${{steps.this_version.outputs.value}} fhirbolt-model/src/generated/r4/resource.rs ; then
exit 0
else
exit 1
fi
16 changes: 14 additions & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
- name: Build
run: cargo build --release --verbose

test:
test_r4:
needs: build
runs-on: ubuntu-latest

Expand All @@ -31,7 +31,19 @@ jobs:
with:
shared-key: "rust-cache"
- name: Run tests
run: cargo test --release --verbose
run: cargo test --features r4 --release --verbose

test_r4b:
needs: build
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: Swatinem/rust-cache@v2
with:
shared-key: "rust-cache"
- name: Run tests
run: cargo test --features r4b --release --verbose

benchmark:
needs: build
Expand Down
48 changes: 48 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug integration test 'test_serde_json'",
"cargo": {
"args": [
"test",
"--all-features",
"--no-run",
"--test=test_serde_json",
"--package=tests"
],
"filter": {
"name": "test_serde_json",
"kind": "test"
}
},
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug integration test 'test_serde_xml'",
"cargo": {
"args": [
"test",
"--all-features",
"--no-run",
"--test=test_serde_xml",
"--package=tests"
],
"filter": {
"name": "test_serde_xml",
"kind": "test"
}
},
"args": [],
"cwd": "${workspaceFolder}"
}
]
}
11 changes: 11 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"rust-analyzer.cargo.features": "all",
"rust-analyzer.files.excludeDirs": [
"fhirbolt-model/src/generated/**",
"fhirbolt-serde/src/model/generated/**"
],
"files.watcherExclude": {
"**/generated/**": true
},
"debug.allowBreakpointsEverywhere": true
}
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ members = [
"fhirbolt",
"fhirbolt-codegen",
"fhirbolt-model",
"fhirbolt-element",
"fhirbolt-serde",
"fhirbolt-shared",
"tests",
"benches",
]
]
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,45 @@ Javascript | tbd./tbd. | tbd./tbd. | |
[Rust]: https://img.shields.io/crates/v/fhirbolt.svg
[docs.rs]: https://docs.rs/fhirbolt
[crates.io]: https://crates.io/crates/fhirbolt

## Rust

The Rust crate supports two working modes:
1. a generic element model
2. working with fully typed model structs.

The element model is always enabled as it is also used internally for deserializing model structs.
Model structs can be optionally enabled by specifying the desried FHIR release as Cargo feature.

You should only include the release that you really need, as this signigicantly increases build time.

```toml
[dependencies]
fhirbolt = { version = "0.2", features = ["r4b"] }
```

## Example

```rust
// The `Resource` type is an enum that contains all possible FHIR resources.
// If the resource type is known in advance, you could also use a concrete resource type
// (like e.g. `fhirbolt::model::r4b::resources::Observation`).
use fhirbolt::model::r4b::Resource;
use fhirbolt::serde::{DeserializationConfig, DeserializationMode};

// The type of `s` is `&str`
let s = "{
\"resourceType\": \"Observation\",
\"status\": \"final\",
\"code\": {
\"text\": \"some code\"
},
\"valueString\": \"some value\"
}";

let r: Resource = fhirbolt::json::from_str(s, None).unwrap();

match r {
Resource::Observation(o) => println!("deserialized observation: {:?}", r),
_ => (),
}
2 changes: 2 additions & 0 deletions benches/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ publish = false

[dependencies]
criterion = "0.4"
serde = "1.0"
serde_json = "1.0"

fhirbolt = { path = "../fhirbolt", features = ["r4"] }
tests = { path = "../tests" }
Expand Down
101 changes: 82 additions & 19 deletions benches/bench_serde.rs
Original file line number Diff line number Diff line change
@@ -1,61 +1,124 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion, Throughput};

use fhirbolt::model::{self, FhirRelease};
use fhirbolt::{element::Element, FhirReleases};

use test_utils::examples::{examples, ExampleFile, JsonOrXml};

pub fn bench(c: &mut Criterion) {
let mut examples_json = examples(FhirRelease::R4, JsonOrXml::Json);
let mut examples_json = examples(FhirReleases::R4, JsonOrXml::Json);
let mut examples_xml = examples(FhirReleases::R4, JsonOrXml::Xml);

let account_bytes_json = examples_json
.get("account-example-with-guarantor.json")
.read_to_vec();
let account_json =
fhirbolt::json::from_slice::<model::r4::resources::Account>(&account_bytes_json, None)
.unwrap();

let mut examples_xml = examples(FhirRelease::R4, JsonOrXml::Xml);

let account_bytes_xml = examples_xml
.get("account-example-with-guarantor(ewg).xml")
.read_to_vec();
let account_xml =
fhirbolt::xml::from_slice::<model::r4::resources::Account>(&account_bytes_xml, None)
.unwrap();

let account_struct: fhirbolt::model::r4::resources::Account =
fhirbolt::json::from_slice(&account_bytes_json, None).unwrap();
let account_enum: fhirbolt::model::r4::Resource =
fhirbolt::json::from_slice(&account_bytes_json, None).unwrap();
let account_element: Element<{ FhirReleases::R4 }> =
fhirbolt::json::from_slice(&account_bytes_json, None).unwrap();

let mut group = c.benchmark_group("json");
group.throughput(Throughput::Bytes(account_bytes_json.len() as u64));
group.bench_function("read account", |b| {
group.bench_function("read account struct", |b| {
b.iter(|| {
black_box(
fhirbolt::json::from_slice::<fhirbolt::model::r4::resources::Account>(
&account_bytes_json,
None,
)
.unwrap(),
)
})
});
group.bench_function("read account enum", |b| {
b.iter(|| {
black_box(
fhirbolt::json::from_slice::<fhirbolt::model::r4::Resource>(
&account_bytes_json,
None,
)
.unwrap(),
)
})
});
group.bench_function("read account element", |b| {
b.iter(|| {
black_box(
fhirbolt::json::from_slice::<model::r4::resources::Account>(
fhirbolt::json::from_slice::<Element<{ FhirReleases::R4 }>>(
&account_bytes_json,
None,
)
.unwrap(),
)
})
});
group.bench_function("write account", |b| {
b.iter(|| black_box(fhirbolt::json::to_vec(&account_json)))
group.bench_function("write account struct", |b| {
b.iter(|| black_box(fhirbolt::json::to_vec(&account_struct)))
});
group.bench_function("write account enum", |b| {
b.iter(|| black_box(fhirbolt::json::to_vec(&account_enum)))
});
group.bench_function("write account element", |b| {
b.iter(|| {
black_box(
fhirbolt::json::to_vec::<Element<{ FhirReleases::R4 }>>(&account_element).unwrap(),
)
})
});
group.finish();

let mut group = c.benchmark_group("xml");
group.throughput(Throughput::Bytes(account_bytes_xml.len() as u64));
group.bench_function("read account", |b| {
group.bench_function("read account struct", |b| {
b.iter(|| {
black_box(
fhirbolt::xml::from_slice::<model::r4::resources::Account>(
fhirbolt::xml::from_slice::<fhirbolt::model::r4::resources::Account>(
&account_bytes_xml,
None,
)
.unwrap(),
)
})
});
group.bench_function("write account", |b| {
b.iter(|| black_box(fhirbolt::xml::to_vec(&account_xml)))
group.bench_function("read account enum", |b| {
b.iter(|| {
black_box(
fhirbolt::xml::from_slice::<fhirbolt::model::r4::Resource>(
&account_bytes_xml,
None,
)
.unwrap(),
)
})
});
group.bench_function("read account element", |b| {
b.iter(|| {
black_box(
fhirbolt::xml::from_slice::<Element<{ FhirReleases::R4 }>>(
&account_bytes_xml,
None,
)
.unwrap(),
)
})
});
group.bench_function("write account struct", |b| {
b.iter(|| black_box(fhirbolt::xml::to_vec(&account_struct)))
});
group.bench_function("write account enum", |b| {
b.iter(|| black_box(fhirbolt::xml::to_vec(&account_enum)))
});
group.bench_function("write account element", |b| {
b.iter(|| {
black_box(
fhirbolt::xml::to_vec::<Element<{ FhirReleases::R4 }>>(&account_element).unwrap(),
)
})
});
group.finish();
}
Expand Down
6 changes: 2 additions & 4 deletions fhirbolt-codegen/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fhirbolt-codegen"
version = "0.1.0"
version = "0.2.0"
edition = "2018"
publish = false

Expand All @@ -16,6 +16,4 @@ lazy_static = "1.4"
quote = "1.0"
proc-macro2 = "1.0"
convert_case = "0.4"
chrono = "0.4"

fhirbolt-shared = { path = "../fhirbolt-shared" }
time = "0.3"
Loading

0 comments on commit d8b1a46

Please sign in to comment.