use alloc::string::String;
use alloc::vec::Vec;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UserExceptionBase {
pub repository_id: String,
pub marshaled_exception: Vec<u8>,
}
impl UserExceptionBase {
#[must_use]
pub fn new(repository_id: impl Into<String>, marshaled_exception: Vec<u8>) -> Self {
Self {
repository_id: repository_id.into(),
marshaled_exception,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ExceptionHolder {
inner: UserExceptionBase,
}
impl ExceptionHolder {
#[must_use]
pub fn new(exception: UserExceptionBase) -> Self {
Self { inner: exception }
}
pub fn raise_exception(&self) -> Result<(), UserExceptionBase> {
Err(self.inner.clone())
}
#[must_use]
pub const fn user_exception(&self) -> &UserExceptionBase {
&self.inner
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn user_exception_base_new_stores_id_and_bytes() {
let ub = UserExceptionBase::new("IDL:Stock/InvalidStock:1.0", alloc::vec![0xDE, 0xAD]);
assert_eq!(ub.repository_id, "IDL:Stock/InvalidStock:1.0");
assert_eq!(ub.marshaled_exception, alloc::vec![0xDE, 0xAD]);
}
#[test]
fn raise_exception_returns_err_with_user_exception() {
let ub = UserExceptionBase::new("IDL:Foo:1.0", alloc::vec![1, 2, 3]);
let holder = ExceptionHolder::new(ub.clone());
let result = holder.raise_exception();
assert_eq!(result, Err(ub));
}
#[test]
fn user_exception_accessor_returns_inner() {
let ub = UserExceptionBase::new("IDL:Bar:1.0", alloc::vec![]);
let holder = ExceptionHolder::new(ub.clone());
assert_eq!(holder.user_exception(), &ub);
}
}