Skip to content

Commit

Permalink
Merge pull request #120 from kinode-dao/hf/add-attach-all-method
Browse files Browse the repository at this point in the history
request,response: add `attach_all()` method
  • Loading branch information
nick1udwig authored Dec 9, 2024
2 parents 6ed0cda + 5d6e2af commit 2aac31a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 18 deletions.
16 changes: 4 additions & 12 deletions src/types/message.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{Address, Capability, LazyLoadBlob, ProcessId};
use serde::{Deserialize, Serialize};
use thiserror::Error;

/// The basic `Message` type.
/// A `Message` is either a [`crate::Request`] or a [`crate::Response`].
Expand All @@ -26,23 +27,14 @@ pub enum Message {
},
}

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Error, Serialize, Deserialize)]
pub enum BuildError {
#[error("no body set for message")]
NoBody,
#[error("no target set for message")]
NoTarget,
}

impl std::fmt::Display for BuildError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
BuildError::NoBody => write!(f, "no body set for message"),
BuildError::NoTarget => write!(f, "no target set for message"),
}
}
}

impl std::error::Error for BuildError {}

impl Message {
/// Get the `source` [`Address`] of a `Message`.
pub fn source(&self) -> &Address {
Expand Down
29 changes: 24 additions & 5 deletions src/types/request.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
Address, Capability, LazyLoadBlob, Message, SendError, _wit_message_to_message,
_wit_send_error_to_send_error, types::message::BuildError,
our_capabilities, Address, Capability, LazyLoadBlob, Message, SendError,
_wit_message_to_message, _wit_send_error_to_send_error, types::message::BuildError,
};

/// `Request` builder. Use [`Request::new()`] or [`Request::to()`] to start a request,
Expand Down Expand Up @@ -34,7 +34,7 @@ impl Request {
capabilities: vec![],
}
}
/// Start building a new Request with the `target` [`Address`]. In order
/// Start building a new `Request` with the `target` [`Address`]. In order
/// to successfully send, you must still fill out at least the `body` field
/// by calling [`Request::body()`] or [`Request::try_body()`] next.
pub fn to<T>(target: T) -> Self
Expand All @@ -52,7 +52,7 @@ impl Request {
capabilities: vec![],
}
}
/// Set the target [`Address`] that this request will go to.
/// Set the `target` [`Address`] that this `Request` will go to.
pub fn target<T>(mut self, target: T) -> Self
where
T: Into<Address>,
Expand Down Expand Up @@ -235,11 +235,30 @@ impl Request {
self
}
/// Attach the [`Capability`] to message this process to the next message.
pub fn attach_messaging(mut self, our: &Address) {
pub fn attach_messaging(mut self, our: &Address) -> Self {
self.capabilities.extend(vec![Capability {
issuer: our.clone(),
params: "\"messaging\"".to_string(),
}]);
self
}
/// Attach all capabilities we have that were issued by `target` (if set) to the next message.
pub fn try_attach_all(mut self) -> Result<Self, BuildError> {
let Some(ref target) = self.target else {
return Err(BuildError::NoTarget);
};
Ok(self.attach_all(target))
}
/// Attach all capabilities we have that were issued by `target` to the next message.
pub fn attach_all(mut self, target: &Address) -> Self {
let target = target.clone();
self.capabilities.extend(
our_capabilities()
.into_iter()
.filter(|cap| cap.issuer == target)
.collect::<Vec<_>>(),
);
self
}
/// Attempt to send the `Request`. This will only fail if the `target` or `body`
/// fields have not been set.
Expand Down
20 changes: 19 additions & 1 deletion src/types/response.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{types::message::BuildError, Capability, LazyLoadBlob};
use crate::{our_capabilities, types::message::BuildError, Address, Capability, LazyLoadBlob};

/// `Response` builder. Use [`Response::new()`] to start a `Response`, then build it,
/// then call [`Response::send()`] on it to fire.
Expand Down Expand Up @@ -150,6 +150,24 @@ impl Response {
self.capabilities = capabilities;
self
}
/// Attach all capabilities we have that were issued by `target` (if set) to the next message.
pub fn try_attach_all(mut self) -> Result<Self, BuildError> {
let Some(ref target) = self.target else {
return Err(BuildError::NoTarget);
};
Ok(self.attach_all(target))
}
/// Attach all capabilities we have that were issued by `target` to the next message.
pub fn attach_all(mut self, target: &Address) -> Self {
let target = target.clone();
self.capabilities.extend(
our_capabilities()
.into_iter()
.filter(|cap| cap.issuer == target)
.collect::<Vec<_>>(),
);
self
}
/// Attempt to send the `Response`. This will only fail if the IPC body field of
/// the `Response` has not yet been set using `body()` or `try_body()`.
pub fn send(self) -> Result<(), BuildError> {
Expand Down

0 comments on commit 2aac31a

Please sign in to comment.