diff --git a/cas/grpc_service/BUILD b/cas/grpc_service/BUILD index 60b5a3032..43ba7c4b8 100644 --- a/cas/grpc_service/BUILD +++ b/cas/grpc_service/BUILD @@ -11,6 +11,7 @@ rust_library( "//third_party:futures_core", "//third_party:stdext", "//cas/store", + "//util:common", ], visibility = ["//cas:__pkg__"] ) diff --git a/cas/grpc_service/cas_server.rs b/cas/grpc_service/cas_server.rs index 1d52c2051..12e64773e 100644 --- a/cas/grpc_service/cas_server.rs +++ b/cas/grpc_service/cas_server.rs @@ -8,8 +8,9 @@ use std::pin::Pin; use futures_core::Stream; use tokio::io::Error; -use tonic::{Code, Request, Response, Status}; +use tonic::{Request, Response, Status}; +use common; use macros::{error_if, make_err}; use proto::build::bazel::remote::execution::v2::{ batch_update_blobs_response, content_addressable_storage_server::ContentAddressableStorage, @@ -20,49 +21,6 @@ use proto::build::bazel::remote::execution::v2::{ }; use store::Store; -// use tokio::io::Error; -// use tonic::Code; -use proto::google::rpc::Status as GrpcStatus; -use std::result::Result; -fn result_to_status(result: Result<(), Error>) -> GrpcStatus { - use tokio::io::ErrorKind; - fn kind_to_code(kind: &ErrorKind) -> Code { - match kind { - ErrorKind::NotFound => Code::NotFound, - ErrorKind::PermissionDenied => Code::PermissionDenied, - ErrorKind::ConnectionRefused => Code::Unavailable, - ErrorKind::ConnectionReset => Code::Unavailable, - ErrorKind::ConnectionAborted => Code::Unavailable, - ErrorKind::NotConnected => Code::Internal, - ErrorKind::AddrInUse => Code::Internal, - ErrorKind::AddrNotAvailable => Code::Internal, - ErrorKind::BrokenPipe => Code::Internal, - ErrorKind::AlreadyExists => Code::AlreadyExists, - ErrorKind::WouldBlock => Code::Internal, - ErrorKind::InvalidInput => Code::InvalidArgument, - ErrorKind::InvalidData => Code::InvalidArgument, - ErrorKind::TimedOut => Code::DeadlineExceeded, - ErrorKind::WriteZero => Code::Internal, - ErrorKind::Interrupted => Code::Aborted, - ErrorKind::Other => Code::Internal, - ErrorKind::UnexpectedEof => Code::Internal, - _ => Code::Internal, - } - } - match result { - Ok(()) => GrpcStatus { - code: Code::Ok as i32, - message: "".to_string(), - details: vec![], - }, - Err(error) => GrpcStatus { - code: kind_to_code(&error.kind()) as i32, - message: format!("Error: {:?}", error), - details: vec![], - }, - } -} - #[derive(Debug)] pub struct CasServer { pub store: Box, @@ -110,10 +68,10 @@ impl ContentAddressableStorage for CasServer { let result_status: Result<(), Error> = try { let digest = request .digest - .ok_or_else(|| make_err!("Digest not found in request"))?; - let size_bytes = usize::try_from(digest.size_bytes).or_else(|_| { - Err(make_err!("Digest size_bytes was not convertable to usize")) - })?; + .ok_or(make_err!("Digest not found in request"))?; + let size_bytes = usize::try_from(digest.size_bytes).or(Err(make_err!( + "Digest size_bytes was not convertable to usize" + )))?; error_if!( size_bytes != request.data.len(), "Digest for upload had mismatching sizes, digest said {} data said {}", @@ -130,7 +88,7 @@ impl ContentAddressableStorage for CasServer { }; let response = batch_update_blobs_response::Response { digest: orig_digest, - status: Some(result_to_status(result_status)), + status: Some(common::result_to_status(result_status)), }; batch_response.responses.push(response); } diff --git a/util/BUILD b/util/BUILD index 86363502d..4d00bd621 100644 --- a/util/BUILD +++ b/util/BUILD @@ -10,3 +10,14 @@ rust_library( ], visibility = ["//visibility:public"], ) + +rust_library( + name = "common", + srcs = ["common.rs"], + deps = [ + "//third_party:tokio", + "//third_party:tonic", + "//proto", + ], + visibility = ["//visibility:public"], +) diff --git a/util/common.rs b/util/common.rs new file mode 100644 index 000000000..b7d4429d8 --- /dev/null +++ b/util/common.rs @@ -0,0 +1,46 @@ +// Copyright 2020 Nathan (Blaise) Bruer. All rights reserved. + +use std::result::Result; + +use tokio::io::{Error, ErrorKind}; + +use proto::google::rpc::Status as GrpcStatus; +use tonic::Code; + +pub fn result_to_status(result: Result<(), Error>) -> GrpcStatus { + fn kind_to_code(kind: &ErrorKind) -> Code { + match kind { + ErrorKind::NotFound => Code::NotFound, + ErrorKind::PermissionDenied => Code::PermissionDenied, + ErrorKind::ConnectionRefused => Code::Unavailable, + ErrorKind::ConnectionReset => Code::Unavailable, + ErrorKind::ConnectionAborted => Code::Unavailable, + ErrorKind::NotConnected => Code::Internal, + ErrorKind::AddrInUse => Code::Internal, + ErrorKind::AddrNotAvailable => Code::Internal, + ErrorKind::BrokenPipe => Code::Internal, + ErrorKind::AlreadyExists => Code::AlreadyExists, + ErrorKind::WouldBlock => Code::Internal, + ErrorKind::InvalidInput => Code::InvalidArgument, + ErrorKind::InvalidData => Code::InvalidArgument, + ErrorKind::TimedOut => Code::DeadlineExceeded, + ErrorKind::WriteZero => Code::Internal, + ErrorKind::Interrupted => Code::Aborted, + ErrorKind::Other => Code::Internal, + ErrorKind::UnexpectedEof => Code::Internal, + _ => Code::Internal, + } + } + match result { + Ok(()) => GrpcStatus { + code: Code::Ok as i32, + message: "".to_string(), + details: vec![], + }, + Err(error) => GrpcStatus { + code: kind_to_code(&error.kind()) as i32, + message: format!("Error: {:?}", error), + details: vec![], + }, + } +}