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
use std::error;
use std::fmt;
use crate::value::{types, Value};
use crate::Mrb;
mod array;
mod boolean;
mod bytes;
mod fixnum;
mod float;
mod hash;
mod nilable;
mod object;
mod string;
pub use self::array::*;
pub use self::boolean::*;
pub use self::bytes::*;
pub use self::fixnum::*;
pub use self::float::*;
pub use self::hash::*;
pub use self::nilable::*;
pub use self::object::*;
pub use self::string::*;
pub trait FromMrb<T> {
type From;
type To;
fn from_mrb(interp: &Mrb, value: T) -> Self;
}
pub trait TryFromMrb<T>
where
Self: Sized,
{
type From;
type To;
unsafe fn try_from_mrb(interp: &Mrb, value: T) -> Result<Self, Error<Self::From, Self::To>>;
}
impl<From, To> TryFromMrb<From> for To
where
To: FromMrb<From>,
{
type From = <Self as FromMrb<From>>::From;
type To = <Self as FromMrb<From>>::To;
unsafe fn try_from_mrb(interp: &Mrb, value: From) -> Result<Self, Error<Self::From, Self::To>> {
Ok(FromMrb::from_mrb(interp, value))
}
}
#[derive(Clone, Eq, PartialEq)]
pub struct Error<From, To> {
from: From,
to: To,
}
impl<From, To> fmt::Display for Error<From, To>
where
From: fmt::Display,
To: fmt::Display,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "failed to convert from {} to {}", self.from, self.to)
}
}
impl<From, To> fmt::Debug for Error<From, To>
where
From: fmt::Display,
To: fmt::Display,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "mruby conversion error: {}", self)
}
}
impl<To, From> error::Error for Error<To, From>
where
From: fmt::Display,
To: fmt::Display,
{
fn description(&self) -> &str {
"Failed to convert types between ruby and rust"
}
fn cause(&self) -> Option<&dyn error::Error> {
None
}
}
impl FromMrb<Value> for () {
type From = types::Ruby;
type To = types::Rust;
fn from_mrb(_interp: &Mrb, _value: Value) -> Self {}
}
#[cfg(test)]
mod tests {
use crate::convert::Error;
use crate::value::types::*;
#[test]
fn ruby_to_rust_error_display() {
let err = Error {
from: Ruby::Fixnum,
to: Rust::Vec,
};
assert_eq!(
format!("{}", err),
"failed to convert from ruby Fixnum to rust Vec"
);
}
#[test]
fn ruby_to_rust_error_debug() {
let err = Error {
from: Ruby::Fixnum,
to: Rust::Vec,
};
assert_eq!(
format!("{:?}", err),
"mruby conversion error: failed to convert from ruby Fixnum to rust Vec"
);
}
#[test]
fn rust_to_ruby_error_display() {
let err = Error {
from: Rust::Bool,
to: Ruby::String,
};
assert_eq!(
format!("{}", err),
"failed to convert from rust bool to ruby String"
);
}
#[test]
fn rust_to_ruby_error_debug() {
let err = Error {
from: Rust::Bool,
to: Ruby::String,
};
assert_eq!(
format!("{:?}", err),
"mruby conversion error: failed to convert from rust bool to ruby String"
);
}
}