1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
//! Parse encoding parameter to `Regexp#initialize` and `Regexp::compile`.

use core::convert::TryFrom;

use super::{Encoding, InvalidEncodingError};
use crate::extn::prelude::*;

impl TryConvertMut<Value, Encoding> for Artichoke {
    type Error = InvalidEncodingError;

    fn try_convert_mut(&mut self, value: Value) -> Result<Encoding, Self::Error> {
        if let Ok(encoding) = value.try_into::<i64>(self) {
            Encoding::try_from(encoding)
        } else if let Ok(encoding) = value.try_into_mut::<&[u8]>(self) {
            Encoding::try_from(encoding)
        } else {
            Ok(Encoding::new())
        }
    }
}