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
//! Converters for nilable primitive Ruby types.
//!
//! Excludes collection types Array and Hash.

use crate::core::{Convert, TryConvert, TryConvertMut, Value as _};
use crate::error::Error;
use crate::value::Value;
use crate::Artichoke;

impl Convert<Option<Value>, Value> for Artichoke {
    fn convert(&self, value: Option<Value>) -> Value {
        Value::from(value)
    }
}

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

impl TryConvertMut<Option<Vec<u8>>, Value> for Artichoke {
    type Error = Error;

    fn try_convert_mut(&mut self, value: Option<Vec<u8>>) -> Result<Value, Self::Error> {
        self.try_convert_mut(value.as_deref())
    }
}

impl TryConvertMut<Option<&[u8]>, Value> for Artichoke {
    type Error = Error;

    fn try_convert_mut(&mut self, value: Option<&[u8]>) -> Result<Value, Self::Error> {
        if let Some(value) = value {
            self.try_convert_mut(value)
        } else {
            Ok(Value::nil())
        }
    }
}

impl TryConvertMut<Option<String>, Value> for Artichoke {
    type Error = Error;

    fn try_convert_mut(&mut self, value: Option<String>) -> Result<Value, Self::Error> {
        self.try_convert_mut(value.as_deref())
    }
}

impl TryConvertMut<Option<&str>, Value> for Artichoke {
    type Error = Error;

    fn try_convert_mut(&mut self, value: Option<&str>) -> Result<Value, Self::Error> {
        if let Some(value) = value {
            self.try_convert_mut(value)
        } else {
            Ok(Value::nil())
        }
    }
}

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

impl TryConvertMut<Value, Option<Vec<u8>>> for Artichoke {
    type Error = Error;

    fn try_convert_mut(&mut self, value: Value) -> Result<Option<Vec<u8>>, Self::Error> {
        if value.is_nil() {
            Ok(None)
        } else {
            self.try_convert_mut(value).map(Some)
        }
    }
}

impl<'a> TryConvertMut<Value, Option<&'a [u8]>> for Artichoke {
    type Error = Error;

    fn try_convert_mut(&mut self, value: Value) -> Result<Option<&'a [u8]>, Self::Error> {
        if value.is_nil() {
            Ok(None)
        } else {
            self.try_convert_mut(value).map(Some)
        }
    }
}

impl TryConvertMut<Value, Option<String>> for Artichoke {
    type Error = Error;

    fn try_convert_mut(&mut self, value: Value) -> Result<Option<String>, Self::Error> {
        if value.is_nil() {
            Ok(None)
        } else {
            self.try_convert_mut(value).map(Some)
        }
    }
}

impl<'a> TryConvertMut<Value, Option<&'a str>> for Artichoke {
    type Error = Error;

    fn try_convert_mut(&mut self, value: Value) -> Result<Option<&'a str>, Self::Error> {
        if value.is_nil() {
            Ok(None)
        } else {
            self.try_convert_mut(value).map(Some)
        }
    }
}

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

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