lewp_css/domain/at_rules/namespace/
namespaces.rs

1// This file is part of css. It is subject to the license terms in the COPYRIGHT file found in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/css/master/COPYRIGHT. No part of predicator, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
2// Copyright © 2017 The developers of css. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/css/master/COPYRIGHT.
3
4use {
5    super::{NamespacePrefix, NamespaceUrl},
6    std::{collections::HashMap, rc::Rc},
7};
8
9#[derive(Default, Debug, Clone, Eq, PartialEq)]
10pub struct Namespaces {
11    defaultNamespace: Option<NamespaceUrl>,
12    namespaces: HashMap<NamespacePrefix, NamespaceUrl>,
13}
14
15impl Namespaces {
16    #[inline(always)]
17    pub fn default_namespace(&self) -> Option<NamespaceUrl> {
18        self.defaultNamespace
19            .as_ref().cloned()
20    }
21
22    #[inline(always)]
23    pub fn namespace_for_prefix(
24        &self,
25        prefix: &NamespacePrefix,
26    ) -> Option<NamespaceUrl> {
27        self.namespaces
28            .get(prefix).cloned()
29    }
30
31    #[inline(always)]
32    pub(crate) fn empty() -> Rc<Self> {
33        Rc::new(Self::default())
34    }
35
36    #[inline(always)]
37    pub(crate) fn update(
38        &mut self,
39        prefix: Option<&NamespacePrefix>,
40        url: &NamespaceUrl,
41    ) {
42        match prefix {
43            None => self.defaultNamespace = Some(url.clone()),
44            Some(prefix) => {
45                self.namespaces.insert(prefix.clone(), url.clone());
46            }
47        }
48    }
49}