From 0d05aa0d02bd3037e81c72dcf7fa5168d5a62097 Mon Sep 17 00:00:00 2001 From: Tom Dyas Date: Wed, 7 Apr 2021 14:39:05 -0700 Subject: [PATCH] feat: Expose status constructors (#579) --- tonic/src/status.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tonic/src/status.rs b/tonic/src/status.rs index 34070e2..dcf381d 100644 --- a/tonic/src/status.rs +++ b/tonic/src/status.rs @@ -380,7 +380,8 @@ impl Status { Status::from_error(&*err.into()) } - pub(crate) fn from_header_map(header_map: &HeaderMap) -> Option { + /// Extract a `Status` from a hyper `HeaderMap`. + pub fn from_header_map(header_map: &HeaderMap) -> Option { header_map.get(GRPC_STATUS_HEADER_CODE).map(|code| { let code = Code::from_bytes(code.as_ref()); let error_message = header_map @@ -661,7 +662,10 @@ impl Code { Code::from(i) } - pub(crate) fn from_bytes(bytes: &[u8]) -> Code { + /// Convert the string representation of a `Code` (as stored, for example, in the `grpc-status` + /// header in a response) into a `Code`. Returns `Code::Unknown` if the code string is not a + /// valid gRPC status code. + pub fn from_bytes(bytes: &[u8]) -> Code { match bytes.len() { 1 => match bytes[0] { b'0' => Code::Ok,