use crate::{
data_types::*,
error::{ErrorKind, Lib3hProtocolError},
protocol_client::Lib3hClientProtocol,
protocol_server::Lib3hServerProtocol,
types::NetworkHash,
uri::Lib3hUri,
};
use std::convert::TryFrom;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum ClientToLib3h {
Bootstrap(BootstrapData),
JoinSpace(SpaceData),
LeaveSpace(SpaceData),
SendDirectMessage(DirectMessageData),
FetchEntry(FetchEntryData),
PublishEntry(ProvidedEntryData),
QueryEntry(QueryEntryData),
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum ClientToLib3hResponse {
BootstrapSuccess,
SendDirectMessageResult(DirectMessageData),
FetchEntryResult(FetchEntryResultData),
QueryEntryResult(QueryEntryResultData),
JoinSpaceResult,
LeaveSpaceResult,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Lib3hToClient {
Connected(ConnectedData),
Unbound(UnboundData),
SendDirectMessageResult(DirectMessageData),
HandleSendDirectMessage(DirectMessageData),
HandleFetchEntry(FetchEntryData),
HandleStoreEntryAspect(StoreEntryAspectData),
HandleDropEntry(DropEntryData),
HandleQueryEntry(QueryEntryData),
HandleGetAuthoringEntryList(GetListData),
HandleGetGossipingEntryList(GetListData),
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Lib3hToClientResponse {
HandleSendDirectMessageResult(DirectMessageData),
HandleFetchEntryResult(FetchEntryResultData),
HandleStoreEntryAspectResult,
HandleDropEntryResult,
HandleQueryEntryResult(QueryEntryResultData),
HandleGetAuthoringEntryListResult(EntryListData),
HandleGetGossipingEntryListResult(EntryListData),
}
impl TryFrom<Lib3hClientProtocol> for ClientToLib3h {
type Error = Lib3hProtocolError;
fn try_from(c: Lib3hClientProtocol) -> Result<Self, Self::Error> {
match c {
Lib3hClientProtocol::Connect(connect_data) => {
Ok(ClientToLib3h::Bootstrap(BootstrapData {
network_or_space_address: connect_data.network_id.clone().into(),
bootstrap_uri: connect_data.peer_location,
}))
}
Lib3hClientProtocol::JoinSpace(space_data) => Ok(ClientToLib3h::JoinSpace(space_data)),
Lib3hClientProtocol::LeaveSpace(space_data) => {
Ok(ClientToLib3h::LeaveSpace(space_data))
}
Lib3hClientProtocol::SendDirectMessage(direct_message_data) => {
Ok(ClientToLib3h::SendDirectMessage(direct_message_data))
}
Lib3hClientProtocol::FetchEntry(fetch_entry_data) => {
Ok(ClientToLib3h::FetchEntry(fetch_entry_data))
}
Lib3hClientProtocol::PublishEntry(provided_entry_data) => {
Ok(ClientToLib3h::PublishEntry(provided_entry_data))
}
Lib3hClientProtocol::QueryEntry(query_entry_data) => {
Ok(ClientToLib3h::QueryEntry(query_entry_data))
}
variant => Err(Lib3hProtocolError::new(ErrorKind::Other(format!(
"{:?} can't convert to ClientToLib3h",
variant
)))),
}
}
}
impl TryFrom<Lib3hClientProtocol> for Lib3hToClientResponse {
type Error = Lib3hProtocolError;
fn try_from(c: Lib3hClientProtocol) -> Result<Self, Self::Error> {
match c {
Lib3hClientProtocol::HandleSendDirectMessageResult(direct_message_data) => Ok(
Lib3hToClientResponse::HandleSendDirectMessageResult(direct_message_data),
),
Lib3hClientProtocol::HandleFetchEntryResult(fetch_entry_result_data) => Ok(
Lib3hToClientResponse::HandleFetchEntryResult(fetch_entry_result_data),
),
Lib3hClientProtocol::HandleQueryEntryResult(query_entry_result_data) => Ok(
Lib3hToClientResponse::HandleQueryEntryResult(query_entry_result_data),
),
Lib3hClientProtocol::HandleGetAuthoringEntryListResult(entry_list_data) => Ok(
Lib3hToClientResponse::HandleGetAuthoringEntryListResult(entry_list_data),
),
Lib3hClientProtocol::HandleGetGossipingEntryListResult(entry_list_data) => Ok(
Lib3hToClientResponse::HandleGetGossipingEntryListResult(entry_list_data),
),
variant => Err(Lib3hProtocolError::new(ErrorKind::Other(format!(
"{:?} can't convert to Lib3hToClientResponse",
variant
)))),
}
}
}
impl TryFrom<Lib3hServerProtocol> for Lib3hToClient {
type Error = Lib3hProtocolError;
fn try_from(c: Lib3hServerProtocol) -> Result<Self, Self::Error> {
match c {
Lib3hServerProtocol::Connected(connected_data) => {
Ok(Lib3hToClient::Connected(connected_data))
}
Lib3hServerProtocol::Disconnected(_disconnected_data) => {
Ok(Lib3hToClient::Unbound(UnboundData {
uri: Lib3hUri::with_undefined(),
}))
}
Lib3hServerProtocol::SendDirectMessageResult(direct_message_data) => {
Ok(Lib3hToClient::SendDirectMessageResult(direct_message_data))
}
Lib3hServerProtocol::HandleSendDirectMessage(direct_message_data) => {
Ok(Lib3hToClient::HandleSendDirectMessage(direct_message_data))
}
Lib3hServerProtocol::HandleFetchEntry(fetch_entry_data) => {
Ok(Lib3hToClient::HandleFetchEntry(fetch_entry_data))
}
Lib3hServerProtocol::HandleStoreEntryAspect(store_entry_aspect_data) => Ok(
Lib3hToClient::HandleStoreEntryAspect(store_entry_aspect_data),
),
Lib3hServerProtocol::HandleDropEntry(drop_entry_data) => {
Ok(Lib3hToClient::HandleDropEntry(drop_entry_data))
}
Lib3hServerProtocol::HandleQueryEntry(query_entry_data) => {
Ok(Lib3hToClient::HandleQueryEntry(query_entry_data))
}
Lib3hServerProtocol::HandleGetAuthoringEntryList(get_list_data) => {
Ok(Lib3hToClient::HandleGetAuthoringEntryList(get_list_data))
}
Lib3hServerProtocol::HandleGetGossipingEntryList(get_list_data) => {
Ok(Lib3hToClient::HandleGetGossipingEntryList(get_list_data))
}
variant => Err(Lib3hProtocolError::new(ErrorKind::Other(format!(
"{:?} can't convert to Lib3hToClient",
variant
)))),
}
}
}
impl From<Lib3hServerProtocol> for ClientToLib3hResponse {
fn from(c: Lib3hServerProtocol) -> Self {
match c {
Lib3hServerProtocol::SendDirectMessageResult(direct_message_data) => {
ClientToLib3hResponse::SendDirectMessageResult(direct_message_data)
}
Lib3hServerProtocol::FetchEntryResult(fetch_entry_result_data) => {
ClientToLib3hResponse::FetchEntryResult(fetch_entry_result_data)
}
Lib3hServerProtocol::QueryEntryResult(query_entry_result_data) => {
ClientToLib3hResponse::QueryEntryResult(query_entry_result_data)
}
Lib3hServerProtocol::Connected(_connected_data) => {
ClientToLib3hResponse::BootstrapSuccess
}
variant => panic!("{:?} can't convert to ClientToLib3hResponse", variant),
}
}
}
impl From<ClientToLib3h> for Lib3hClientProtocol {
fn from(c: ClientToLib3h) -> Self {
match c {
ClientToLib3h::Bootstrap(bootstrap_data) => Lib3hClientProtocol::Connect(ConnectData {
request_id: "".to_string(),
peer_location: bootstrap_data.bootstrap_uri,
network_id: bootstrap_data.network_or_space_address.into(),
}),
ClientToLib3h::JoinSpace(space_data) => Lib3hClientProtocol::JoinSpace(space_data),
ClientToLib3h::LeaveSpace(space_data) => Lib3hClientProtocol::LeaveSpace(space_data),
ClientToLib3h::SendDirectMessage(direct_message_data) => {
Lib3hClientProtocol::SendDirectMessage(direct_message_data)
}
ClientToLib3h::FetchEntry(fetch_entry_data) => {
Lib3hClientProtocol::FetchEntry(fetch_entry_data)
}
ClientToLib3h::PublishEntry(provided_entry_data) => {
Lib3hClientProtocol::PublishEntry(provided_entry_data)
}
ClientToLib3h::QueryEntry(query_entry_data) => {
Lib3hClientProtocol::QueryEntry(query_entry_data)
}
}
}
}
impl From<Lib3hToClientResponse> for Lib3hClientProtocol {
fn from(c: Lib3hToClientResponse) -> Self {
match c {
Lib3hToClientResponse::HandleSendDirectMessageResult(direct_message_data) => {
Lib3hClientProtocol::HandleSendDirectMessageResult(direct_message_data)
}
Lib3hToClientResponse::HandleFetchEntryResult(fetch_entry_result_data) => {
Lib3hClientProtocol::HandleFetchEntryResult(fetch_entry_result_data)
}
Lib3hToClientResponse::HandleQueryEntryResult(query_entry_result_data) => {
Lib3hClientProtocol::HandleQueryEntryResult(query_entry_result_data)
}
Lib3hToClientResponse::HandleGetAuthoringEntryListResult(entry_list_data) => {
Lib3hClientProtocol::HandleGetAuthoringEntryListResult(entry_list_data)
}
Lib3hToClientResponse::HandleGetGossipingEntryListResult(entry_list_data) => {
Lib3hClientProtocol::HandleGetGossipingEntryListResult(entry_list_data)
}
variant => panic!("{:?} can't convert to Lib3hClientProtocol", variant),
}
}
}
impl From<Lib3hToClient> for Lib3hServerProtocol {
fn from(c: Lib3hToClient) -> Self {
match c {
Lib3hToClient::Connected(connected_data) => {
Lib3hServerProtocol::Connected(connected_data)
}
Lib3hToClient::Unbound(_unbound_data) => {
Lib3hServerProtocol::Disconnected(DisconnectedData {
network_id: NetworkHash::default(),
})
}
Lib3hToClient::SendDirectMessageResult(direct_message_data) => {
Lib3hServerProtocol::SendDirectMessageResult(direct_message_data)
}
Lib3hToClient::HandleSendDirectMessage(direct_message_data) => {
Lib3hServerProtocol::HandleSendDirectMessage(direct_message_data)
}
Lib3hToClient::HandleFetchEntry(fetch_entry_data) => {
Lib3hServerProtocol::HandleFetchEntry(fetch_entry_data)
}
Lib3hToClient::HandleStoreEntryAspect(store_entry_aspect_data) => {
Lib3hServerProtocol::HandleStoreEntryAspect(store_entry_aspect_data)
}
Lib3hToClient::HandleDropEntry(drop_entry_data) => {
Lib3hServerProtocol::HandleDropEntry(drop_entry_data)
}
Lib3hToClient::HandleQueryEntry(query_entry_data) => {
Lib3hServerProtocol::HandleQueryEntry(query_entry_data)
}
Lib3hToClient::HandleGetAuthoringEntryList(get_list_data) => {
Lib3hServerProtocol::HandleGetAuthoringEntryList(get_list_data)
}
Lib3hToClient::HandleGetGossipingEntryList(get_list_data) => {
Lib3hServerProtocol::HandleGetGossipingEntryList(get_list_data)
}
}
}
}
impl From<ClientToLib3hResponse> for Lib3hServerProtocol {
fn from(c: ClientToLib3hResponse) -> Self {
match c {
ClientToLib3hResponse::SendDirectMessageResult(direct_message_data) => {
Lib3hServerProtocol::SendDirectMessageResult(direct_message_data)
}
ClientToLib3hResponse::FetchEntryResult(fetch_entry_result_data) => {
Lib3hServerProtocol::FetchEntryResult(fetch_entry_result_data)
}
ClientToLib3hResponse::QueryEntryResult(query_entry_result_data) => {
Lib3hServerProtocol::QueryEntryResult(query_entry_result_data)
}
ClientToLib3hResponse::BootstrapSuccess => {
Lib3hServerProtocol::Connected(ConnectedData {
request_id: String::new(),
uri: Lib3hUri::with_undefined(),
})
}
variant => panic!("{:?} can't convert to Lib3hServerProtocol", variant),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::convert::TryInto;
use url::Url;
fn connect_data() -> ConnectData {
ConnectData {
request_id: "".to_string(),
peer_location: Url::parse("wss://192.168.0.102:58081/").unwrap().into(),
network_id: "network_id".into(),
}
}
#[test]
fn test_translate_protocol() {
let d = connect_data();
let s = Lib3hClientProtocol::Connect(d.clone());
let to_c: ClientToLib3h = s.clone().try_into().expect("A ClientToLib3h protocol");
assert_eq!(
to_c,
ClientToLib3h::Bootstrap(BootstrapData {
bootstrap_uri: Url::parse("wss://192.168.0.102:58081/").unwrap().into(),
network_or_space_address: "network_id".into(),
})
);
let to_s: Lib3hClientProtocol = to_c.into();
assert_eq!(to_s, s);
}
}