-
-
Notifications
You must be signed in to change notification settings - Fork 557
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rust): make
dyn_info
more rigid with structs and enums
- Loading branch information
1 parent
4a6ac77
commit b61e1d0
Showing
3 changed files
with
163 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
179 changes: 129 additions & 50 deletions
179
implementations/rust/ockam/ockam_command/src/util/dyn_info.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters