Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrap generated capabilities to provide convenience functions #9

Merged
merged 4 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
warp generated capabilities to provide some comfort functions
  • Loading branch information
Lachstec committed Feb 15, 2024
commit fdda741ac89ec8ee87d699fa9f7e5f942622f571
49 changes: 49 additions & 0 deletions src/client/capabilities.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use crate::gen::gnmi::CapabilityResponse;
use crate::gen::gnmi::ModelData;


pub use crate::gen::gnmi::Encoding;

/// Capabilities of a given gNMI Target device.
///
/// Contains information about the capabilities that supported by a gNMI Target device.
#[derive(Debug, Clone)]
pub struct Capabilities(pub(crate) CapabilityResponse);

impl<'a> Capabilities {
/// Retrieve the gNMI Version that the target device supports.
pub fn gnmi_version(&'a self) -> &'a str {
self.0.g_nmi_version.as_str()
}

/// Check if target device supports a given model.
///
/// # Arguments
/// - name: Name of the model
/// - organization: Organization publishing the model
/// - version: Version of the model
pub fn supports_model(&self, name: &str, organization: &str, version: &str) -> bool {
self.0.supported_models.contains(&ModelData {
name: name.to_string(),
organization: organization.to_string(),
version: version.to_string()
})
}

/// Check if a target device supports a given [`Encoding`].
///
/// # Arguments
/// - encoding: The [`Encoding`] to check for.
pub fn supports_encoding(&self, encoding: Encoding) -> bool {
let enc: i32 = match encoding {
Encoding::Json => 0,
Encoding::Bytes => 1,
Encoding::Proto => 2,
Encoding::Ascii => 3,
Encoding::JsonIetf => 4
};

self.0.supported_encodings.contains(&enc)
}
}

7 changes: 4 additions & 3 deletions src/client/client.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::auth::AuthService;
use crate::error::GinmiError;
use crate::gen::gnmi::g_nmi_client::GNmiClient;
use crate::gen::gnmi::{CapabilityRequest, CapabilityResponse};
use crate::gen::gnmi::CapabilityRequest;
use super::capabilities::Capabilities;
use http::HeaderValue;
use std::str::FromStr;
use std::sync::Arc;
Expand Down Expand Up @@ -36,10 +37,10 @@ impl<'a> Client {
/// let capabilities = client.capabilities().await;
/// # }
/// ```
pub async fn capabilities(&mut self) -> CapabilityResponse {
pub async fn capabilities(&mut self) -> Capabilities {
let req = CapabilityRequest::default();
match self.inner.capabilities(req).await {
Ok(val) => val.into_inner(),
Ok(val) => Capabilities(val.into_inner()),
Err(e) => panic!("Error getting capabilities: {:?}", e),
}
}
Expand Down
8 changes: 7 additions & 1 deletion src/client/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
mod client;
mod capabilities;

pub use client::{Client, ClientBuilder};
pub use client::{Client, ClientBuilder};

pub use capabilities::{
Capabilities,
Encoding
};
8 changes: 6 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ mod auth;
mod client;
mod error;

pub use client::{Client, ClientBuilder};

pub use client::{
Client,
ClientBuilder,
Encoding,
Capabilities
};
pub use error::GinmiError;

pub(crate) mod gen {
Expand Down