From f5c09403b8abc16d3c1664a0de87d35e8175d913 Mon Sep 17 00:00:00 2001 From: Patrick Elsen Date: Tue, 2 May 2023 11:55:35 +0200 Subject: [PATCH 1/2] Improves error reporting --- languages/rust/oso/src/host/class.rs | 4 +++ languages/rust/oso/src/host/from_polar.rs | 6 +++- languages/rust/oso/src/host/value.rs | 34 +++++++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/languages/rust/oso/src/host/class.rs b/languages/rust/oso/src/host/class.rs index 3d60856cca..d4cd58cc26 100644 --- a/languages/rust/oso/src/host/class.rs +++ b/languages/rust/oso/src/host/class.rs @@ -379,6 +379,10 @@ impl Instance { .unwrap_or_else(|_| self.debug_type_name) } + pub(crate) fn debug_name(&self) -> &'static str { + &self.debug_type_name + } + /// Lookup an attribute on the instance via the registered `Class` pub fn get_attr(&self, name: &str, host: &mut Host) -> crate::Result { tracing::trace!({ method = %name }, "get_attr"); diff --git a/languages/rust/oso/src/host/from_polar.rs b/languages/rust/oso/src/host/from_polar.rs index 2b3caaf391..365f77417d 100644 --- a/languages/rust/oso/src/host/from_polar.rs +++ b/languages/rust/oso/src/host/from_polar.rs @@ -71,7 +71,11 @@ where if let PolarValue::Instance(instance) = val { Ok(instance.downcast::(None).map_err(|e| e.user())?.clone()) } else { - Err(TypeError::expected("Instance").user()) + Err( + TypeError::expected(format!("Instance of {}", std::any::type_name::())) + .got(val.type_name().to_string()) + .user(), + ) } } } diff --git a/languages/rust/oso/src/host/value.rs b/languages/rust/oso/src/host/value.rs index 487b8b2c1c..e4198cd713 100644 --- a/languages/rust/oso/src/host/value.rs +++ b/languages/rust/oso/src/host/value.rs @@ -127,4 +127,38 @@ This may mean you performed an operation in your policy over an unbound variable }; Term::new_from_ffi(value) } + + pub fn type_name(&self) -> PolarValueType { + match self { + PolarValue::Integer(_) => PolarValueType::Integer, + PolarValue::Float(_) => PolarValueType::Float, + PolarValue::String(_) => PolarValueType::String, + PolarValue::Boolean(_) => PolarValueType::Boolean, + PolarValue::Map(_) => PolarValueType::Map, + PolarValue::List(_) => PolarValueType::List, + PolarValue::Variable(_) => PolarValueType::Variable, + PolarValue::Instance(i) => PolarValueType::Instance(i.debug_name()), + } + } +} + +#[derive(Clone, Debug)] +pub enum PolarValueType { + Integer, + Float, + String, + Boolean, + Map, + List, + Variable, + Instance(&'static str), +} + +impl std::fmt::Display for PolarValueType { + fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + PolarValueType::Instance(name) => write!(fmt, "Instance<{}>", name), + _ => std::fmt::Debug::fmt(self, fmt), + } + } } From 2e14be3e3ec6af31766c8ef28b7cf5bb8837b245 Mon Sep 17 00:00:00 2001 From: Ali Somay Date: Tue, 2 May 2023 15:24:55 +0200 Subject: [PATCH 2/2] Make clippy happy --- languages/rust/oso/src/host/class.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/languages/rust/oso/src/host/class.rs b/languages/rust/oso/src/host/class.rs index d4cd58cc26..cc441df103 100644 --- a/languages/rust/oso/src/host/class.rs +++ b/languages/rust/oso/src/host/class.rs @@ -380,7 +380,7 @@ impl Instance { } pub(crate) fn debug_name(&self) -> &'static str { - &self.debug_type_name + self.debug_type_name } /// Lookup an attribute on the instance via the registered `Class`