use crate::Address;
use std::cmp::Ordering;
use url::Url;
pub type AspectKey = (Address, Address);
#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize)]
pub struct EntryAspectData {
pub aspect_address: Address,
pub type_hint: String,
#[serde(with = "base64")]
pub aspect: Vec<u8>,
pub publish_ts: u64,
}
impl Ord for EntryAspectData {
fn cmp(&self, other: &Self) -> Ordering {
self.aspect_address.cmp(&other.aspect_address)
}
}
impl PartialOrd for EntryAspectData {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct EntryData {
pub entry_address: Address,
pub aspect_list: Vec<EntryAspectData>,
}
impl EntryData {
pub fn get(&self, aspect_address: &Address) -> Option<EntryAspectData> {
for aspect in self.aspect_list.iter() {
if aspect.aspect_address == *aspect_address {
return Some(aspect.clone());
}
}
None
}
pub fn merge(&mut self, other: &EntryData) -> bool {
if self.entry_address != other.entry_address {
return false;
}
let mut to_append = Vec::new();
for aspect in other.aspect_list.iter() {
if self
.aspect_list
.iter()
.any(|a| a.aspect_address == aspect.aspect_address)
{
continue;
}
to_append.push(aspect.clone());
}
if to_append.len() == 0 {
return false;
}
self.aspect_list.append(&mut to_append);
true
}
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct GenericResultData {
pub request_id: String,
pub space_address: Address,
pub to_agent_id: Address,
#[serde(with = "base64")]
pub result_info: Vec<u8>,
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct ConnectData {
pub request_id: String,
#[serde(with = "url_serde")]
pub peer_uri: Url,
pub network_id: String,
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct ConnectedData {
pub request_id: String,
#[serde(with = "url_serde")]
pub uri: Url,
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct DisconnectedData {
pub network_id: String,
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct SpaceData {
pub request_id: String,
pub space_address: Address,
pub agent_id: Address,
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct DirectMessageData {
pub space_address: Address,
pub request_id: String,
pub to_agent_id: Address,
pub from_agent_id: Address,
#[serde(with = "base64")]
pub content: Vec<u8>,
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct QueryEntryData {
pub space_address: Address,
pub entry_address: Address,
pub request_id: String,
pub requester_agent_id: Address,
#[serde(with = "base64")]
pub query: Vec<u8>,
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct QueryEntryResultData {
pub space_address: Address,
pub entry_address: Address,
pub request_id: String,
pub requester_agent_id: Address,
pub responder_agent_id: Address,
#[serde(with = "base64")]
pub query_result: Vec<u8>,
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct ProvidedEntryData {
pub space_address: Address,
pub provider_agent_id: Address,
pub entry: EntryData,
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct StoreEntryAspectData {
pub request_id: String,
pub space_address: Address,
pub provider_agent_id: Address,
pub entry_address: Address,
pub entry_aspect: EntryAspectData,
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct DropEntryData {
pub space_address: Address,
pub request_id: String,
pub entry_address: Address,
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct FetchEntryData {
pub space_address: Address,
pub entry_address: Address,
pub request_id: String,
pub provider_agent_id: Address,
pub aspect_address_list: Option<Vec<Address>>,
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct FetchEntryResultData {
pub space_address: Address,
pub provider_agent_id: Address,
pub request_id: String,
pub entry: EntryData,
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct GetListData {
pub space_address: Address,
pub provider_agent_id: Address,
pub request_id: String,
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct EntryListData {
pub space_address: Address,
pub provider_agent_id: Address,
pub request_id: String,
pub address_map: std::collections::HashMap<Address, Vec<Address>>,
}
mod base64 {
extern crate base64;
use serde::{de, Deserialize, Deserializer, Serializer};
pub fn serialize<S>(bytes: &[u8], serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.collect_str(&base64::display::Base64Display::with_config(
bytes,
base64::STANDARD,
))
}
pub fn deserialize<'de, D>(deserializer: D) -> Result<Vec<u8>, D::Error>
where
D: Deserializer<'de>,
{
let s = <String>::deserialize(deserializer)?;
base64::decode(&s).map_err(de::Error::custom)
}
}