Skip to content

Commit

Permalink
to squash
Browse files Browse the repository at this point in the history
  • Loading branch information
Olivier committed Jan 2, 2025
1 parent 66d3951 commit c405041
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
9 changes: 7 additions & 2 deletions opcua-client/src/session/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ impl Session {
Error::new(status_code, "Reading Server namespace array failed")
})?;
if let Some(Variant::Array(array)) = &result[0].value {
let map = NamespaceMap::new_from_variant_array(&array.values);
let map = NamespaceMap::new_from_variant_array(&array.values)?;
let map_clone = map.clone();
self.set_namespaces(map);
Ok(map_clone)
Expand All @@ -461,10 +461,15 @@ impl Session {
}
}

/// Return index of supplied namespace url from cache
pub fn get_namespace_index_from_cache(&mut self, url: &str) -> Option<u16> {
self.encoding_context.read().namespaces().get_index(url)
}

/// Return index of supplied namespace url
/// by first looking at namespace cache and querying server if necessary
pub async fn get_namespace_index(&mut self, url: &str) -> Result<u16, Error> {
if let Some(idx) = self.encoding_context.read().namespaces().get_index(url) {
if let Some(idx) = self.get_namespace_index_from_cache(url) {
return Ok(idx);
};
let map = self.read_namespace_array().await?;
Expand Down
17 changes: 10 additions & 7 deletions opcua-types/src/namespaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use hashbrown::HashMap;

use crate::{ExpandedNodeId, NodeId, Variant};
use crate::{Error, ExpandedNodeId, NodeId, StatusCode, Variant};

/// Utility for handling assignment of namespaces on server startup.
#[derive(Debug, Default, Clone)]
Expand All @@ -28,19 +28,22 @@ impl NamespaceMap {

/// Create a new namespace map from a vec of variant as we get when reading
/// the namespace array from the server
pub fn new_from_variant_array(array: &[Variant]) -> Self {
let known_namespaces = array
pub fn new_from_variant_array(array: &[Variant]) -> Result<Self, Error> {
let known_namespaces: HashMap<String, u16> = array
.iter()
.enumerate()
.map(|(idx, v)| {
if let Variant::String(s) = v {
(s.value().clone().unwrap_or(String::new()), idx as u16)
Ok((s.value().clone().unwrap_or(String::new()), idx as u16))
} else {
(String::new(), idx as u16)
Err(Error::new(
StatusCode::Bad,
"Namespace array on server contains invalid data",
))
}
})
.collect();
Self { known_namespaces }
.collect::<Result<HashMap<_, _>, _>>()?;
Ok(Self { known_namespaces })
}

/// Add a new namespace, returning its index in the namespace map.
Expand Down

0 comments on commit c405041

Please sign in to comment.