spinoso_exception/core/standarderror.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
// @generated
use alloc::borrow::Cow;
use alloc::string::String;
use alloc::vec::Vec;
use core::fmt;
#[cfg(feature = "std")]
use std::error;
use scolapasta_string_escape::format_debug_escape_into;
use crate::RubyException;
/// Ruby `StandardError` error type.
///
/// Descendants of class [`Exception`] are used to communicate between
/// [`Kernel#raise`] and `rescue` statements in `begin ... end` blocks.
/// Exception objects carry information about the exception – its type (the
/// exception's class name), an optional descriptive string, and optional
/// traceback information. `Exception` subclasses may add additional information
/// like [`NameError#name`].
///
/// [`Exception`]: https://ruby-doc.org/core-3.1.2/Exception.html
/// [`Kernel#raise`]: https://ruby-doc.org/core-3.1.2/Kernel.html#method-i-raise
/// [`NameError#name`]: https://ruby-doc.org/core-3.1.2/NameError.html#method-i-name
#[derive(Default, Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct StandardError {
message: Cow<'static, [u8]>,
}
impl StandardError {
/// Construct a new, default `StandardError` Ruby exception.
///
/// This constructor sets the exception message to `StandardError`.
///
/// # Examples
///
/// ```
/// # use spinoso_exception::*;
/// let exception = StandardError::new();
/// assert_eq!(exception.message(), b"StandardError");
/// ```
#[inline]
#[must_use]
pub const fn new() -> Self {
const DEFAULT_MESSAGE: &[u8] = b"StandardError";
// `Exception` objects initialized via (for example)
// `raise RuntimeError` or `RuntimeError.new` have `message`
// equal to the exception's class name.
let message = Cow::Borrowed(DEFAULT_MESSAGE);
Self { message }
}
/// Construct a new, `StandardError` Ruby exception with the given
/// message.
///
/// # Examples
///
/// ```
/// # use spinoso_exception::*;
/// let exception = StandardError::with_message("an error occurred");
/// assert_eq!(exception.message(), b"an error occurred");
/// ```
#[inline]
#[must_use]
pub const fn with_message(message: &'static str) -> Self {
let message = Cow::Borrowed(message.as_bytes());
Self { message }
}
/// Return the message this Ruby exception was constructed with.
///
/// # Examples
///
/// ```
/// # use spinoso_exception::*;
/// let exception = StandardError::new();
/// assert_eq!(exception.message(), b"StandardError");
/// let exception = StandardError::from("something went wrong");
/// assert_eq!(exception.message(), b"something went wrong");
/// ```
#[inline]
#[must_use]
pub fn message(&self) -> &[u8] {
self.message.as_ref()
}
/// Return this Ruby exception's class name.
///
/// # Examples
///
/// ```
/// # use spinoso_exception::*;
/// let exception = StandardError::new();
/// assert_eq!(exception.name(), "StandardError");
/// ```
#[inline]
#[must_use]
pub const fn name(&self) -> &'static str {
"StandardError"
}
}
impl From<String> for StandardError {
#[inline]
fn from(message: String) -> Self {
let message = Cow::Owned(message.into_bytes());
Self { message }
}
}
impl From<&'static str> for StandardError {
#[inline]
fn from(message: &'static str) -> Self {
let message = Cow::Borrowed(message.as_bytes());
Self { message }
}
}
impl From<Cow<'static, str>> for StandardError {
#[inline]
fn from(message: Cow<'static, str>) -> Self {
let message = match message {
Cow::Borrowed(s) => Cow::Borrowed(s.as_bytes()),
Cow::Owned(s) => Cow::Owned(s.into_bytes()),
};
Self { message }
}
}
impl From<Vec<u8>> for StandardError {
#[inline]
fn from(message: Vec<u8>) -> Self {
let message = Cow::Owned(message);
Self { message }
}
}
impl From<&'static [u8]> for StandardError {
#[inline]
fn from(message: &'static [u8]) -> Self {
let message = Cow::Borrowed(message);
Self { message }
}
}
impl From<Cow<'static, [u8]>> for StandardError {
#[inline]
fn from(message: Cow<'static, [u8]>) -> Self {
Self { message }
}
}
impl fmt::Display for StandardError {
#[inline]
fn fmt(&self, mut f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.name())?;
f.write_str(" (")?;
let message = self.message.as_ref();
format_debug_escape_into(&mut f, message)?;
f.write_str(")")?;
Ok(())
}
}
#[cfg(feature = "std")]
impl error::Error for StandardError {}
impl RubyException for StandardError {
#[inline]
fn message(&self) -> Cow<'_, [u8]> {
Cow::Borrowed(Self::message(self))
}
#[inline]
fn name(&self) -> Cow<'_, str> {
Cow::Borrowed(Self::name(self))
}
}