spinoso_exception/core/argumenterror.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 `ArgumentError` 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 ArgumentError {
message: Cow<'static, [u8]>,
}
impl ArgumentError {
/// Construct a new, default `ArgumentError` Ruby exception.
///
/// This constructor sets the exception message to `ArgumentError`.
///
/// # Examples
///
/// ```
/// # use spinoso_exception::*;
/// let exception = ArgumentError::new();
/// assert_eq!(exception.message(), b"ArgumentError");
/// ```
#[inline]
#[must_use]
pub const fn new() -> Self {
const DEFAULT_MESSAGE: &[u8] = b"ArgumentError";
// `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, `ArgumentError` Ruby exception with the given
/// message.
///
/// # Examples
///
/// ```
/// # use spinoso_exception::*;
/// let exception = ArgumentError::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 = ArgumentError::new();
/// assert_eq!(exception.message(), b"ArgumentError");
/// let exception = ArgumentError::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 = ArgumentError::new();
/// assert_eq!(exception.name(), "ArgumentError");
/// ```
#[inline]
#[must_use]
pub const fn name(&self) -> &'static str {
"ArgumentError"
}
}
impl From<String> for ArgumentError {
#[inline]
fn from(message: String) -> Self {
let message = Cow::Owned(message.into_bytes());
Self { message }
}
}
impl From<&'static str> for ArgumentError {
#[inline]
fn from(message: &'static str) -> Self {
let message = Cow::Borrowed(message.as_bytes());
Self { message }
}
}
impl From<Cow<'static, str>> for ArgumentError {
#[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 ArgumentError {
#[inline]
fn from(message: Vec<u8>) -> Self {
let message = Cow::Owned(message);
Self { message }
}
}
impl From<&'static [u8]> for ArgumentError {
#[inline]
fn from(message: &'static [u8]) -> Self {
let message = Cow::Borrowed(message);
Self { message }
}
}
impl From<Cow<'static, [u8]>> for ArgumentError {
#[inline]
fn from(message: Cow<'static, [u8]>) -> Self {
Self { message }
}
}
impl fmt::Display for ArgumentError {
#[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 ArgumentError {}
impl RubyException for ArgumentError {
#[inline]
fn message(&self) -> Cow<'_, [u8]> {
Cow::Borrowed(Self::message(self))
}
#[inline]
fn name(&self) -> Cow<'_, str> {
Cow::Borrowed(Self::name(self))
}
}