Skip to content

Commit

Permalink
add try_attach_all method alongside attach_all
Browse files Browse the repository at this point in the history
  • Loading branch information
nick1udwig committed Dec 9, 2024
1 parent d500ce6 commit 5d6e2af
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 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
11 changes: 9 additions & 2 deletions src/types/request.rs
Original file line number Diff line number Diff line change
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 @@ -242,6 +242,13 @@ impl Request {
}]);
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();
Expand Down
7 changes: 7 additions & 0 deletions src/types/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,13 @@ 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();
Expand Down

0 comments on commit 5d6e2af

Please sign in to comment.