ifc_template_catalog/definition/
applicability.rs1use thiserror::Error;
4
5#[derive(Debug, Clone, PartialEq, Eq, Hash, bincode::Encode, bincode::Decode)]
7pub struct Applicability {
8 pub raw: String,
10 pub entity: String,
12 pub predefined_type: Option<String>,
14}
15
16impl Applicability {
17 pub fn entity(entity: impl Into<String>) -> Self {
19 let entity = entity.into();
20 Self {
21 raw: entity.clone(),
22 entity,
23 predefined_type: None,
24 }
25 }
26
27 pub fn parse(raw: impl Into<String>) -> Result<Self, ApplicabilityError> {
29 let raw = raw.into();
30 let value = raw.trim();
31 if value.is_empty() {
32 return Err(ApplicabilityError::Empty);
33 }
34 let (entity, predefined_type) = match value.split_once('/') {
35 Some((entity, predefined))
36 if !entity.trim().is_empty() && !predefined.trim().is_empty() =>
37 {
38 if predefined.contains('/') {
39 return Err(ApplicabilityError::Invalid(raw));
40 }
41 (entity.trim().to_owned(), Some(predefined.trim().to_owned()))
42 }
43 Some(_) => return Err(ApplicabilityError::Invalid(raw)),
44 None => (value.to_owned(), None),
45 };
46 Ok(Self {
47 raw,
48 entity,
49 predefined_type,
50 })
51 }
52}
53
54#[derive(Debug, Clone, PartialEq, Eq, Error)]
56#[non_exhaustive]
57pub enum ApplicabilityError {
58 #[error("applicability selector is empty")]
60 Empty,
61 #[error("invalid applicability selector `{0}`")]
64 Invalid(String),
65}