astarte_interfaces/mapping/
mod.rs1use endpoint::EndpointError;
22
23use self::endpoint::Endpoint;
24
25use crate::interface::MAX_INTERFACE_MAPPINGS;
26use crate::schema::{MappingType, SchemaError};
27
28pub mod collection;
29pub mod datastream;
30pub mod endpoint;
31pub mod path;
32pub mod properties;
33
34#[derive(Debug, thiserror::Error)]
36pub enum MappingError {
37 #[error("an interface must have at lease one mapping")]
39 Empty,
40 #[error(
42 "too many mappings {0}, interfaces can have a max of {max} mappings",
43 max = MAX_INTERFACE_MAPPINGS
44 )]
45 TooMany(usize),
46 #[error("the mappings has a duplicated endpoint {endpoint}")]
50 Duplicated {
51 endpoint: String,
53 },
54 #[error("couldn't parse the mapping's endpoint")]
56 Endpoint(#[from] EndpointError),
57 #[error("object endpoint should have at least 2 levels: '{0}'")]
59 TooShortForObject(String),
60 #[error("invalid schema for mapping")]
62 Schema(#[from] SchemaError),
63 #[cfg(feature = "strict")]
65 #[cfg_attr(docsrs, doc(cfg(feature = "strict")))]
66 #[error("{field} is set for a {interface_type} interface")]
67 InvalidField {
68 field: &'static str,
70 interface_type: crate::schema::InterfaceType,
72 },
73}
74
75macro_rules! invalid_filed {
77 (properties, $field:literal) => {
78 cfg_if::cfg_if! {
79 if #[cfg(feature = "strict")] {
80 return Err($crate::mapping::MappingError::InvalidField{
81 field: $field,
82 interface_type: $crate::schema::InterfaceType::Properties
83 });
84 } else {
85 tracing::warn!("property cannot have $field, ignoring");
86 }
87 }
88 };
89 (datastream, $field:literal) => {
90 cfg_if::cfg_if! {
91 if #[cfg(feature = "strict")] {
92 return Err($crate::mapping::MappingError::InvalidField{
93 field: $field,
94 interface_type: $crate::schema::InterfaceType::Datastream,
95 });
96 } else {
97 tracing::warn!("property cannot have $field, ignoring");
98 }
99 }
100 };
101}
102
103pub(crate) use invalid_filed;
104
105pub trait InterfaceMapping {
107 fn endpoint(&self) -> &Endpoint<String>;
109 fn mapping_type(&self) -> MappingType;
111 #[cfg(feature = "doc-fields")]
113 #[cfg_attr(docsrs, doc(cfg(feature = "doc-fields")))]
114 fn description(&self) -> Option<&str>;
115 #[cfg(feature = "doc-fields")]
117 #[cfg_attr(docsrs, doc(cfg(feature = "doc-fields")))]
118 fn doc(&self) -> Option<&str>;
119}