wireman_core/descriptor/
reflection_request.rs

1use super::metadata::Metadata;
2use crate::error::{Error, Result};
3use tonic::metadata::{Ascii, MetadataKey, MetadataValue};
4
5/// Holds all the necessary data for a reflection request.
6#[derive(Debug, Clone)]
7pub struct ReflectionRequest {
8    /// The host address.
9    pub host: String,
10    /// The requests metadata.
11    pub metadata: Option<Metadata>,
12}
13
14impl ReflectionRequest {
15    /// Creates a new reflection request with a given hostname.
16    #[must_use]
17    pub fn new(host: &str) -> Self {
18        Self {
19            host: host.to_string(),
20            metadata: None,
21        }
22    }
23
24    /// Insert metadata into the reflection request.
25    ///
26    /// # Errors
27    ///
28    /// - Failed to parse metadata value/key to ascii
29    pub fn insert_metadata(&mut self, key: &str, val: &str) -> Result<()> {
30        let key: MetadataKey<Ascii> = key.parse().map_err(|_| Error::ParseToAsciiError)?;
31        let val: MetadataValue<Ascii> = val.parse().map_err(|_| Error::ParseToAsciiError)?;
32        let map = self.metadata.get_or_insert(Metadata::new());
33        map.insert(key, val);
34        Ok(())
35    }
36}