| 341 | |
| 342 | #[sqlfunc(sqlname = "convert_from", propagates_nulls = true)] |
| 343 | fn convert_from<'a>(a: &'a [u8], b: &str) -> Result<&'a str, EvalError> { |
| 344 | // Convert PostgreSQL-style encoding names[1] to WHATWG-style encoding names[2], |
| 345 | // which the encoding library uses[3]. |
| 346 | // [1]: https://www.postgresql.org/docs/9.5/multibyte.html |
| 347 | // [2]: https://encoding.spec.whatwg.org/ |
| 348 | // [3]: https://github.com/lifthrasiir/rust-encoding/blob/4e79c35ab6a351881a86dbff565c4db0085cc113/src/label.rs |
| 349 | let encoding_name = b.to_lowercase().replace('_', "-").into_boxed_str(); |
| 350 | |
| 351 | // Supporting other encodings is tracked by database-issues#797. |
| 352 | if encoding_from_whatwg_label(&encoding_name).map(|e| e.name()) != Some("utf-8") { |
| 353 | return Err(EvalError::InvalidEncodingName(encoding_name)); |
| 354 | } |
| 355 | |
| 356 | match str::from_utf8(a) { |
| 357 | Ok(from) => Ok(from), |
| 358 | Err(e) => Err(EvalError::InvalidByteSequence { |
| 359 | byte_sequence: e.to_string().into(), |
| 360 | encoding_name, |
| 361 | }), |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | #[sqlfunc] |
| 366 | fn encode(bytes: &[u8], format: &str) -> Result<String, EvalError> { |