Skip to content

Commit

Permalink
Move macros to //util folder
Browse files Browse the repository at this point in the history
  • Loading branch information
allada committed Dec 26, 2020
1 parent 4f8e5a7 commit 2fe5d30
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 18 deletions.
1 change: 1 addition & 0 deletions cas/store/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ rust_library(
":traits",
"//third_party:tokio",
"//third_party:hex",
"//util:macros",
],
proc_macro_deps = ["//third_party:async_trait"],
visibility = ["//cas:__pkg__"]
Expand Down
27 changes: 9 additions & 18 deletions cas/store/memory_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt, Error, Error

use traits::StoreTrait;

use macros::{error_if, make_err};

#[derive(Debug)]
pub struct MemoryStore {
map: HashMap<[u8; 32], Arc<Vec<u8>>>,
Expand All @@ -22,16 +24,6 @@ impl MemoryStore {
}
}

macro_rules! make_err {
($($arg:tt)+) => {{
Error::new(
ErrorKind::InvalidInput,
format!("{}", format_args!($($arg)+)
),
)
}};
}

#[async_trait]
impl StoreTrait for MemoryStore {
async fn has(&self, hash: &str, _expected_size: usize) -> Result<bool, Error> {
Expand All @@ -50,14 +42,13 @@ impl StoreTrait for MemoryStore {
.or_else(|_| Err(make_err!("Hex length is not 64 hex characters")))?;
let mut buffer = Vec::new();
let read_size = reader.read_to_end(&mut buffer).await?;
if read_size != expected_size {
return Err(make_err!(
"Expected size {} but got size {} for hash {} CAS insert",
expected_size,
read_size,
hash
));
}
error_if!(
read_size != expected_size,
"Expected size {} but got size {} for hash {} CAS insert",
expected_size,
read_size,
hash
);
self.map.insert(raw_key, Arc::new(buffer));
Ok(())
}
Expand Down
9 changes: 9 additions & 0 deletions util/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Copyright 2020 Nathan (Blaise) Bruer. All rights reserved.

load("@io_bazel_rules_rust//rust:rust.bzl", "rust_library")

rust_library(
name = "macros",
srcs = ["macros.rs"],
visibility = ["//visibility:public"],
)
21 changes: 21 additions & 0 deletions util/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2020 Nathan (Blaise) Bruer. All rights reserved.

#[macro_export]
macro_rules! make_err {
($($arg:tt)+) => {{
Error::new(
ErrorKind::InvalidInput,
format!("{}", format_args!($($arg)+)
),
)
}};
}

#[macro_export]
macro_rules! error_if {
($cond:expr, $($arg:tt)+) => {{
if $cond {
return Err(make_err!($($arg)+));
}
}}
}

0 comments on commit 2fe5d30

Please sign in to comment.