[refactoring] Group all the CBOR util functions in cbor_util
This cl only moves all the CBOR utility functions in one place.
Some return error types are adjusted to adapt the change. There
should be no behavior change.
Bug: 310931749
Test: atest rialto_test
Change-Id: Ie3db9c93b1355d25d7d796818e19e554560e1f23
diff --git a/service_vm/requests/src/dice.rs b/service_vm/requests/src/dice.rs
index 15cfbc9..557b678 100644
--- a/service_vm/requests/src/dice.rs
+++ b/service_vm/requests/src/dice.rs
@@ -16,7 +16,11 @@
use alloc::string::String;
use alloc::vec::Vec;
-use ciborium::value::{Integer, Value};
+use cbor_util::{
+ cbor_value_type, value_to_array, value_to_byte_array, value_to_bytes, value_to_map,
+ value_to_num, value_to_text,
+};
+use ciborium::value::Value;
use core::cell::OnceCell;
use core::result;
use coset::{
@@ -24,9 +28,7 @@
};
use diced_open_dice::{DiceMode, HASH_SIZE};
use log::error;
-use service_vm_comm::{
- cbor_value_type, to_unexpected_item_error, value_to_bytes, RequestProcessingError,
-};
+use service_vm_comm::RequestProcessingError;
type Result<T> = result::Result<T, RequestProcessingError>;
@@ -399,33 +401,6 @@
}
}
-fn value_to_array(v: Value, context: &'static str) -> coset::Result<Vec<Value>> {
- v.into_array().map_err(|e| to_unexpected_item_error(&e, "array", context))
-}
-
-fn value_to_text(v: Value, context: &'static str) -> coset::Result<String> {
- v.into_text().map_err(|e| to_unexpected_item_error(&e, "tstr", context))
-}
-
-fn value_to_map(v: Value, context: &'static str) -> coset::Result<Vec<(Value, Value)>> {
- v.into_map().map_err(|e| to_unexpected_item_error(&e, "map", context))
-}
-
-fn value_to_num<T: TryFrom<Integer>>(v: Value, context: &'static str) -> Result<T> {
- let num = v.into_integer().map_err(|e| to_unexpected_item_error(&e, "int", context))?;
- num.try_into().map_err(|_| {
- error!("The provided value '{num:?}' is not a valid number: {context}");
- RequestProcessingError::InvalidDiceChain
- })
-}
-
-fn value_to_byte_array<const N: usize>(v: Value, context: &'static str) -> Result<[u8; N]> {
- value_to_bytes(v, context)?.try_into().map_err(|e| {
- error!("The provided value '{context}' is not an array of length {N}: {e:?}");
- RequestProcessingError::InternalError
- })
-}
-
fn to_mode(value: Value) -> Result<DiceMode> {
let mode = match value {
// Mode is supposed to be encoded as a 1-byte bstr, but some implementations instead