artichoke_backend/convert/
boolean.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
181
182
183
184
use crate::convert::UnboxRubyError;
use crate::core::{Convert, TryConvert, Value as _};
use crate::error::Error;
use crate::sys;
use crate::types::{Ruby, Rust};
use crate::value::Value;
use crate::Artichoke;

impl Convert<bool, Value> for Artichoke {
    fn convert(&self, value: bool) -> Value {
        // SAFETY: Boolean Ruby Values do not need to be protected because they
        // are immediates and do not live on the mruby heap.
        if value {
            Value::from(unsafe { sys::mrb_sys_true_value() })
        } else {
            Value::from(unsafe { sys::mrb_sys_false_value() })
        }
    }
}

impl Convert<Option<bool>, Value> for Artichoke {
    fn convert(&self, value: Option<bool>) -> Value {
        if let Some(value) = value {
            self.convert(value)
        } else {
            Value::nil()
        }
    }
}

impl TryConvert<Value, bool> for Artichoke {
    type Error = Error;

    fn try_convert(&self, value: Value) -> Result<bool, Self::Error> {
        if let Ruby::Bool = value.ruby_type() {
            let inner = value.inner();
            if unsafe { sys::mrb_sys_value_is_true(inner) } {
                Ok(true)
            } else if unsafe { sys::mrb_sys_value_is_false(inner) } {
                Ok(false)
            } else {
                // This branch is unreachable because `Ruby::Bool` typed values
                // are guaranteed to be either true or false.
                Err(UnboxRubyError::new(&value, Rust::Bool).into())
            }
        } else {
            Err(UnboxRubyError::new(&value, Rust::Bool).into())
        }
    }
}

impl TryConvert<Value, Option<bool>> for Artichoke {
    type Error = Error;

    fn try_convert(&self, value: Value) -> Result<Option<bool>, Self::Error> {
        if value.is_nil() {
            Ok(None)
        } else {
            Ok(Some(self.try_convert(value)?))
        }
    }
}

#[cfg(test)]
mod tests {
    use quickcheck::quickcheck;

    use crate::test::prelude::*;

    #[test]
    fn fail_convert() {
        let mut interp = interpreter();
        // get a Ruby value that can't be converted to a primitive type.
        let value = interp.eval(b"Object.new").unwrap();
        let result = value.try_convert_into::<bool>(&interp);
        assert!(result.is_err());
    }

    quickcheck! {
        fn convert_to_bool(b: bool) -> bool {
            let interp = interpreter();
            let value = interp.convert(b);
            value.ruby_type() == Ruby::Bool
        }

        fn convert_to_nilable_bool(b: Option<bool>) -> bool {
            let interp = interpreter();
            let value = interp.convert(b);
            if b.is_some() {
                value.ruby_type() == Ruby::Bool
            } else {
                value.ruby_type() == Ruby::Nil
            }
        }

        fn bool_with_value(b: bool) -> bool {
            let interp = interpreter();
            let value = interp.convert(b);
            let value = value.inner();
            if b {
                if unsafe { sys::mrb_sys_value_is_false(value) } {
                    return false;
                }
                if !unsafe { sys::mrb_sys_value_is_true(value) } {
                    return false;
                }
            } else {
                if !unsafe { sys::mrb_sys_value_is_false(value) } {
                    return false;
                }
                if unsafe { sys::mrb_sys_value_is_true(value) } {
                    return false;
                }
            }
            if unsafe { sys::mrb_sys_value_is_nil(value) } {
                return false;
            }
            true
        }

        fn nilable_bool_with_value(b: Option<bool>) -> bool {
            let interp = interpreter();
            let value = interp.convert(b);
            let value = value.inner();
            match b {
                Some(true) => {
                    if unsafe { sys::mrb_sys_value_is_false(value) } {
                        return false;
                    }
                    if !unsafe { sys::mrb_sys_value_is_true(value) } {
                        return false;
                    }
                    if unsafe { sys::mrb_sys_value_is_nil(value) } {
                        return false;
                    }
                }
                Some(false) => {
                    if !unsafe { sys::mrb_sys_value_is_false(value) } {
                        return false;
                    }
                    if unsafe { sys::mrb_sys_value_is_true(value) } {
                        return false;
                    }
                    if unsafe { sys::mrb_sys_value_is_nil(value) } {
                        return false;
                    }
                }
                None => {
                    if unsafe { sys::mrb_sys_value_is_false(value) } {
                        return false;
                    }
                    if unsafe { sys::mrb_sys_value_is_true(value) } {
                        return false;
                    }
                    if !unsafe { sys::mrb_sys_value_is_nil(value) } {
                        return false;
                    }
                }
            }
            true
        }

        fn roundtrip(b: bool) -> bool {
            let interp = interpreter();
            let value = interp.convert(b);
            let value = value.try_convert_into::<bool>(&interp).unwrap();
            value == b
        }

        fn nilable_roundtrip(b: Option<bool>) -> bool {
            let interp = interpreter();
            let value = interp.convert(b);
            let value = value.try_convert_into::<Option<bool>>(&interp).unwrap();
            value == b
        }

        fn roundtrip_err(i: i64) -> bool {
            let interp = interpreter();
            let value = interp.convert(i);
            let value = value.try_convert_into::<bool>(&interp);
            value.is_err()
        }
    }
}