phenopacket_builder/
lib.rs

1#![deny(unsafe_code)] // At least for now 👻
2//! # phenopacket-builder
3//!
4//! `phenopacket-builder` streamlines programmatic assembly of Phenopacket Schema building blocks.
5//!
6//! # Examples
7//!
8//! See the
9//! [use cases](https://github.com/P2GX/phenopacket-builder/blob/master/tests/use_cases.rs)
10//! for examples.
11
12use phenopackets::schema::v2::core::OntologyClass;
13
14mod v2;
15
16#[derive(Copy, Clone, Debug, PartialEq, Hash, Eq)]
17pub struct Set;
18#[derive(Copy, Clone, Debug, Default, PartialEq, Hash, Eq)]
19pub struct Unset;
20
21pub trait Buildable {
22    type Builder: Default;
23    fn builder() -> Self::Builder {
24        Self::Builder::default()
25    }
26}
27
28pub trait Build<T> {
29    fn build(self) -> T;
30}
31
32/// To allow submitting `T` where `Build<T>` is expected.
33///
34/// This is used across the builders.
35///
36/// # Example
37///
38/// ```
39/// use phenopacket_builder::Build;
40///
41/// fn takes_build_val(val: impl Build<u8>) -> u8 {
42///     val.build()
43/// }
44///
45/// assert_eq!(takes_build_val(123), 123);
46/// ```
47impl<T, U> Build<U> for T
48where
49    T: Into<U>,
50{
51    fn build(self) -> U {
52        self.into()
53    }
54}
55
56/// A shortcut for creating an [`OntologyClass`] from its `id` and `label`.
57///
58/// # Example
59///
60/// ```
61/// use phenopacket_builder::oc;
62///
63/// let seizure = oc("HP:0001250", "Seizure");
64///
65/// assert_eq!(&seizure.id, "HP:0001250");
66/// assert_eq!(&seizure.label, "Seizure");
67/// ```
68pub fn oc(id: impl Into<String>, label: impl Into<String>) -> OntologyClass {
69    OntologyClass {
70        id: id.into(),
71        label: label.into(),
72    }
73}