use std::{borrow::Borrow, ops::Deref};
use crate::Interface;
use super::{mapping::path::MappingPath, DatastreamObject, Mapping};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct PropertyRef<'a>(pub(crate) &'a Interface);
impl<'a> Borrow<Interface> for PropertyRef<'a> {
fn borrow(&self) -> &Interface {
self.0
}
}
impl<'a> Deref for PropertyRef<'a> {
type Target = Interface;
fn deref(&self) -> &Self::Target {
self.0
}
}
impl<'a> AsRef<Interface> for PropertyRef<'a> {
fn as_ref(&self) -> &Interface {
self.0
}
}
#[derive(Debug, Clone, Copy)]
pub(crate) struct ObjectRef<'a> {
pub(crate) interface: &'a Interface,
pub(crate) object: &'a DatastreamObject,
}
impl<'a> ObjectRef<'a> {
pub(crate) fn new(interface: &'a Interface) -> Option<Self> {
match &interface.inner {
crate::interface::InterfaceType::DatastreamIndividual(_)
| crate::interface::InterfaceType::Properties(_) => None,
crate::interface::InterfaceType::DatastreamObject(object) => {
Some(Self { interface, object })
}
}
}
}
impl<'a> Deref for ObjectRef<'a> {
type Target = DatastreamObject;
fn deref(&self) -> &Self::Target {
self.object
}
}
#[derive(Debug, Clone, Copy)]
pub(crate) struct MappingRef<'a, I: 'a> {
path: &'a MappingPath<'a>,
mapping: Mapping<&'a str>,
interface: I,
}
impl<'a> MappingRef<'a, &'a Interface> {
pub(crate) fn new(interface: &'a Interface, path: &'a MappingPath) -> Option<Self> {
interface.mapping(path).map(|mapping| Self {
interface,
path,
mapping,
})
}
pub(crate) fn as_prop(&self) -> Option<MappingRef<'a, PropertyRef<'a>>> {
self.interface.as_prop_ref().map(|interface| MappingRef {
interface,
path: self.path,
mapping: self.mapping,
})
}
}
impl<'a> MappingRef<'a, PropertyRef<'a>> {
pub(crate) fn with_prop(interface: PropertyRef<'a>, path: &'a MappingPath) -> Option<Self> {
let mapping = interface.0.mapping(path)?;
Some(Self {
interface,
path,
mapping,
})
}
}
impl<'a, I> MappingRef<'a, I> {
#[inline]
pub(crate) fn mapping(&self) -> &Mapping<&str> {
&self.mapping
}
#[inline]
pub(crate) fn interface(&self) -> &I {
&self.interface
}
#[inline]
pub(crate) fn path(&self) -> &MappingPath {
self.path
}
}
impl<'a, I: 'a> Deref for MappingRef<'a, I> {
type Target = Mapping<&'a str>;
fn deref(&self) -> &Self::Target {
&self.mapping
}
}