Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions languages/rust/oso/src/host/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PolarValue> {
tracing::trace!({ method = %name }, "get_attr");
Expand Down
6 changes: 5 additions & 1 deletion languages/rust/oso/src/host/from_polar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ where
if let PolarValue::Instance(instance) = val {
Ok(instance.downcast::<T>(None).map_err(|e| e.user())?.clone())
} else {
Err(TypeError::expected("Instance").user())
Err(
TypeError::expected(format!("Instance of {}", std::any::type_name::<T>()))
.got(val.type_name().to_string())
.user(),
)
}
}
}
Expand Down
34 changes: 34 additions & 0 deletions languages/rust/oso/src/host/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}
}