use serde::Deserialize;
use serde_json::Value as JsonValue;
use super::types::legal::{Brief, Hearing, LegalCase, Regulation, Statute, Treaty};
use super::types::specialized::{
AudioVisualWork, Classic, Dataset, Event, Patent, Software, Standard,
};
use super::types::structural::{
Collection, CollectionComponent, Monograph, Serial, SerialComponent,
};
use super::{ClassExtension, InputReference, UnknownClassData};
macro_rules! boxed_reference_constructor {
($fn_name:ident, $ty:ty, $variant:ident) => {
fn $fn_name(reference: Box<$ty>) -> Self {
Self {
extension: ClassExtension::$variant(reference),
}
}
};
}
impl InputReference {
pub(super) fn from_known<T>(
class_extension: impl FnOnce(Box<T>) -> ClassExtension,
value: JsonValue,
) -> Result<Self, serde_json::Error>
where
T: for<'de> Deserialize<'de>,
{
if !matches!(&value, JsonValue::Object(_)) {
return Err(<serde_json::Error as serde::de::Error>::custom(
"reference body must be a JSON object",
));
}
let inner = serde_json::from_value(value)?;
Ok(Self {
extension: class_extension(Box::new(inner)),
})
}
#[allow(non_snake_case, reason = "Transitional compatibility constructor")]
#[must_use]
pub fn Monograph(reference: Box<Monograph>) -> Self {
Self::from_boxed_monograph(reference)
}
#[allow(non_snake_case, reason = "Transitional compatibility constructor")]
#[must_use]
pub fn CollectionComponent(reference: Box<CollectionComponent>) -> Self {
Self::from_boxed_collection_component(reference)
}
#[allow(non_snake_case, reason = "Transitional compatibility constructor")]
#[must_use]
pub fn SerialComponent(reference: Box<SerialComponent>) -> Self {
Self::from_boxed_serial_component(reference)
}
#[allow(non_snake_case, reason = "Transitional compatibility constructor")]
#[must_use]
pub fn Collection(reference: Box<Collection>) -> Self {
Self::from_boxed_collection(reference)
}
#[allow(non_snake_case, reason = "Transitional compatibility constructor")]
#[must_use]
pub fn Serial(reference: Box<Serial>) -> Self {
Self::from_boxed_serial(reference)
}
#[allow(non_snake_case, reason = "Transitional compatibility constructor")]
#[must_use]
pub fn LegalCase(reference: Box<LegalCase>) -> Self {
Self::from_boxed_legal_case(reference)
}
#[allow(non_snake_case, reason = "Transitional compatibility constructor")]
#[must_use]
pub fn Statute(reference: Box<Statute>) -> Self {
Self::from_boxed_statute(reference)
}
#[allow(non_snake_case, reason = "Transitional compatibility constructor")]
#[must_use]
pub fn Treaty(reference: Box<Treaty>) -> Self {
Self::from_boxed_treaty(reference)
}
#[allow(non_snake_case, reason = "Transitional compatibility constructor")]
#[must_use]
pub fn Hearing(reference: Box<Hearing>) -> Self {
Self::from_boxed_hearing(reference)
}
#[allow(non_snake_case, reason = "Transitional compatibility constructor")]
#[must_use]
pub fn Regulation(reference: Box<Regulation>) -> Self {
Self::from_boxed_regulation(reference)
}
#[allow(non_snake_case, reason = "Transitional compatibility constructor")]
#[must_use]
pub fn Brief(reference: Box<Brief>) -> Self {
Self::from_boxed_brief(reference)
}
#[allow(non_snake_case, reason = "Transitional compatibility constructor")]
#[must_use]
pub fn Classic(reference: Box<Classic>) -> Self {
Self::from_boxed_classic(reference)
}
#[allow(non_snake_case, reason = "Transitional compatibility constructor")]
#[must_use]
pub fn Patent(reference: Box<Patent>) -> Self {
Self::from_boxed_patent(reference)
}
#[allow(non_snake_case, reason = "Transitional compatibility constructor")]
#[must_use]
pub fn Dataset(reference: Box<Dataset>) -> Self {
Self::from_boxed_dataset(reference)
}
#[allow(non_snake_case, reason = "Transitional compatibility constructor")]
#[must_use]
pub fn Standard(reference: Box<Standard>) -> Self {
Self::from_boxed_standard(reference)
}
#[allow(non_snake_case, reason = "Transitional compatibility constructor")]
#[must_use]
pub fn Software(reference: Box<Software>) -> Self {
Self::from_boxed_software(reference)
}
#[allow(non_snake_case, reason = "Transitional compatibility constructor")]
#[must_use]
pub fn Event(reference: Box<Event>) -> Self {
Self::from_boxed_event(reference)
}
#[allow(non_snake_case, reason = "Transitional compatibility constructor")]
#[must_use]
pub fn AudioVisual(reference: Box<AudioVisualWork>) -> Self {
Self::from_boxed_audio_visual(reference)
}
#[allow(non_snake_case, reason = "Transitional compatibility constructor")]
#[must_use]
pub fn Unknown(reference: Box<UnknownClassData>) -> Self {
Self {
extension: ClassExtension::Unknown(reference),
}
}
boxed_reference_constructor!(from_boxed_monograph, Monograph, Monograph);
boxed_reference_constructor!(
from_boxed_collection_component,
CollectionComponent,
CollectionComponent
);
boxed_reference_constructor!(
from_boxed_serial_component,
SerialComponent,
SerialComponent
);
boxed_reference_constructor!(from_boxed_collection, Collection, Collection);
boxed_reference_constructor!(from_boxed_serial, Serial, Serial);
boxed_reference_constructor!(from_boxed_legal_case, LegalCase, LegalCase);
boxed_reference_constructor!(from_boxed_statute, Statute, Statute);
boxed_reference_constructor!(from_boxed_treaty, Treaty, Treaty);
boxed_reference_constructor!(from_boxed_hearing, Hearing, Hearing);
boxed_reference_constructor!(from_boxed_regulation, Regulation, Regulation);
boxed_reference_constructor!(from_boxed_brief, Brief, Brief);
boxed_reference_constructor!(from_boxed_classic, Classic, Classic);
boxed_reference_constructor!(from_boxed_patent, Patent, Patent);
boxed_reference_constructor!(from_boxed_dataset, Dataset, Dataset);
boxed_reference_constructor!(from_boxed_standard, Standard, Standard);
boxed_reference_constructor!(from_boxed_software, Software, Software);
boxed_reference_constructor!(from_boxed_event, Event, Event);
boxed_reference_constructor!(from_boxed_audio_visual, AudioVisualWork, AudioVisual);
}