Repository: fujiapple852/trippy (language: rust)

Issue:
Trippy fails to parse generated config file on Windows
On Windows, when you run the below command to generate a template configuration file:

```
trip.exe --print-config-template > trippy.toml
```

Then run Trippy:

```
trip.exe example.com
```

It fails with error:

```
Stream did not contain valid UTF-8
```

Trippy writes out the config template as valid UTF-8 and expects to read it back as valid UTF-8.  However the above command results in `trippy.toml` being encoded as UTF-16 LE on Windows.  This occurs as the redirect to file operation (i.e. `>`) in Windows shells (PowerShell and cmd.exe) will re-encoded the byes as UTF-16 LE.

Trippy should be fixed to allow reading config files encoded in various encodings.


Discussion / hints:

Fix patch:
diff --git a/Cargo.lock b/Cargo.lock
index 1889d39bd..c945bfeba 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -519,6 +519,24 @@ version = "1.9.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07"
 
+[[package]]
+name = "encoding_rs"
+version = "0.8.33"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7268b386296a025e474d5140678f75d6de9493ae55a5d709eeb9dd08149945e1"
+dependencies = [
+ "cfg-if",
+]
+
+[[package]]
+name = "encoding_rs_io"
+version = "0.1.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1cc3c5651fb62ab8aa3103998dade57efdd028544bd300516baa31840c252a83"
+dependencies = [
+ "encoding_rs",
+]
+
 [[package]]
 name = "enum-as-inner"
 version = "0.6.0"
@@ -1834,6 +1852,7 @@ dependencies = [
  "csv",
  "derive_more",
  "dns-lookup",
+ "encoding_rs_io",
  "etcetera",
  "hex-literal",
  "hickory-resolver",
diff --git a/Cargo.toml b/Cargo.toml
index 2ce155b9c..15c41f81e 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -54,6 +54,7 @@ tracing-chrome = "0.7.1"
 petgraph = "0.6.4"
 csv = "1.3.0"
 serde_with = "3.5.1"
+encoding_rs_io = "0.1.7"
 
 # Library dependencies (Linux)
 [target.'cfg(target_os = "linux")'.dependencies]
diff --git a/deny.toml b/deny.toml
index 03e2b8d30..f194f9dd4 100644
--- a/deny.toml
+++ b/deny.toml
@@ -1,6 +1,6 @@
 [licenses]
 unlicensed = "deny"
-allow = [ "Apache-2.0", "MIT", "Unicode-DFS-2016", "ISC", "BSD-2-Clause" ]
+allow = [ "Apache-2.0", "MIT", "Unicode-DFS-2016", "ISC", "BSD-2-Clause", "BSD-3-Clause", "WTFPL" ]
 copyleft = "deny"
 allow-osi-fsf-free = "neither"
 default = "deny"
diff --git a/src/config/file.rs b/src/config/file.rs
index 5b457b761..a9fe30c03 100644
--- a/src/config/file.rs
+++ b/src/config/file.rs
@@ -5,11 +5,12 @@ use crate::config::{
     LogFormat, LogSpanEvents, Mode, MultipathStrategyConfig, ProtocolConfig,
 };
 use anyhow::Context;
+use encoding_rs_io::DecodeReaderBytes;
 use etcetera::BaseStrategy;
 use humantime::format_duration;
 use serde::Deserialize;
 use std::fs::File;
-use std::io::read_to_string;
+use std::io::{BufReader, Read};
 use std::path::Path;
 use trippy::tracing::defaults;
 
@@ -49,7 +50,10 @@ pub fn read_default_config_file() -> anyhow::Result<Option<ConfigFile>> {
 pub fn read_config_file<P: AsRef<Path>>(path: P) -> anyhow::Result<ConfigFile> {
     let file = File::open(path.as_ref())
         .with_context(|| format!("config file not found: {:?}", path.as_ref()))?;
-    Ok(toml::from_str(&read_to_string(file)?)?)
+    let mut decoder = DecodeReaderBytes::new(BufReader::new(file));
+    let mut dest = String::new();
+    decoder.read_to_string(&mut dest)?;
+    Ok(toml::from_str(&dest)?)
 }
 
 fn read_files<P: AsRef<Path>>(dir: P) -> anyhow::Result<Option<ConfigFile>> {


Test patch: