From d611de5ac7588a34e1324ccf13d9449361fff66e Mon Sep 17 00:00:00 2001
From: sardap <22311832+sardap@users.noreply.github.com>
Date: Mon, 15 Jan 2024 20:40:45 +1100
Subject: [PATCH] added with_delimiter function to csv asset plugin allowing
user to change the delimiter used
---
src/csv.rs | 14 +++++++++++++-
1 file changed, 13 insertions(+), 1 deletion(-)
diff --git a/src/csv.rs b/src/csv.rs
index e17c8f3..28dd3f6 100644
--- a/src/csv.rs
+++ b/src/csv.rs
@@ -9,6 +9,7 @@ use thiserror::Error;
pub struct CsvAssetPlugin {
extensions: Vec<&'static str>,
_marker: PhantomData,
+ delimiter: u8,
}
impl Plugin for CsvAssetPlugin
@@ -21,6 +22,7 @@ where
.register_asset_loader(CsvAssetLoader:: {
extensions: self.extensions.clone(),
_marker: PhantomData,
+ delimiter: self.delimiter,
});
}
}
@@ -34,13 +36,21 @@ where
Self {
extensions: extensions.to_owned(),
_marker: PhantomData,
+ delimiter: b',',
}
}
+
+ /// Change the delimiter used to parse the CSV file.
+ pub fn with_delimiter(mut self, delimiter: u8) -> Self {
+ self.delimiter = delimiter;
+ self
+ }
}
struct CsvAssetLoader {
extensions: Vec<&'static str>,
_marker: PhantomData,
+ delimiter: u8,
}
/// Possible errors that can be produced by [`CsvAssetLoader`]
@@ -82,7 +92,9 @@ where
Box::pin(async move {
let mut bytes = Vec::new();
reader.read_to_end(&mut bytes).await?;
- let mut reader = csv::Reader::from_reader(bytes.as_slice());
+ let mut reader = csv::ReaderBuilder::new()
+ .delimiter(self.delimiter)
+ .from_reader(bytes.as_slice());
let mut handles = vec![];
for (index, result) in reader.deserialize().enumerate() {
let asset: A = result?;