Skip to content

Commit 830a86c

Browse files
authored
Merge pull request #79 from kinode-dao/dr/kernel-command-response
add KernelPrintResponse, ProcessMap, and update PersistedProcess to match runtime
2 parents b492f3b + 9998921 commit 830a86c

File tree

4 files changed

+73
-15
lines changed

4 files changed

+73
-15
lines changed

src/kernel_types.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ pub struct Capability {
4747
pub params: String, // JSON-string
4848
}
4949

50+
impl std::fmt::Display for Capability {
51+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
52+
write!(
53+
f,
54+
"{}({})",
55+
self.issuer,
56+
serde_json::from_str::<serde_json::Value>(&self.params)
57+
.unwrap_or(serde_json::json!("invalid JSON in capability"))
58+
)
59+
}
60+
}
61+
5062
#[derive(Clone, Debug, Serialize, Deserialize)]
5163
pub struct SendError {
5264
pub kind: SendErrorKind,
@@ -139,16 +151,27 @@ pub enum KernelResponse {
139151
StartedProcess,
140152
RunProcessError,
141153
KilledProcess(ProcessId),
154+
Debug(KernelPrintResponse),
142155
}
143156

157+
#[derive(Debug, Serialize, Deserialize)]
158+
pub enum KernelPrintResponse {
159+
ProcessMap(ProcessMap),
160+
Process(Option<PersistedProcess>),
161+
HasCap(Option<bool>),
162+
}
163+
164+
pub type ProcessMap = HashMap<ProcessId, PersistedProcess>;
165+
166+
// NOTE: this is different from the runtime representation of a process
167+
// in that the capabilities are stored as a HashSet instead of a HashMap.
144168
#[derive(Clone, Debug, Serialize, Deserialize)]
145169
pub struct PersistedProcess {
146170
pub wasm_bytes_handle: String,
147-
// pub drive: String,
148-
// pub full_path: String,
171+
pub wit_version: Option<u32>,
149172
pub on_exit: OnExit,
150173
pub capabilities: HashSet<Capability>,
151-
pub public: bool, // marks if a process allows messages from any process
174+
pub public: bool,
152175
}
153176

154177
#[derive(Serialize, Deserialize, Debug)]

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use serde_json::Value;
1919

2020
wit_bindgen::generate!({
2121
path: "kinode-wit",
22+
generate_unused_types: true,
2223
world: "lib",
2324
});
2425

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: 43 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,25 @@ 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+
3447
impl std::str::FromStr for PackageId {
3548
type Err = ProcessIdParseError;
3649
/// Attempt to parse a `PackageId` from a string. The string must
@@ -64,8 +77,29 @@ impl std::str::FromStr for PackageId {
6477
}
6578
}
6679

80+
impl std::hash::Hash for PackageId {
81+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
82+
self.package_name.hash(state);
83+
self.publisher_node.hash(state);
84+
}
85+
}
86+
6787
impl std::fmt::Display for PackageId {
6888
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
6989
write!(f, "{}:{}", self.package_name, self.publisher_node)
7090
}
7191
}
92+
93+
impl From<(&str, &str)> for PackageId {
94+
fn from(input: (&str, &str)) -> Self {
95+
PackageId::new(input.0, input.1)
96+
}
97+
}
98+
99+
impl std::cmp::Eq for PackageId {}
100+
101+
impl PartialEq for PackageId {
102+
fn eq(&self, other: &Self) -> bool {
103+
self.package_name == other.package_name && self.publisher_node == other.publisher_node
104+
}
105+
}

0 commit comments

Comments
 (0)