Skip to content
Open
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
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod loader;

pub use crate::loader::{CsvLoader, ExactSizeIterable, Loader};
pub use crate::loader::{CsvLoader, Loader, Record};
35 changes: 29 additions & 6 deletions src/loader.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,41 @@
use std::fs::File;
use std::iter;

pub trait ExactSizeIterable {
fn iter(&self) -> impl iter::ExactSizeIterator<Item = &[u8]>;
pub trait AsRef<'a, T>
where
T: ?Sized,
{
fn as_ref(&self) -> &'a T;
}

impl ExactSizeIterable for csv::ByteRecord {
fn iter(&self) -> impl iter::ExactSizeIterator<Item = &[u8]> {
impl<'a> AsRef<'a, [u8]> for &'a [u8] {
fn as_ref(&self) -> &'a [u8] {
self
}
}

pub trait Record {
type Item<'a>: AsRef<'a, [u8]>
where
Self: 'a;

type Iter<'a>: ExactSizeIterator<Item = Self::Item<'a>>
where
Self: 'a;

fn iter(&self) -> Self::Iter<'_>;
}

impl Record for csv::ByteRecord {
type Item<'a> = &'a [u8];
type Iter<'a> = csv::ByteRecordIter<'a>;

fn iter(&self) -> Self::Iter<'_> {
self.into_iter()
}
}

pub trait Loader {
type RecordType: ExactSizeIterable;
type RecordType: Record;

/// Name of the resource we're loading from (e.g., a file path).
fn name(&self) -> &str;
Expand Down
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use clap::Parser;
use std::iter;
use std::sync::LazyLock;

use csvsql::ExactSizeIterable;
use csvsql::loader::AsRef;
use csvsql::Record;

fn normalize_col(col: &str) -> String {
static RE: LazyLock<regex::Regex> = LazyLock::new(|| regex::Regex::new(r"\(.*?\)$").unwrap());
Expand Down Expand Up @@ -92,6 +93,7 @@ fn _load_table_from_loader(
let record = record?;
let row = record.iter();
let row_len = row.len();
let row = row.map(|v| v.as_ref());
if row_len > normalized_cols.len() {
anyhow::bail!(
"Too many fields on row {}, fields: {:?}",
Expand Down
Loading