diff --git a/crates/nu-command/src/strings/encode_decode/encode.rs b/crates/nu-command/src/strings/encode_decode/encode.rs index b017da4a8a..98fcc34179 100644 --- a/crates/nu-command/src/strings/encode_decode/encode.rs +++ b/crates/nu-command/src/strings/encode_decode/encode.rs @@ -37,6 +37,7 @@ impl Command for Encode { big5, euc-jp, euc-kr, gbk, iso-8859-1, cp1252, latin5 Note that since the Encoding Standard doesn't specify encoders for utf-16le and utf-16be, these are not yet supported. +More information can be found here: https://docs.rs/encoding_rs/latest/encoding_rs/#utf-16le-utf-16be-and-unicode-encoding-schemes For a more complete list of encodings, please refer to the encoding_rs documentation link at https://docs.rs/encoding_rs/latest/encoding_rs/#statics"# diff --git a/crates/nu-command/src/strings/encode_decode/encoding.rs b/crates/nu-command/src/strings/encode_decode/encoding.rs index a063820d4c..91c7e72b17 100644 --- a/crates/nu-command/src/strings/encode_decode/encoding.rs +++ b/crates/nu-command/src/strings/encode_decode/encoding.rs @@ -50,6 +50,19 @@ pub fn encode( } else { parse_encoding(encoding_name.span, &encoding_name.item) }?; + + // Since the Encoding Standard doesn't specify encoders for "UTF-16BE" and "UTF-16LE" + // Check if the encoding is one of them and return an error + if ["UTF-16BE", "UTF-16LE"].contains(&encoding.name()) { + return Err(ShellError::GenericError { + error: format!(r#"{} encoding is not supported"#, &encoding_name.item), + msg: "invalid encoding".into(), + span: Some(encoding_name.span), + help: Some("refer to https://docs.rs/encoding_rs/latest/encoding_rs/index.html#statics for a valid list of encodings".into()), + inner: vec![], + }); + } + let (result, _actual_encoding, replacements) = encoding.encode(s); // Because encoding_rs is a Web-facing crate, it defaults to replacing unknowns with HTML entities. // This behaviour can be enabled with -i. Otherwise, it becomes an error. @@ -102,9 +115,7 @@ mod test { #[case::iso_8859_1("iso-8859-1", "Some ¼½¿ Data µ¶·¸¹º")] #[case::cp1252("cp1252", "Some ¼½¿ Data")] #[case::latin5("latin5", "Some ¼½¿ Data µ¶·¸¹º")] - // Tests for specific renditions of UTF-16 and UTF-8 labels - #[case::utf16("utf16", "")] - #[case::utf_hyphen_16("utf-16", "")] + // Tests for specific renditions of UTF-8 labels #[case::utf8("utf8", "")] #[case::utf_hyphen_8("utf-8", "")] fn smoke(#[case] encoding: String, #[case] expected: &str) {