Skip to content

Commit

Permalink
feat(rust): make dyn_info more rigid with structs and enums
Browse files Browse the repository at this point in the history
  • Loading branch information
lameferret committed Aug 16, 2022
1 parent 4a6ac77 commit b61e1d0
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 106 deletions.
88 changes: 33 additions & 55 deletions implementations/rust/ockam/ockam_command/src/node/show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,65 +31,43 @@ impl ShowCommand {

// TODO: This function should be replaced with a better system of
// printing the node state in the future but for now we can just tell
// clippy to stop complainaing about it.
// clippy to stop complaining about it.
#[allow(clippy::too_many_arguments)]
fn print_node_info(node_cfg: &NodeConfig, node_name: &str, status: &str, default_id: &str) {
let node_out = util::dyn_info::DynInfo::new_node()
.name(node_name)
.status(status)
.services()
.service(
"TCP Listener",
&format!("/ip4/127.0.0.1/tcp/{}", node_cfg.port),
)
.service_detailed(
"Secure Channel Listener",
"/service/api",
&format!("/ip4/127.0.0.1/tcp/{}/service/api", node_cfg.port),
default_id,
default_id,
)
.service("Uppercase", "/service/uppercase")
.service("Echo", "/service/echo")
.scl()
.build();
use util::dyn_info::{NodeService, ServiceType};
let node_out = util::dyn_info::DynNodeInfo::new(node_name.to_string())
.status(status.to_string())
.service(NodeService::new(
ServiceType::TCPListener,
format!("/ip4/127.0.0.1/tcp/{}", node_cfg.port),
None,
None,
None,
))
.service(NodeService::new(
ServiceType::SecureChannelListener,
format!("/service/api"),
Some(format!("/ip4/127.0.0.1/tcp/{}/service/api", node_cfg.port)),
Some(default_id.to_string()),
Some(vec![format!("{}", default_id)]),
))
.service(NodeService::new(
ServiceType::Uppercase,
"/service/uppercase".to_string(),
None,
None,
None,
))
.service(NodeService::new(
ServiceType::Echo,
"/service/echo".to_string(),
None,
None,
None,
))
.secure_channel_addr_listener("/service/api".to_string());

println!("{}", node_out);
// println!(
// r#"
// Node:
// Name: {}
// Status: {}
// Services:
// Service:
// Type: TCP Listener
// Address: /ip4/127.0.0.1/tcp/{}
// Service:
// Type: Secure Channel Listener
// Address: /service/api
// Route: /ip4/127.0.0.1/tcp/{}/service/api
// Identity: {}
// Authorized Identities:
// - {}
// Service:
// Type: Uppercase
// Address: /service/uppercase
// Service:
// Type: Echo
// Address: /service/echo
// Secure Channel Listener Address: /service/api
// "#,
// node_name,
// match status {
// "UP" => status.light_green(),
// "DOWN" => status.light_red(),
// _ => status.white(),
// },
// node_cfg.port,
// node_cfg.port,
// default_id,
// default_id,
// );
}

pub async fn query_status(
Expand Down
179 changes: 129 additions & 50 deletions implementations/rust/ockam/ockam_command/src/util/dyn_info.rs
Original file line number Diff line number Diff line change
@@ -1,68 +1,147 @@
pub struct DynInfo(String);
use colorful::Colorful;
use std::fmt;
#[derive(Debug, Clone)]
pub struct DynNodeInfo {
name: String,
status: String,
services: Vec<NodeService>,
secure_channel_addr_listener: String,
}

impl DynInfo {
pub fn new_node() -> Self {
DynInfo(String::from("Node:\n"))
}
#[derive(Debug, Clone)]
pub struct NodeService {
service_type: ServiceType,
address: String,
route: Option<String>,
identity: Option<String>,
auth_identity: Option<Vec<String>>,
}
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub enum ServiceType {
TCPConnection,
TCPListener,
SecureChannelConnection,
SecureChannelListener,
Uppercase,
Echo,
}

pub fn name(mut self, name: &str) -> Self {
let name = format!(" Name: {}\n", name);
self.0.push_str(&name);
self
}
// Can't accept arbitrary status
// #[derive(Debug)]
// pub enum Status {
// UP,
// DOWN,
// NONE,
// }

pub fn status(mut self, status: &str) -> Self {
let status = format!(" Status: {}\n", status);
self.0.push_str(&status);
impl DynNodeInfo {
/// Name of the Node
pub fn new(name: String) -> Self {
Self {
name,
status: String::new(),
services: Vec::new(),
secure_channel_addr_listener: String::new(),
}
}
/// Status can either be UP, DOWN, TODO
pub fn status(mut self, status: String) -> Self {
self.status = status;
self
}

pub fn services(mut self) -> Self {
self.0.push_str(" Services:");
/// Use NodeService::new()
pub fn service(mut self, service: NodeService) -> Self {
self.services.push(service);
self
}

pub fn service(mut self, r#type: &str, address: &str) -> Self {
let service = format!(
r#"
Service:
Type: {}
Address: {}"#,
r#type, address
);
self.0.push_str(&service);
pub fn secure_channel_addr_listener(mut self, addr: String) -> Self {
self.secure_channel_addr_listener = addr;
self
}
}

pub fn service_detailed(
mut self,
r#type: &str,
address: &str,
route: &str,
identity: &str,
auth_identity: &str,
impl NodeService {
pub fn new(
service_type: ServiceType,
address: String,
route: Option<String>,
identity: Option<String>,
auth_identity: Option<Vec<String>>,
) -> Self {
let service_detailed = format!(
r#"
Service:
Type: {}
Address: {}
Route: {}
Identity: {}
Authorized Identities:
- {}"#,
r#type, address, route, identity, auth_identity
);
self.0.push_str(&service_detailed);
self
Self {
service_type,
address,
route,
identity,
auth_identity,
}
}
}

pub fn scl(mut self) -> Self {
self.0
.push_str("\n Secure Channel Listener Address: /service/api");
self
impl fmt::Display for DynNodeInfo {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut format_services = String::new();
for service in &self.services {
format_services = format_services + &format!("{}", service);
}

write!(
f,
"{}",
format!(
"Node:\n\tName: {}\n\tStatus: {}\n\tServices:\n{}\tSecure Channel Listener Address:{}",
self.name,
match self.status.as_str() {
"UP" => self.status.clone().light_green(),
"DOWN" => self.status.clone().light_red(),
_ => self.status.clone().white(),
},
format_services,
self.secure_channel_addr_listener,
)
)
}
}

impl fmt::Display for NodeService {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut out = format!(
"\t\tService:\n\t\t\tType: {}\n\t\t\tAddress: {}\n",
self.service_type, self.address
);
match (
self.route.clone(),
self.identity.clone(),
self.auth_identity.clone(),
) {
(Some(route), Some(identity), Some(auth_identity)) => {
let mut format_auth_identity = String::new();
for iden in auth_identity {
format_auth_identity = format_auth_identity + &format!("\t\t\t\t- {}\n", iden);
}
out.push_str(&format!(
"\t\t\tRoute: {}\n\t\t\tIdentity: {}\n\t\t\tAuthorized Identities: \n{}",
route, identity, format_auth_identity
));
}
(_, _, _) => (),
}
write!(f, "{}", out)
}
pub fn build(self) -> String {
self.0
}

impl fmt::Display for ServiceType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ServiceType::TCPConnection => write!(f, "TCP Connection"),
ServiceType::TCPListener => write!(f, "TCP Listener"),
ServiceType::SecureChannelConnection => write!(f, "Secure Channel Connection"),
ServiceType::SecureChannelListener => write!(f, "Secure Channel Listener"),
ServiceType::Uppercase => write!(f, "Uppercase"),
ServiceType::Echo => write!(f, "Echo"),
}
}
}
2 changes: 1 addition & 1 deletion implementations/rust/ockam/ockam_command/src/util/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub mod api;
pub mod dyn_info;
pub mod exitcode;
pub mod startup;
pub mod dyn_info;

mod addon;
pub use addon::AddonCommand;
Expand Down

0 comments on commit b61e1d0

Please sign in to comment.