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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
use std::{
    convert::{TryFrom, TryInto},
    error::Error,
    ffi::{CStr, CString, FromBytesWithNulError},
    fmt,
    os::raw::c_int,
};
use crate::{
    object::NonNullObject,
    prelude::*,
    ruby,
};

/// An encoding for `String`.
#[derive(Clone, Copy, Debug)]
#[repr(transparent)]
pub struct Encoding(NonNullObject);

impl AsRef<AnyObject> for Encoding {
    #[inline]
    fn as_ref(&self) -> &AnyObject { self.0.as_ref() }
}

impl From<Encoding> for AnyObject {
    #[inline]
    fn from(object: Encoding) -> AnyObject { object.0.into() }
}

impl PartialEq<AnyObject> for Encoding {
    #[inline]
    fn eq(&self, obj: &AnyObject) -> bool {
        self.as_any_object() == obj
    }
}

unsafe impl Object for Encoding {
    #[inline]
    fn cast<A: Object>(obj: A) -> Option<Self> {
        if obj.class().inherits(Class::of::<Self>()) {
            unsafe { Some(Self::cast_unchecked(obj)) }
        } else {
            None
        }
    }
}

impl fmt::Display for Encoding {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.as_any_object().fmt(f)
    }
}

impl PartialEq for Encoding {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self._enc() == other._enc()
    }
}

impl Eq for Encoding {}

impl TryFrom<&CStr> for Encoding {
    type Error = EncodingLookupError;

    #[inline]
    fn try_from(s: &CStr) -> Result<Self, Self::Error> {
        let index = unsafe { ruby::rb_enc_find_index(s.as_ptr()) };
        if index < 0 {
            Err(EncodingLookupError::UnknownName)
        } else {
            Ok(Encoding::_from_index(index))
        }
    }
}

impl TryFrom<&CString> for Encoding {
    type Error = EncodingLookupError;

    #[inline]
    fn try_from(s: &CString) -> Result<Self, Self::Error> {
        s.as_c_str().try_into()
    }
}

impl TryFrom<&[u8]> for Encoding {
    type Error = EncodingLookupError;

    #[inline]
    fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
        CStr::from_bytes_with_nul(bytes)?.try_into()
    }
}

impl TryFrom<&Vec<u8>> for Encoding {
    type Error = EncodingLookupError;

    #[inline]
    fn try_from(bytes: &Vec<u8>) -> Result<Self, Self::Error> {
        bytes.as_slice().try_into()
    }
}

impl TryFrom<&str> for Encoding {
    type Error = EncodingLookupError;

    #[inline]
    fn try_from(s: &str) -> Result<Self, Self::Error> {
        s.as_bytes().try_into()
    }
}

impl TryFrom<&std::string::String> for Encoding {
    type Error = EncodingLookupError;

    #[inline]
    fn try_from(s: &std::string::String) -> Result<Self, Self::Error> {
        s.as_str().try_into()
    }
}

impl Encoding {
    #[inline]
    pub(crate) fn _from_enc(enc: *mut ruby::rb_encoding) -> Self {
        unsafe { Self::from_raw(ruby::rb_enc_from_encoding(enc)) }
    }

    #[inline]
    pub(crate) fn _from_index(i: c_int) -> Self {
        unsafe { Self::_from_enc(ruby::rb_enc_from_index(i)) }
    }

    #[inline]
    pub(crate) fn _rdata(self) -> *mut ruby::RData {
        self.as_any_object()._ptr() as _
    }

    #[inline]
    pub(crate) fn _enc(self) -> *mut ruby::rb_encoding {
        unsafe {
            let enc = (*self._rdata()).data as *mut ruby::rb_encoding;
            debug_assert_eq!(enc, ruby::rb_to_encoding(self.raw()));
            enc
        }
    }

    #[inline]
    pub(crate) fn _index(self) -> c_int {
        unsafe { (*self._enc()).index() }
    }

    /// Returns the `ASCII-8BIT` encoding.
    ///
    /// # Examples
    ///
    /// This is essentially an "anything goes" encoding:
    ///
    /// ```
    /// use rosy::string::{String, Encoding};
    ///
    /// # rosy::vm::init().unwrap();
    /// let bytes: &[u8] = &[b'a', b'z', 0, 255];
    /// let string = String::from(bytes);
    ///
    /// assert_eq!(string.encoding(), Encoding::ascii_8bit());
    /// ```
    #[inline]
    pub fn ascii_8bit() -> Encoding {
        unsafe { Encoding::_from_enc(ruby::rb_ascii8bit_encoding()) }
    }

    /// Returns the `UTF-8` encoding.
    ///
    /// # Examples
    ///
    /// ```
    /// # rosy::vm::init().unwrap();
    /// use rosy::string::Encoding;
    ///
    /// let utf8 = Encoding::find("UTF-8\0").unwrap();
    /// assert_eq!(utf8, Encoding::utf8());
    /// ```
    #[inline]
    pub fn utf8() -> Encoding {
        unsafe { Encoding::_from_enc(ruby::rb_utf8_encoding()) }
    }

    /// Returns the `US-ASCII` encoding.
    ///
    /// # Examples
    ///
    /// ```
    /// # rosy::vm::init().unwrap();
    /// use rosy::string::Encoding;
    ///
    /// let ascii = Encoding::find("US-ASCII\0").unwrap();
    /// assert_eq!(ascii, Encoding::us_ascii());
    /// ```
    #[inline]
    pub fn us_ascii() -> Encoding {
        unsafe { Encoding::_from_enc(ruby::rb_usascii_encoding()) }
    }

    /// Attempts to find `encoding`, returning an error if either:
    /// - `encoding` cannot be passed in as a nul-terminated C string.
    /// - The requested encoding was not found.
    ///
    /// # Examples
    ///
    /// Looking up an encoding is straightforward since Rust allows for
    /// embedding nul bytes in its UTF-8 strings:
    ///
    /// ```
    /// # rosy::vm::init().unwrap();
    /// use rosy::string::Encoding;
    ///
    /// let utf8 = Encoding::find("UTF-8\0").unwrap();
    /// let ascii = Encoding::find("US-ASCII\0").unwrap();
    ///
    /// assert_ne!(utf8, ascii);
    /// ```
    #[inline]
    pub fn find<E>(encoding: E) -> Result<Self, EncodingLookupError>
        where E: TryInto<Self, Error=EncodingLookupError>
    {
        encoding.try_into()
    }

    /// Returns the encoding's name.
    ///
    /// # Examples
    ///
    /// ```
    /// # rosy::vm::init().unwrap();
    /// use rosy::string::Encoding;
    ///
    /// assert_eq!(Encoding::utf8().name().to_bytes(), b"UTF-8");
    /// ```
    #[inline]
    pub fn name(&self) -> &CStr {
        unsafe { CStr::from_ptr((*self._enc()).name) }
    }

    /// Returns whether `self` is `ASCII-8BIT`.
    #[inline]
    pub fn is_ascii_8bit(self) -> bool {
        self._index() == ruby::rb_encoding::ascii_8bit_index()
    }

    /// Returns whether `self` is `UTF-8`.
    #[inline]
    pub fn is_utf8(self) -> bool {
        self._index() == ruby::rb_encoding::utf8_index()
    }

    /// Returns whether `self` is `US-ASCII`.
    #[inline]
    pub fn is_us_ascii(self) -> bool {
        self._index() == ruby::rb_encoding::us_ascii_index()
    }

    /// Returns whether `self` is the locale encoding.
    #[inline]
    pub fn is_locale(self) -> bool {
        unsafe { self._index() == ruby::rb_locale_encindex() }
    }

    /// Returns whether `self` is the filesystem encoding.
    #[inline]
    pub fn is_filesystem(self) -> bool {
        unsafe { self._index() == ruby::rb_filesystem_encindex() }
    }

    /// Returns whether `self` is the default external encoding.
    #[inline]
    pub fn is_default_external(self) -> bool {
        unsafe { self._enc() == ruby::rb_default_external_encoding() }
    }

    /// Returns whether `self` is the default internal encoding.
    #[inline]
    pub fn is_default_internal(self) -> bool {
        unsafe { self._enc() == ruby::rb_default_internal_encoding() }
    }
}

/// The error returned when [`Encoding::find`](struct.Encoding.html#method.find)
/// fails.
#[derive(Debug)]
pub enum EncodingLookupError {
    /// The encoding name could not be found.
    UnknownName,
    /// The encoding name string was not C-compatible.
    InvalidCStr(FromBytesWithNulError),
}

impl Error for EncodingLookupError {
    #[inline]
    fn description(&self) -> &str {
        use EncodingLookupError::*;
        match self {
            UnknownName => "Unknown encoding name",
            InvalidCStr(error) => error.description(),
        }
    }
}

impl fmt::Display for EncodingLookupError {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use EncodingLookupError::*;
        match self {
            UnknownName => self.description().fmt(f),
            InvalidCStr(error) => error.fmt(f),
        }
    }
}

impl From<FromBytesWithNulError> for EncodingLookupError {
    #[inline]
    fn from(error: FromBytesWithNulError) -> Self {
        EncodingLookupError::InvalidCStr(error)
    }
}