Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ lint:
just --justfile {{justfile()}} clippy

clippy:
cargo clippy --all-targets
cargo clippy --all-targets --all-features

check:
cargo check --all-targets
cargo check --all-targets --all-features

fmt:
cargo fmt --all
Expand Down
188 changes: 187 additions & 1 deletion src/native/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

#[cfg(feature = "python")]
use pyo3::{prelude::*, types::PyDateTime};
use pyo3::{
prelude::*,
types::{PyDateTime, PyDict},
};

use crate::native::deserializers::{
default_datetime_none, default_string_none, deserialize_empty_string_as_none,
Expand Down Expand Up @@ -76,6 +79,17 @@ impl Value {
fn value(&self) -> PyResult<String> {
Ok(self.value.clone())
}

pub fn to_dict<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyDict>> {
let dict = PyDict::new_bound(py);
dict.set_item("by", &self.by)?;
dict.set_item("by_unique_id", &self.by_unique_id)?;
dict.set_item("role", &self.role)?;
dict.set_item("when", to_py_datetime(py, &self.when)?)?;
dict.set_item("value", &self.value)?;

Ok(dict)
}
}

#[cfg(not(feature = "python"))]
Expand Down Expand Up @@ -144,6 +158,17 @@ impl Reason {
fn value(&self) -> PyResult<String> {
Ok(self.value.clone())
}

pub fn to_dict<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyDict>> {
let dict = PyDict::new_bound(py);
dict.set_item("by", &self.by)?;
dict.set_item("by_unique_id", &self.by_unique_id)?;
dict.set_item("role", &self.role)?;
dict.set_item("when", to_py_datetime(py, &self.when)?)?;
dict.set_item("value", &self.value)?;

Ok(dict)
}
}

#[cfg(not(feature = "python"))]
Expand All @@ -167,6 +192,42 @@ pub struct Entry {
pub reason: Option<Reason>,
}

#[cfg(feature = "python")]
#[pymethods]
impl Entry {
#[getter]
fn entry_id(&self) -> PyResult<String> {
Ok(self.entry_id.clone())
}

#[getter]
fn value(&self) -> PyResult<Option<Value>> {
Ok(self.value.clone())
}

#[getter]
fn reason(&self) -> PyResult<Option<Reason>> {
Ok(self.reason.clone())
}

pub fn to_dict<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyDict>> {
let dict = PyDict::new_bound(py);
dict.set_item("entry_id", &self.entry_id)?;
if let Some(value) = &self.value {
dict.set_item("value", value.to_dict(py)?)?;
} else {
dict.set_item("value", py.None())?;
}
if let Some(reason) = &self.reason {
dict.set_item("reason", reason.to_dict(py)?)?;
} else {
dict.set_item("reason", py.None())?;
}

Ok(dict)
}
}

#[cfg(not(feature = "python"))]
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -250,6 +311,29 @@ impl Field {
fn entries(&self) -> PyResult<Option<Vec<Entry>>> {
Ok(self.entries.clone())
}

pub fn to_dict<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyDict>> {
let dict = PyDict::new_bound(py);
dict.set_item("name", &self.name)?;
dict.set_item("field_type", &self.field_type)?;
dict.set_item("data_type", &self.data_type)?;
dict.set_item("error_code", &self.error_code)?;
dict.set_item("when_created", to_py_datetime(py, &self.when_created)?)?;
dict.set_item("keep_history", self.keep_history)?;

let mut entry_dicts = Vec::new();
if let Some(entries) = &self.entries {
for entry in entries {
let entry_dict = entry.to_dict(py)?;
entry_dicts.push(entry_dict.to_object(py));
}
dict.set_item("entries", entry_dicts)?;
} else {
dict.set_item("entries", py.None())?;
}

Ok(dict)
}
}

#[cfg(not(feature = "python"))]
Expand Down Expand Up @@ -283,6 +367,50 @@ pub struct Category {
pub fields: Option<Vec<Field>>,
}

#[cfg(feature = "python")]
#[pymethods]
impl Category {
#[getter]
fn name(&self) -> PyResult<String> {
Ok(self.name.clone())
}

#[getter]
fn category_type(&self) -> PyResult<String> {
Ok(self.category_type.clone())
}

#[getter]
fn highest_index(&self) -> PyResult<usize> {
Ok(self.highest_index)
}

#[getter]
fn fields(&self) -> PyResult<Option<Vec<Field>>> {
Ok(self.fields.clone())
}

pub fn to_dict<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyDict>> {
let dict = PyDict::new_bound(py);
dict.set_item("name", &self.name)?;
dict.set_item("category_type", &self.category_type)?;
dict.set_item("highest_index", self.highest_index)?;

let mut field_dicts = Vec::new();
if let Some(fields) = &self.fields {
for field in fields {
let field_dict = field.to_dict(py)?;
field_dicts.push(field_dict.to_object(py));
}
dict.set_item("fields", field_dicts)?;
} else {
dict.set_item("fields", py.None())?;
}

Ok(dict)
}
}

#[cfg(not(feature = "python"))]
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -336,6 +464,16 @@ impl State {
fn date_signed<'py>(&self, py: Python<'py>) -> PyResult<Option<Bound<'py, PyDateTime>>> {
to_py_datetime_option(py, &self.date_signed)
}

pub fn to_dict<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyDict>> {
let dict = PyDict::new_bound(py);
dict.set_item("value", &self.value)?;
dict.set_item("signer", &self.signer)?;
dict.set_item("signer_unique_id", &self.signer_unique_id)?;
dict.set_item("date_signed", to_py_datetime_option(py, &self.date_signed)?)?;

Ok(dict)
}
}

#[cfg(not(feature = "python"))]
Expand Down Expand Up @@ -539,4 +677,52 @@ impl Form {
fn categories(&self) -> PyResult<Option<Vec<Category>>> {
Ok(self.categories.clone())
}

pub fn to_dict<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyDict>> {
let dict = PyDict::new_bound(py);
dict.set_item("name", &self.name)?;
dict.set_item(
"last_modified",
to_py_datetime_option(py, &self.last_modified)?,
)?;
dict.set_item("who_last_modified_name", &self.who_last_modified_name)?;
dict.set_item("who_last_modified_role", &self.who_last_modified_role)?;
dict.set_item("when_created", self.when_created)?;
dict.set_item("has_errors", self.has_errors)?;
dict.set_item("has_warnings", self.has_warnings)?;
dict.set_item("locked", self.locked)?;
dict.set_item("user", &self.user)?;
dict.set_item(
"date_time_changed",
to_py_datetime_option(py, &self.date_time_changed)?,
)?;
dict.set_item("form_title", &self.form_title)?;
dict.set_item("form_index", self.form_index)?;
dict.set_item("form_group", &self.form_group)?;
dict.set_item("form_state", &self.form_state)?;

let mut state_dicts = Vec::new();
if let Some(states) = &self.states {
for state in states {
let state_dict = state.to_dict(py)?;
state_dicts.push(state_dict.to_object(py));
}
dict.set_item("states", state_dicts)?;
} else {
dict.set_item("states", py.None())?;
}

if let Some(categories) = &self.categories {
let mut category_dicts = Vec::new();
for category in categories {
let category_dict = category.to_dict(py)?;
category_dicts.push(category_dict.to_object(py));
}
dict.set_item("categories", category_dicts)?;
} else {
dict.set_item("categories", py.None())?;
}

Ok(dict)
}
}
53 changes: 52 additions & 1 deletion src/native/site_native.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use chrono::{DateTime, Utc};

#[cfg(feature = "python")]
use pyo3::{prelude::*, types::PyDateTime};
use pyo3::{
prelude::*,
types::{PyDateTime, PyDict},
};

use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -91,6 +94,33 @@ impl Site {
fn forms(&self) -> PyResult<Option<Vec<Form>>> {
Ok(self.forms.clone())
}

pub fn to_dict<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyDict>> {
let dict = PyDict::new_bound(py);
dict.set_item("name", &self.name)?;
dict.set_item("unique_id", &self.unique_id)?;
dict.set_item("number_of_patients", self.number_of_patients)?;
dict.set_item(
"count_of_randomized_patients",
self.count_of_randomized_patients,
)?;
dict.set_item("when_created", to_py_datetime(py, &self.when_created)?)?;
dict.set_item("creator", &self.creator)?;
dict.set_item("number_of_forms", self.number_of_forms)?;

let mut form_dicts = Vec::new();
if let Some(forms) = &self.forms {
for form in forms {
let form_dict = form.to_dict(py)?;
form_dicts.push(form_dict.to_object(py));
}
dict.set_item("forms", form_dicts)?;
} else {
dict.set_item("forms", py.None())?;
}

Ok(dict)
}
}

#[cfg(not(feature = "python"))]
Expand All @@ -113,6 +143,27 @@ pub struct SiteNative {
pub sites: Vec<Site>,
}

#[cfg(feature = "python")]
#[pymethods]
impl SiteNative {
#[getter]
fn sites(&self) -> PyResult<Vec<Site>> {
Ok(self.sites.clone())
}

/// Convert the class instance to a dictionary
fn to_dict<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyDict>> {
let dict = PyDict::new_bound(py);
let mut site_dicts = Vec::new();
for site in &self.sites {
let site_dict = site.to_dict(py)?;
site_dicts.push(site_dict.to_object(py));
}
dict.set_item("sites", site_dicts)?;
Ok(dict)
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
Loading