Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,4 @@ azure_storage_blobs = { version = "0.21.0", default-features = false, features =
] }
serde_path_to_error = "0.1.17"
expect-test = "1.5.0"
encoding_rs = "0.8.35"
3 changes: 2 additions & 1 deletion src/ops/sources/amazon_s3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ impl SourceExecutor for Executor {
Some(SourceValue::Existence(if self.binary {
fields_value!(bytes.to_vec())
} else {
fields_value!(String::from_utf8_lossy(&bytes).to_string())
let (s, _) = utils::bytes_decode::bytes_to_string(&bytes);
fields_value!(s)
}))
} else {
None
Expand Down
3 changes: 2 additions & 1 deletion src/ops/sources/azure_blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ impl SourceExecutor for Executor {
Some(SourceValue::Existence(if self.binary {
fields_value!(bytes)
} else {
fields_value!(String::from_utf8_lossy(&bytes).to_string())
let (s, _) = utils::bytes_decode::bytes_to_string(&bytes);
fields_value!(s)
}))
} else {
None
Expand Down
5 changes: 2 additions & 3 deletions src/ops/sources/google_drive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,9 +400,8 @@ impl SourceExecutor for Executor {
if self.binary {
content.to_bytes().to_vec().into()
} else {
String::from_utf8_lossy(&content.to_bytes())
.to_string()
.into()
let (s, _) = utils::bytes_decode::bytes_to_string(&content.to_bytes());
s.into()
},
];
Some(SourceValue::Existence(FieldValues { fields }))
Expand Down
3 changes: 2 additions & 1 deletion src/ops/sources/local_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ impl SourceExecutor for Executor {
let content = if self.binary {
fields_value!(content)
} else {
fields_value!(String::from_utf8_lossy(&content).to_string())
let (s, _) = utils::bytes_decode::bytes_to_string(&content);
fields_value!(s)
};
Some(SourceValue::Existence(content))
}
Expand Down
12 changes: 12 additions & 0 deletions src/utils/bytes_decode.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use encoding_rs::Encoding;

pub fn bytes_to_string(bytes: &[u8]) -> (String, bool) {
// 1) BOM sniff first (definitive for UTF-8/16; UTF-32 is not supported here).
if let Some((enc, bom_len)) = Encoding::for_bom(bytes) {
let (cow, had_errors) = enc.decode_without_bom_handling(&bytes[bom_len..]);
return (cow.into_owned(), had_errors);
}
// 2) Otherwise, try UTF-8 (accepts input with or without a UTF-8 BOM).
let (cow, had_errors) = encoding_rs::UTF_8.decode_with_bom_removal(bytes);
(cow.into_owned(), had_errors)
}
1 change: 1 addition & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod bytes_decode;
pub mod concur_control;
pub mod db;
pub mod deser;
Expand Down
Loading