oss_vizier/util.rs
1// Copyright 2022 Sebastien Soudan.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Utilities for the Vizier API.
16
17use prost::DecodeError;
18
19use crate::google::rpc::Status;
20use crate::operation;
21
22/// Error from decoding operation results.
23#[derive(thiserror::Error, Debug)]
24pub enum Error {
25 /// Error while decoding
26 #[error("{0}")]
27 DecodeError(#[from] DecodeError),
28 /// RPC error
29 #[error("Status: {}", .0.message)]
30 RPCStatus(Status),
31 /// Invalid type
32 #[error("Invalid type {0}")]
33 InvalidType(String),
34}
35
36/// Decodes the result of an operation as with the specified [`type_url`](Any.type_url) as
37/// the provided (by the generic type parameter `X`) message.
38pub fn decode_operation_result_as<X>(
39 result: operation::Result,
40 _type_url: impl AsRef<str>,
41) -> Result<X, Error>
42where
43 X: prost::Message + Default,
44{
45 match result {
46 operation::Result::Error(s) => Err(Error::RPCStatus(s)),
47 operation::Result::Response(resp) => {
48 let resp: X = X::decode(&resp.value[..])?;
49 Ok(resp)
50 }
51 }
52}