|
| 1 | +use prost::{DecodeError, Message}; |
| 2 | +use prost_types::Any; |
| 3 | + |
| 4 | +use super::super::{pb, FromAny, IntoAny}; |
| 5 | + |
| 6 | +/// Used to encode/decode the `ResourceInfo` standard error message described |
| 7 | +/// in [error_details.proto]. Describes the resource that is being accessed. |
| 8 | +/// |
| 9 | +/// [error_details.proto]: https://github.com/googleapis/googleapis/blob/master/google/rpc/error_details.proto |
| 10 | +#[derive(Clone, Debug)] |
| 11 | +pub struct ResourceInfo { |
| 12 | + /// Type of resource being accessed. |
| 13 | + pub resource_type: String, |
| 14 | + |
| 15 | + /// Name of the resource being accessed. |
| 16 | + pub resource_name: String, |
| 17 | + |
| 18 | + /// The owner of the resource (optional). |
| 19 | + pub owner: String, |
| 20 | + |
| 21 | + /// Describes the error encountered when accessing the resource. |
| 22 | + pub description: String, |
| 23 | +} |
| 24 | + |
| 25 | +impl ResourceInfo { |
| 26 | + /// Type URL of the `ResourceInfo` standard error message type. |
| 27 | + pub const TYPE_URL: &'static str = "type.googleapis.com/google.rpc.ResourceInfo"; |
| 28 | + |
| 29 | + /// Creates a new [`ResourceInfo`] struct. |
| 30 | + pub fn new( |
| 31 | + resource_type: impl Into<String>, |
| 32 | + resource_name: impl Into<String>, |
| 33 | + owner: impl Into<String>, |
| 34 | + description: impl Into<String>, |
| 35 | + ) -> Self { |
| 36 | + ResourceInfo { |
| 37 | + resource_type: resource_type.into(), |
| 38 | + resource_name: resource_name.into(), |
| 39 | + owner: owner.into(), |
| 40 | + description: description.into(), |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + /// Returns `true` if [`ResourceInfo`] fields are empty, and `false` if |
| 45 | + /// they are not. |
| 46 | + pub fn is_empty(&self) -> bool { |
| 47 | + self.resource_type.is_empty() |
| 48 | + && self.resource_name.is_empty() |
| 49 | + && self.owner.is_empty() |
| 50 | + && self.description.is_empty() |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +impl IntoAny for ResourceInfo { |
| 55 | + fn into_any(self) -> Any { |
| 56 | + let detail_data = pb::ResourceInfo { |
| 57 | + resource_type: self.resource_type, |
| 58 | + resource_name: self.resource_name, |
| 59 | + owner: self.owner, |
| 60 | + description: self.description, |
| 61 | + }; |
| 62 | + |
| 63 | + Any { |
| 64 | + type_url: ResourceInfo::TYPE_URL.to_string(), |
| 65 | + value: detail_data.encode_to_vec(), |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +impl FromAny for ResourceInfo { |
| 71 | + fn from_any(any: Any) -> Result<Self, DecodeError> { |
| 72 | + let buf: &[u8] = &any.value; |
| 73 | + let res_info = pb::ResourceInfo::decode(buf)?; |
| 74 | + |
| 75 | + let debug_info = ResourceInfo { |
| 76 | + resource_type: res_info.resource_type, |
| 77 | + resource_name: res_info.resource_name, |
| 78 | + owner: res_info.owner, |
| 79 | + description: res_info.description, |
| 80 | + }; |
| 81 | + |
| 82 | + Ok(debug_info) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +#[cfg(test)] |
| 87 | +mod tests { |
| 88 | + use super::super::super::{FromAny, IntoAny}; |
| 89 | + use super::ResourceInfo; |
| 90 | + |
| 91 | + #[test] |
| 92 | + fn gen_error_info() { |
| 93 | + let error_info = |
| 94 | + ResourceInfo::new("resource-type", "resource-name", "owner", "description"); |
| 95 | + |
| 96 | + let formatted = format!("{:?}", error_info); |
| 97 | + |
| 98 | + let expected_filled = "ResourceInfo { resource_type: \"resource-type\", resource_name: \"resource-name\", owner: \"owner\", description: \"description\" }"; |
| 99 | + |
| 100 | + assert!( |
| 101 | + formatted.eq(expected_filled), |
| 102 | + "filled ResourceInfo differs from expected result" |
| 103 | + ); |
| 104 | + |
| 105 | + let gen_any = error_info.into_any(); |
| 106 | + |
| 107 | + let formatted = format!("{:?}", gen_any); |
| 108 | + |
| 109 | + let expected = |
| 110 | + "Any { type_url: \"type.googleapis.com/google.rpc.ResourceInfo\", value: [10, 13, 114, 101, 115, 111, 117, 114, 99, 101, 45, 116, 121, 112, 101, 18, 13, 114, 101, 115, 111, 117, 114, 99, 101, 45, 110, 97, 109, 101, 26, 5, 111, 119, 110, 101, 114, 34, 11, 100, 101, 115, 99, 114, 105, 112, 116, 105, 111, 110] }"; |
| 111 | + |
| 112 | + assert!( |
| 113 | + formatted.eq(expected), |
| 114 | + "Any from filled ResourceInfo differs from expected result" |
| 115 | + ); |
| 116 | + |
| 117 | + let br_details = match ResourceInfo::from_any(gen_any) { |
| 118 | + Err(error) => panic!("Error generating ResourceInfo from Any: {:?}", error), |
| 119 | + Ok(from_any) => from_any, |
| 120 | + }; |
| 121 | + |
| 122 | + let formatted = format!("{:?}", br_details); |
| 123 | + |
| 124 | + assert!( |
| 125 | + formatted.eq(expected_filled), |
| 126 | + "ResourceInfo from Any differs from expected result" |
| 127 | + ); |
| 128 | + } |
| 129 | +} |
0 commit comments