Skip to content

Commit

Permalink
fix: add proper hashing/eq check for Capability
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-frmr committed Aug 13, 2024
1 parent 22d08dd commit 4eccb97
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion src/kernel_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::kinode::process::standard as wit;
use crate::{Address, ProcessId};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::hash::{Hash, Hasher};

//
// process-facing kernel types, used for process
Expand Down Expand Up @@ -41,12 +42,55 @@ pub enum Message {
Response((Response, Option<Context>)),
}

#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Capability {
pub issuer: Address,
pub params: String, // JSON-string
}

impl Eq for Capability {}

impl PartialEq for Capability {
fn eq(&self, other: &Self) -> bool {
let self_json_params: serde_json::Value =
serde_json::from_str(&self.params).unwrap_or_default();
let other_json_params: serde_json::Value =
serde_json::from_str(&other.params).unwrap_or_default();
self.issuer == other.issuer && self_json_params == other_json_params
}
}

impl Hash for Capability {
fn hash<H: Hasher>(&self, state: &mut H) {
self.issuer.hash(state);
let params: serde_json::Value = serde_json::from_str(&self.params).unwrap_or_default();
params.hash(state);
}
}

impl Capability {
pub fn new<T, U>(issuer: T, params: U) -> Self
where
T: Into<Address>,
U: Into<String>,
{
Capability {
issuer: issuer.into(),
params: params.into(),
}
}

pub fn messaging<T>(issuer: T) -> Self
where
T: Into<Address>,
{
Capability {
issuer: issuer.into(),
params: "\"messaging\"".into(),
}
}
}

impl std::fmt::Display for Capability {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
Expand Down

0 comments on commit 4eccb97

Please sign in to comment.