1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
use std::collections::HashMap; use uri::Uri; use Result; use error::{Error, ErrorType}; /// Representation of a specific namespace. #[derive(Debug)] pub struct Namespace { prefix: String, uri: Uri, } impl Namespace { /// `Namespace` constructor. /// /// # Examples /// /// ``` /// use rdf::namespace::Namespace; /// use rdf::uri::Uri; /// /// let ns = Namespace::new("example".to_string(), /// Uri::new("http://example.org/".to_string())); /// ``` pub fn new(prefix: String, uri: Uri) -> Namespace { Namespace { prefix, uri } } /// Returns the prefix of the namespace. /// /// # Examples /// /// ``` /// use rdf::namespace::Namespace; /// use rdf::uri::Uri; /// /// let ns = Namespace::new("example".to_string(), /// Uri::new("http://example.org/".to_string())); /// /// assert_eq!(ns.prefix(), "example"); /// ``` pub fn prefix(&self) -> &String { &self.prefix } /// Returns the URI of the namespace. /// /// # Examples /// /// ``` /// use rdf::namespace::Namespace; /// use rdf::uri::Uri; /// /// let ns = Namespace::new("example".to_string(), /// Uri::new("http://example.org/".to_string())); /// /// assert_eq!(ns.uri(), &Uri::new("http://example.org/".to_string())); /// ``` pub fn uri(&self) -> &Uri { &self.uri } } /// Storage for multiple namespaces. #[derive(PartialEq, Debug, Default)] pub struct NamespaceStore { /// The namespace prefix is associated with the namespace URI. namespaces: HashMap<String, Uri>, } impl NamespaceStore { /// Constructor for `NamespaceStore`. /// /// # Examples /// /// ``` /// use rdf::namespace::NamespaceStore; /// /// let nss = NamespaceStore::new(); /// ``` pub fn new() -> NamespaceStore { NamespaceStore { namespaces: HashMap::new(), } } /// Returns the stored namespaces with prefixes. pub fn namespaces(&self) -> &HashMap<String, Uri> { &self.namespaces } /// Adds a new namespace. /// /// # Examples /// /// ``` /// use rdf::namespace::NamespaceStore; /// use rdf::namespace::Namespace; /// use rdf::uri::Uri; /// /// let mut nss = NamespaceStore::new(); /// /// let ns = Namespace::new("example".to_string(), /// Uri::new("http://example.org/".to_string())); /// /// nss.add(&ns); /// ``` pub fn add(&mut self, ns: &Namespace) { self.namespaces.insert(ns.prefix().clone(), ns.uri.clone()); } /// Returns the URI of a specific namespace. /// /// # Examples /// /// ``` /// use rdf::namespace::NamespaceStore; /// use rdf::namespace::Namespace; /// use rdf::uri::Uri; /// /// let mut nss = NamespaceStore::new(); /// /// let ns = Namespace::new("example".to_string(), /// Uri::new("http://example.org/".to_string())); /// /// nss.add(&ns); /// /// assert_eq!(nss.get_uri_by_prefix("example").unwrap(), /// &Uri::new("http://example.org/".to_string())) /// ``` pub fn get_uri_by_prefix(&self, prefix: &str) -> Result<&Uri> { match self.namespaces.get(prefix) { Some(uri) => Ok(uri), None => Err(Error::new( ErrorType::InvalidNamespace, "Namespace does not exists for prefix: ".to_string() + prefix, )), } } }