rutie 0.12.0

The tie between Ruby and Rust.
Documentation
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
use crate::{
    binding::encoding,
    types::{EncodingIndex, Value, ValueType},
    AnyException, AnyObject, Class, Exception, NilClass, Object, RString, VerifiedObject,
};

#[derive(Debug)]
#[repr(C)]
pub struct Encoding {
    value: Value,
}

impl Encoding {
    /// Returns the `ASCII-8BIT` (binary) encoding (`rb_ascii8bit_encoding`).
    ///
    /// # Examples
    ///
    /// ```
    /// use rutie::{Encoding, VM};
    /// # VM::init();
    ///
    /// assert_eq!(Encoding::ascii_8bit().name(), "ASCII-8BIT");
    /// ```
    pub fn ascii_8bit() -> Self {
        Encoding::from(encoding::ascii_8bit_encoding())
    }

    /// Returns the locale's encoding (`rb_locale_encoding`).
    ///
    /// # Examples
    ///
    /// ```
    /// use rutie::{Encoding, VM};
    /// # VM::init();
    ///
    /// assert!(!Encoding::locale().name().is_empty());
    /// ```
    pub fn locale() -> Self {
        Encoding::from(encoding::locale_encoding())
    }

    /// Returns the filesystem encoding (`rb_filesystem_encoding`).
    ///
    /// # Examples
    ///
    /// ```
    /// use rutie::{Encoding, VM};
    /// # VM::init();
    ///
    /// assert!(!Encoding::filesystem().name().is_empty());
    /// ```
    pub fn filesystem() -> Self {
        Encoding::from(encoding::filesystem_encoding())
    }

    /// Returns the encoding of `object` (a `String`, `Symbol`, `Regexp`,
    /// ...), or `None` for objects without one (`rb_enc_get`).
    ///
    /// # Examples
    ///
    /// ```
    /// use rutie::{Encoding, Fixnum, RString, VM};
    /// # VM::init();
    ///
    /// assert_eq!(Encoding::of(&RString::new_utf8("x")).unwrap().name(), "UTF-8");
    /// assert!(Encoding::of(&Fixnum::new(1)).is_none());
    /// ```
    pub fn of<T: Object>(object: &T) -> Option<Self> {
        let found = encoding::encoding_of(object.value());

        if found.is_nil() {
            None
        } else {
            Some(Encoding::from(found))
        }
    }

    /// Returns Ruby's internal index for the encoding (`rb_to_encoding_index`).
    ///
    /// # Examples
    ///
    /// ```
    /// use rutie::{Encoding, VM};
    /// # VM::init();
    ///
    /// assert_eq!(Encoding::utf8().index(), Encoding::utf8().index());
    /// assert_ne!(Encoding::utf8().index(), Encoding::us_ascii().index());
    /// ```
    pub fn index(&self) -> EncodingIndex {
        encoding::encoding_index(self.value())
    }

    /// Returns the character with code point `code` in this encoding as a
    /// string (Ruby's `Integer#chr(encoding)`, `rb_enc_uint_chr`), or the
    /// `RangeError` if the encoding cannot represent it.
    ///
    /// # Examples
    ///
    /// ```
    /// use rutie::{Encoding, VM};
    /// # VM::init();
    ///
    /// assert_eq!(Encoding::utf8().chr(0x1F980).unwrap().to_str(), "🦀");
    /// assert!(Encoding::us_ascii().chr(0xE9).is_err());
    /// ```
    pub fn chr(&self, code: u32) -> Result<RString, AnyException> {
        let encoding = self.value();

        crate::binding::vm::protect_value(|| encoding::chr(code, encoding))
            .map(RString::from)
            .map_err(AnyException::from)
    }

    /// Returns `true` if ASCII text is valid in this encoding (Ruby's
    /// `ascii_compatible?`).
    ///
    /// # Examples
    ///
    /// ```
    /// use rutie::{Encoding, VM};
    /// # VM::init();
    /// VM::init_loadpath(); // Needed for the encoding database
    /// VM::require("enc/encdb");
    ///
    /// assert!(Encoding::utf8().is_ascii_compatible());
    /// assert!(!Encoding::find("UTF-16LE").unwrap().is_ascii_compatible());
    /// ```
    pub fn is_ascii_compatible(&self) -> bool {
        unsafe { self.send("ascii_compatible?", &[]) }
            .value()
            .is_true()
    }

    /// Returns `true` for dummy encodings, which Ruby can name but not
    /// process (Ruby's `dummy?`).
    ///
    /// # Examples
    ///
    /// ```
    /// use rutie::{Encoding, VM};
    /// # VM::init();
    /// VM::init_loadpath(); // Needed for the encoding database
    /// VM::require("enc/encdb");
    ///
    /// assert!(!Encoding::utf8().is_dummy());
    /// assert!(Encoding::find("UTF-16").unwrap().is_dummy());
    /// ```
    pub fn is_dummy(&self) -> bool {
        unsafe { self.send("dummy?", &[]) }.value().is_true()
    }

    /// Creates a UTF-8 instance of `Encoding`.
    ///
    /// # Examples
    ///
    /// ```
    /// use rutie::{Encoding, VM};
    /// # VM::init();
    ///
    /// assert_eq!(Encoding::utf8().name(), "UTF-8");
    /// ```
    ///
    /// Ruby:
    ///
    /// ```ruby
    /// Encoding::UTF_8
    /// ```
    pub fn utf8() -> Self {
        Self::from(encoding::utf8_encoding())
    }

    /// Creates a US-ASCII instance of `Encoding`.
    ///
    /// # Examples
    ///
    /// ```
    /// use rutie::{Encoding, VM};
    /// # VM::init();
    ///
    /// assert_eq!(Encoding::us_ascii().name(), "US-ASCII");
    /// ```
    ///
    /// Ruby:
    ///
    /// ```ruby
    /// Encoding::US_ASCII
    /// ```
    pub fn us_ascii() -> Self {
        Self::from(encoding::usascii_encoding())
    }

    /// Creates a new instance of `Encoding` from the default external encoding.
    ///
    /// # Examples
    ///
    /// ```
    /// use rutie::{Encoding, Object, RString, VM};
    /// # VM::init();
    ///
    /// let from_ruby = VM::eval("Encoding.default_external.name").unwrap();
    ///
    /// assert_eq!(
    ///     Encoding::default_external().name(),
    ///     from_ruby.try_convert_to::<RString>().unwrap().to_str()
    /// );
    /// ```
    ///
    /// Ruby:
    ///
    /// ```ruby
    /// Encoding.default_external
    /// ```
    pub fn default_external() -> Self {
        Self::from(encoding::default_external())
    }

    /// Creates an instance of `Ok(Encoding)` from the default internal encoding
    /// if there is one, otherwise it returns `Err(NilClass)`.
    ///
    /// # Examples
    ///
    /// ```
    /// use rutie::{Encoding, VM};
    /// # VM::init();
    ///
    /// // Ruby has no default internal encoding unless one is set.
    /// assert!(Encoding::default_internal().is_err());
    ///
    /// VM::eval("Encoding.default_internal = 'UTF-8'").unwrap();
    /// assert_eq!(Encoding::default_internal().unwrap().name(), "UTF-8");
    /// ```
    ///
    /// Ruby:
    ///
    /// ```ruby
    /// Encoding.default_internal
    /// ```
    pub fn default_internal() -> Result<Self, NilClass> {
        let result = encoding::default_internal();

        if result.is_nil() {
            Err(NilClass::from(result))
        } else {
            Ok(Self::from(result))
        }
    }

    /// Returns encoding name.
    ///
    /// # Examples
    ///
    /// ```
    /// use rutie::{RString, Encoding, VM};
    /// # VM::init();
    ///
    /// let enc = Encoding::utf8();
    ///
    /// assert_eq!(enc.name(), "UTF-8")
    /// ```
    ///
    /// Ruby:
    ///
    /// ```ruby
    /// enc = Encoding::UTF_8
    ///
    /// enc.name == "UTF-8"
    /// ```
    pub fn name(&self) -> String {
        let name = unsafe { self.send("name", &[]) };

        RString::from(name.value()).to_string()
    }

    /// Find an `Ok(Encoding)` for given string name or return an `Err(AnyException)`.
    ///
    /// # Examples
    ///
    /// ```
    /// use rutie::{VM, Encoding};
    /// # VM::init();
    ///
    /// let encoding = Encoding::find("UTF-8");
    ///
    /// match encoding {
    ///     Ok(enc) => assert_eq!(enc.name(), "UTF-8"),
    ///     Err(_) => unreachable!()
    /// }
    /// ```
    ///
    /// Ruby:
    ///
    /// ```ruby
    /// encoding = Encoding.find("UTF-8")
    ///
    /// encoding.name == "UTF-8"
    /// ```
    ///
    /// The following is an example where a Ruby exception object of `ArgumentError` is returned.
    ///
    /// ```
    /// use rutie::{VM, Encoding, Exception};
    /// # VM::init();
    ///
    /// let encoding = Encoding::find("UTF8");
    ///
    /// match encoding {
    ///     Ok(_) => unreachable!(),
    ///     Err(e) => assert_eq!(e.message(), "unknown encoding name - UTF8")
    /// }
    /// ```
    pub fn find(s: &str) -> Result<Encoding, AnyException> {
        let idx = encoding::find_encoding_index(s);

        if idx < 0 {
            Err(AnyException::new(
                "ArgumentError",
                Some(&format!("unknown encoding name - {}", s)),
            ))
        } else {
            Ok(Encoding::from(encoding::from_encoding_index(idx)))
        }
    }

    /// Returns an instance of `Ok(Encoding)` if the objects are
    /// compatible encodings, otherwise it returns `Err(NilClass)`.
    ///
    /// # Examples
    ///
    /// ```
    /// use rutie::{Encoding, VM, RString, NilClass};
    /// # VM::init();
    ///
    /// let utf8 = RString::new_utf8("asdf");
    /// let us_ascii= RString::new_usascii_unchecked("qwerty");
    ///
    /// let result = Encoding::is_compatible(&utf8, &us_ascii);
    ///
    /// assert!(result.is_ok());
    ///
    /// let result = Encoding::is_compatible(&utf8, &NilClass::new());
    ///
    /// assert!(result.is_err());
    /// ```
    pub fn is_compatible(obj1: &impl Object, obj2: &impl Object) -> Result<Self, NilClass> {
        let result = encoding::compatible_encoding(obj1.value(), obj2.value());

        if result.is_nil() {
            Err(NilClass::from(result))
        } else {
            Ok(Self::from(result))
        }
    }
}

impl Default for Encoding {
    fn default() -> Self {
        Encoding::default_external()
    }
}

impl From<Value> for Encoding {
    fn from(value: Value) -> Self {
        Encoding { value }
    }
}

impl Into<Value> for Encoding {
    fn into(self) -> Value {
        self.value
    }
}

impl Into<AnyObject> for Encoding {
    fn into(self) -> AnyObject {
        AnyObject::from(self.value)
    }
}

impl Object for Encoding {
    #[inline]
    fn value(&self) -> Value {
        self.value
    }
}

impl VerifiedObject for Encoding {
    fn is_correct_type<T: Object>(object: &T) -> bool {
        // `Encoding` instances are `T_DATA`, not classes.
        Class::encoding().case_equals(object)
    }

    fn error_message() -> &'static str {
        "Error converting to Encoding"
    }
}

impl PartialEq for Encoding {
    fn eq(&self, other: &Self) -> bool {
        self.equals(other)
    }
}

#[cfg(test)]
mod tests {
    use crate::{Encoding, EncodingSupport, Fixnum, Object, RString, Symbol, VM};

    #[test]
    fn test_encoding_lookup_and_chr() {
        crate::on_ruby_thread(|| {
            // An embedded VM only knows the built-in encodings until the
            // encoding database is loaded.
            VM::init_loadpath();
            VM::require("enc/encdb");

            assert_eq!(Encoding::ascii_8bit().name(), "ASCII-8BIT");
            assert_eq!(
                Encoding::of(&Symbol::new("sym")).unwrap().name(),
                "US-ASCII"
            );
            assert!(Encoding::of(&Fixnum::new(1)).is_none());
            assert_eq!(
                Encoding::of(&RString::new_utf8("é")).unwrap().index(),
                Encoding::utf8().index()
            );

            let e = Encoding::utf8().chr(0xE9).unwrap();
            assert_eq!(e.to_str(), "é");
            assert_eq!(e.encoding().name(), "UTF-8");
            assert!(Encoding::utf8().chr(0xD800).is_err());

            let mut binary = RString::from_bytes(b"\xFF", &Encoding::ascii_8bit());
            assert!(binary
                .concat_bytes("é".as_bytes(), &Encoding::utf8())
                .is_err());

            assert!(Encoding::find("Shift_JIS").unwrap().is_ascii_compatible());
        });
    }

    #[test]
    fn test_encoding_try_convert() {
        crate::on_ruby_thread(|| {
            let encoding = VM::eval("''.encoding").unwrap();

            assert!(encoding.try_convert_to::<Encoding>().is_ok());
            assert!(RString::new_utf8("x").try_convert_to::<Encoding>().is_err());
        });
    }

    #[test]
    fn test_encoding_defaults_and_compatibility() {
        crate::on_ruby_thread(|| {
            assert_eq!(Encoding::us_ascii().name(), "US-ASCII");
            assert!(!Encoding::us_ascii().is_dummy());

            // The process-wide defaults match what Ruby reports.
            let external = VM::eval("Encoding.default_external.name").unwrap();
            assert_eq!(
                Encoding::default_external().name(),
                external.try_convert_to::<RString>().unwrap().to_str()
            );
            let internal = VM::eval("Encoding.default_internal").unwrap();
            assert_eq!(Encoding::default_internal().is_err(), internal.is_nil());

            // Without the encoding database loaded, `Encoding.find('locale')`
            // is unavailable, so compare with what Ruby's own objects report.
            let locale = Encoding::locale();
            assert!(!locale.name().is_empty());
            assert!(unsafe { locale.send("ascii_compatible?", &[]) }
                .value()
                .is_true());
            assert!(!Encoding::filesystem().name().is_empty());

            let utf8 = RString::new_utf8("é");
            let ascii = RString::new_usascii_unchecked("a");
            assert_eq!(
                Encoding::is_compatible(&utf8, &ascii).unwrap().name(),
                "UTF-8"
            );

            let binary = VM::eval(r#""\xFF".b"#).unwrap();
            assert!(Encoding::is_compatible(&utf8, &binary).is_err());
        });
    }
}