rutie/class/
any_exception.rs1use crate::{
2 types::{Value, ValueType},
3 AnyObject, Class, Exception, NilClass, Object, TryConvert, VerifiedObject,
4};
5use std::{
6 borrow::Borrow,
7 fmt,
8 fmt::{Display, Formatter},
9 ops::Deref,
10};
11
12pub struct AnyException {
13 value: Value,
14}
15
16impl From<Value> for AnyException {
17 fn from(value: Value) -> Self {
18 AnyException { value }
19 }
20}
21
22impl Into<Value> for AnyException {
23 fn into(self) -> Value {
24 self.value
25 }
26}
27
28impl Into<AnyObject> for AnyException {
29 fn into(self) -> AnyObject {
30 AnyObject::from(self.value)
31 }
32}
33
34impl Borrow<Value> for AnyException {
35 fn borrow(&self) -> &Value {
36 &self.value
37 }
38}
39
40impl AsRef<Value> for AnyException {
41 fn as_ref(&self) -> &Value {
42 &self.value
43 }
44}
45
46impl AsRef<AnyException> for AnyException {
47 #[inline]
48 fn as_ref(&self) -> &Self {
49 self
50 }
51}
52
53impl Object for AnyException {
54 #[inline]
55 fn value(&self) -> Value {
56 self.value
57 }
58}
59
60impl Deref for AnyException {
61 type Target = Value;
62
63 fn deref(&self) -> &Value {
64 &self.value
65 }
66}
67
68impl Exception for AnyException {}
69
70impl TryConvert<AnyObject> for AnyException {
71 type Nil = NilClass;
72
73 fn try_convert(obj: AnyObject) -> Result<Self, NilClass> {
74 obj.try_convert_to::<AnyException>()
75 .map_err(|_| NilClass::new())
76 }
77}
78
79impl VerifiedObject for AnyException {
80 fn is_correct_type<T: Object>(object: &T) -> bool {
81 Class::from_existing("Exception").case_equals(object)
82 }
83
84 fn error_message() -> &'static str {
85 "Error converting to AnyException"
86 }
87}
88
89impl Display for AnyException {
90 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
91 write!(f, "{}", self.inspect())
92 }
93}
94
95impl fmt::Debug for AnyException {
96 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
97 write!(f, "{}", self.inspect())
98 }
99}
100
101impl PartialEq for AnyException {
102 fn eq(&self, other: &Self) -> bool {
103 self.equals(other)
104 }
105}