Skip to content

Commit 4eccb97

Browse files
committed
fix: add proper hashing/eq check for Capability
1 parent 22d08dd commit 4eccb97

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

src/kernel_types.rs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::kinode::process::standard as wit;
22
use crate::{Address, ProcessId};
33
use serde::{Deserialize, Serialize};
44
use std::collections::{HashMap, HashSet};
5+
use std::hash::{Hash, Hasher};
56

67
//
78
// process-facing kernel types, used for process
@@ -41,12 +42,55 @@ pub enum Message {
4142
Response((Response, Option<Context>)),
4243
}
4344

44-
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
45+
#[derive(Clone, Debug, Serialize, Deserialize)]
4546
pub struct Capability {
4647
pub issuer: Address,
4748
pub params: String, // JSON-string
4849
}
4950

51+
impl Eq for Capability {}
52+
53+
impl PartialEq for Capability {
54+
fn eq(&self, other: &Self) -> bool {
55+
let self_json_params: serde_json::Value =
56+
serde_json::from_str(&self.params).unwrap_or_default();
57+
let other_json_params: serde_json::Value =
58+
serde_json::from_str(&other.params).unwrap_or_default();
59+
self.issuer == other.issuer && self_json_params == other_json_params
60+
}
61+
}
62+
63+
impl Hash for Capability {
64+
fn hash<H: Hasher>(&self, state: &mut H) {
65+
self.issuer.hash(state);
66+
let params: serde_json::Value = serde_json::from_str(&self.params).unwrap_or_default();
67+
params.hash(state);
68+
}
69+
}
70+
71+
impl Capability {
72+
pub fn new<T, U>(issuer: T, params: U) -> Self
73+
where
74+
T: Into<Address>,
75+
U: Into<String>,
76+
{
77+
Capability {
78+
issuer: issuer.into(),
79+
params: params.into(),
80+
}
81+
}
82+
83+
pub fn messaging<T>(issuer: T) -> Self
84+
where
85+
T: Into<Address>,
86+
{
87+
Capability {
88+
issuer: issuer.into(),
89+
params: "\"messaging\"".into(),
90+
}
91+
}
92+
}
93+
5094
impl std::fmt::Display for Capability {
5195
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
5296
write!(

0 commit comments

Comments
 (0)