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
use crate::convert::{Convert, TryConvert};
use crate::sys;
use crate::types::{Ruby, Rust};
use crate::value::Value;
use crate::{Artichoke, ArtichokeError};

impl Convert<bool, Value> for Artichoke {
    fn convert(&self, value: bool) -> Value {
        if value {
            Value::new(self, unsafe { sys::mrb_sys_true_value() })
        } else {
            Value::new(self, unsafe { sys::mrb_sys_false_value() })
        }
    }
}

impl TryConvert<Value, bool> for Artichoke {
    fn try_convert(&self, value: Value) -> Result<bool, ArtichokeError> {
        match value.ruby_type() {
            Ruby::Bool => {
                let value = value.inner();
                if unsafe { sys::mrb_sys_value_is_true(value) } {
                    Ok(true)
                } else if unsafe { sys::mrb_sys_value_is_false(value) } {
                    Ok(false)
                } else {
                    // This should be unreachable
                    Err(ArtichokeError::ConvertToRust {
                        from: Ruby::Bool,
                        to: Rust::Bool,
                    })
                }
            }
            type_tag => Err(ArtichokeError::ConvertToRust {
                from: type_tag,
                to: Rust::Bool,
            }),
        }
    }
}

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

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

    #[test]
    fn fail_convert() {
        let interp = crate::interpreter().expect("init");
        // get a mrb_value that can't be converted to a primitive type.
        let value = interp.eval(b"Object.new").expect("eval");
        let expected = Err(ArtichokeError::ConvertToRust {
            from: Ruby::Object,
            to: Rust::Bool,
        });
        let result = value.try_into::<bool>();
        assert_eq!(result, expected);
    }

    #[quickcheck]
    fn convert_to_bool(b: bool) -> bool {
        let interp = crate::interpreter().expect("init");
        let value = interp.convert(b);
        value.ruby_type() == Ruby::Bool
    }

    #[quickcheck]
    fn bool_with_value(b: bool) -> bool {
        let interp = crate::interpreter().expect("init");
        let value = interp.convert(b);
        let value = value.inner();
        if b {
            !unsafe { sys::mrb_sys_value_is_false(value) }
                && unsafe { sys::mrb_sys_value_is_true(value) }
                && !unsafe { sys::mrb_sys_value_is_nil(value) }
        } else {
            !unsafe { sys::mrb_sys_value_is_true(value) }
                && unsafe { sys::mrb_sys_value_is_false(value) }
                && !unsafe { sys::mrb_sys_value_is_nil(value) }
        }
    }

    #[quickcheck]
    fn roundtrip(b: bool) -> bool {
        let interp = crate::interpreter().expect("init");
        let value = interp.convert(b);
        let value = value.try_into::<bool>().expect("convert");
        value == b
    }

    #[quickcheck]
    fn roundtrip_err(i: i64) -> bool {
        let interp = crate::interpreter().expect("init");
        let value = interp.convert(i);
        let value = value.try_into::<bool>();
        let expected = Err(ArtichokeError::ConvertToRust {
            from: Ruby::Fixnum,
            to: Rust::Bool,
        });
        value == expected
    }
}