Skip to content

Commit

Permalink
feat(comms): add or_optional trait extension for RpcStatus
Browse files Browse the repository at this point in the history
  • Loading branch information
sdbondi committed Jun 30, 2022
1 parent ed39913 commit 487c723
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
1 change: 1 addition & 0 deletions comms/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ pub mod utils;
// TODO: Test utils should be part of a `tari_comms_test` crate
// #[cfg(test)]
pub mod test_utils;
pub mod traits;

//---------------------------------- Re-exports --------------------------------------------//
// Rather than requiring dependent crates to import dependencies for use with `tari_comms` we re-export them here.
Expand Down
30 changes: 28 additions & 2 deletions comms/core/src/protocol/rpc/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ use log::*;
use thiserror::Error;

use super::RpcError;
use crate::proto;
use crate::{proto, traits::OrOptional};

const LOG_TARGET: &str = "comms::rpc::status";

#[derive(Debug, Error, Clone)]
#[derive(Debug, Error, Clone, PartialEq, Eq)]
pub struct RpcStatus {
code: RpcStatusCode,
details: String,
Expand Down Expand Up @@ -141,6 +141,10 @@ impl RpcStatus {
pub fn is_ok(&self) -> bool {
self.code.is_ok()
}

pub fn is_not_found(&self) -> bool {
self.code.is_not_found()
}
}

impl Display for RpcStatus {
Expand Down Expand Up @@ -202,6 +206,15 @@ impl<T, E: std::error::Error> RpcStatusResultExt<T> for Result<T, E> {
}
}

impl<T> OrOptional<T> for Result<T, RpcStatus> {
type Error = RpcStatus;

fn or_optional(self) -> Result<Option<T>, Self::Error> {
self.map(Some)
.or_else(|status| if status.is_not_found() { Ok(None) } else { Err(status) })
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RpcStatusCode {
/// Request succeeded
Expand Down Expand Up @@ -296,4 +309,17 @@ mod test {
assert_eq!(RpcStatusCode::from(Conflict as u32), Conflict);
assert_eq!(RpcStatusCode::from(123), InvalidRpcStatusCode);
}

#[test]
fn rpc_status_or_optional() {
assert!(Result::<(), RpcStatus>::Ok(()).or_optional().is_ok());
assert_eq!(
Result::<(), _>::Err(RpcStatus::not_found("foo")).or_optional(),
Ok(None)
);
assert_eq!(
Result::<(), _>::Err(RpcStatus::general("foo")).or_optional(),
Err(RpcStatus::general("foo"))
);
}
}
28 changes: 28 additions & 0 deletions comms/core/src/traits.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2022, The Tari Project
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
// following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
// disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
// following disclaimer in the documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
// products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

/// Extension trait that typically converts Result<T, Self::Error> to Result<Option<T>, Self::Error>
/// based on some implementer logic.
pub trait OrOptional<T> {
type Error;
fn or_optional(self) -> Result<Option<T>, Self::Error>;
}

0 comments on commit 487c723

Please sign in to comment.