Skip to main content

ifc_template_catalog/definition/
applicability.rs

1//! Applicability selectors from PSD/QTO catalogs.
2
3use thiserror::Error;
4
5/// An entity selector, optionally restricted by IFC predefined type.
6#[derive(Debug, Clone, PartialEq, Eq, Hash, bincode::Encode, bincode::Decode)]
7pub struct Applicability {
8    /// Original selector text retained for provenance and round trips.
9    pub raw: String,
10    /// IFC entity name.
11    pub entity: String,
12    /// Optional predefined type after `/` in publication data.
13    pub predefined_type: Option<String>,
14}
15
16impl Applicability {
17    /// Construct an unrestricted entity selector.
18    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    /// Parse `IfcEntity` or `IfcEntity/PREDEFINEDTYPE`.
28    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/// Invalid catalog applicability syntax.
55#[derive(Debug, Clone, PartialEq, Eq, Error)]
56#[non_exhaustive]
57pub enum ApplicabilityError {
58    /// The selector string was empty or whitespace-only.
59    #[error("applicability selector is empty")]
60    Empty,
61    /// The selector had a `/` but its entity or predefined-type side was
62    /// empty, or it contained more than one `/`.
63    #[error("invalid applicability selector `{0}`")]
64    Invalid(String),
65}