Skip to content

Commit 38c1487

Browse files
committed
update wit
1 parent 010e175 commit 38c1487

File tree

3 files changed

+39
-13
lines changed

3 files changed

+39
-13
lines changed

kinode-wit

src/types/address.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pub use crate::{Address, PackageId, ProcessId};
1+
pub use crate::{Address, ProcessId};
22
use serde::{Deserialize, Serialize};
33
use std::hash::{Hash, Hasher};
44

@@ -39,8 +39,8 @@ impl Address {
3939
&self.process.publisher_node
4040
}
4141
/// Read the package_id (package + publisher) from an `Address`.
42-
pub fn package_id(&self) -> PackageId {
43-
PackageId::new(self.package(), self.publisher())
42+
pub fn package_id(&self) -> crate::PackageId {
43+
crate::PackageId::new(self.package(), self.publisher())
4444
}
4545
}
4646

src/types/package_id.rs

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
1+
pub use crate::PackageId;
12
use crate::ProcessIdParseError;
23
use serde::{Deserialize, Serialize};
3-
use std::hash::Hash;
4-
5-
/// PackageId is like a ProcessId, but for a package. Only contains the name
6-
/// of the package and the name of the publisher.
7-
#[derive(Hash, Eq, PartialEq, Debug, Clone, Serialize, Deserialize)]
8-
pub struct PackageId {
9-
package_name: String,
10-
publisher_node: String,
11-
}
124

5+
/// `PackageId` is defined in the wit bindings, but constructors and methods
6+
/// are defined here. A `PackageId` contains a package name and a publisher node ID.
137
impl PackageId {
148
/// Create a new `PackageId`.
159
pub fn new(package_name: &str, publisher_node: &str) -> Self {
@@ -31,6 +25,32 @@ impl PackageId {
3125
}
3226
}
3327

28+
impl Serialize for PackageId {
29+
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
30+
where
31+
S: serde::ser::Serializer,
32+
{
33+
format!("{}", self).serialize(serializer)
34+
}
35+
}
36+
37+
impl<'a> Deserialize<'a> for PackageId {
38+
fn deserialize<D>(deserializer: D) -> Result<PackageId, D::Error>
39+
where
40+
D: serde::de::Deserializer<'a>,
41+
{
42+
let s = String::deserialize(deserializer)?;
43+
s.parse().map_err(serde::de::Error::custom)
44+
}
45+
}
46+
47+
impl std::hash::Hash for PackageId {
48+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
49+
self.package_name.hash(state);
50+
self.publisher_node.hash(state);
51+
}
52+
}
53+
3454
impl std::str::FromStr for PackageId {
3555
type Err = ProcessIdParseError;
3656
/// Attempt to parse a `PackageId` from a string. The string must
@@ -69,3 +89,9 @@ impl std::fmt::Display for PackageId {
6989
write!(f, "{}:{}", self.package_name, self.publisher_node)
7090
}
7191
}
92+
93+
impl PartialEq for PackageId {
94+
fn eq(&self, other: &Self) -> bool {
95+
self.package_name == other.package_name && self.publisher_node == other.publisher_node
96+
}
97+
}

0 commit comments

Comments
 (0)