artichoke_backend/
platform_string.rs

1use std::borrow::Cow;
2
3use artichoke_core::convert::TryConvertMut;
4use scolapasta_path::ConvertBytesError;
5use spinoso_exception::ArgumentError;
6
7use crate::Artichoke;
8use crate::core::ClassRegistry;
9use crate::error::{Error, RubyException};
10use crate::sys;
11
12impl RubyException for ConvertBytesError {
13    fn message(&self) -> Cow<'_, [u8]> {
14        Cow::Borrowed(b"invalid byte sequence")
15    }
16
17    fn name(&self) -> Cow<'_, str> {
18        "ArgumentError".into()
19    }
20
21    fn vm_backtrace(&self, interp: &mut Artichoke) -> Option<Vec<Vec<u8>>> {
22        let _ = interp;
23        None
24    }
25
26    fn as_mrb_value(&self, interp: &mut Artichoke) -> Option<sys::mrb_value> {
27        let message = interp.try_convert_mut(self.message()).ok()?;
28        let value = interp.new_instance::<ArgumentError>(&[message]).ok().flatten()?;
29        Some(value.inner())
30    }
31}
32
33impl From<ConvertBytesError> for Error {
34    fn from(exception: ConvertBytesError) -> Self {
35        let err: Box<dyn RubyException> = Box::new(exception);
36        Self::from(err)
37    }
38}
39
40#[cfg(test)]
41mod tests {
42    use super::ConvertBytesError;
43    use crate::error::{Error, RubyException};
44
45    #[test]
46    fn box_convert() {
47        // Prevents regressing on a stack overflow caused by a mutual recursion.
48        let err = Error::from(ConvertBytesError::new());
49        assert_eq!(err.name(), "ArgumentError");
50    }
51}