use endpoint::EndpointError;
use self::endpoint::Endpoint;
use crate::interface::MAX_INTERFACE_MAPPINGS;
use crate::schema::{MappingType, SchemaError};
pub mod collection;
pub mod datastream;
pub mod endpoint;
pub mod path;
pub mod properties;
#[derive(Debug, thiserror::Error)]
pub enum MappingError {
#[error("an interface must have at lease one mapping")]
Empty,
#[error(
"too many mappings {0}, interfaces can have a max of {max} mappings",
max = MAX_INTERFACE_MAPPINGS
)]
TooMany(usize),
#[error("the mappings has a duplicated endpoint {endpoint}")]
Duplicated {
endpoint: String,
},
#[error("couldn't parse the mapping's endpoint")]
Endpoint(#[from] EndpointError),
#[error("object endpoint should have at least 2 levels: '{0}'")]
TooShortForObject(String),
#[error("invalid schema for mapping")]
Schema(#[from] SchemaError),
#[cfg(feature = "strict")]
#[cfg_attr(docsrs, doc(cfg(feature = "strict")))]
#[error("{field} is set for a {interface_type} interface")]
InvalidField {
field: &'static str,
interface_type: crate::schema::InterfaceType,
},
}
macro_rules! invalid_filed {
(properties, $field:literal) => {
cfg_if::cfg_if! {
if #[cfg(feature = "strict")] {
return Err($crate::mapping::MappingError::InvalidField{
field: $field,
interface_type: $crate::schema::InterfaceType::Properties
});
} else {
tracing::warn!("property cannot have $field, ignoring");
}
}
};
(datastream, $field:literal) => {
cfg_if::cfg_if! {
if #[cfg(feature = "strict")] {
return Err($crate::mapping::MappingError::InvalidField{
field: $field,
interface_type: $crate::schema::InterfaceType::Datastream,
});
} else {
tracing::warn!("property cannot have $field, ignoring");
}
}
};
}
pub(crate) use invalid_filed;
pub trait InterfaceMapping {
fn endpoint(&self) -> &Endpoint<String>;
fn mapping_type(&self) -> MappingType;
#[cfg(feature = "doc-fields")]
#[cfg_attr(docsrs, doc(cfg(feature = "doc-fields")))]
fn description(&self) -> Option<&str>;
#[cfg(feature = "doc-fields")]
#[cfg_attr(docsrs, doc(cfg(feature = "doc-fields")))]
fn doc(&self) -> Option<&str>;
}