feat: splitting

This commit is contained in:
alexm committed 2026-09-26 01:15:12 -04:00
1 parent 220363d4d3
commit eabc98ad31
29 files changed
+4843 -234

No files matched your search

+4 -1
View File
@@ -767,7 +767,10 @@ impl FlatResponse {
email: none_if_empty(&self.email),
section: none_if_empty(&self.section),
item_number: self.item_number,
item_ref: none_if_empty(&self.item_ref),
// Canonicalized on read, so a term ingested before 2.0 pools with
// one ingested after it instead of splitting into two items.
item_ref: none_if_empty(&self.item_ref)
.map(|id| crate::item::canonical_id(&id).to_string()),
item_version: if self.item_version == 0 {
None
} else {
+67 -1
View File
@@ -316,6 +316,70 @@ pub fn read_path(path: &Path) -> Result<ResponseSet> {
}
}
/// Reads a response file without turning its rows into [`Response`]s.
///
/// What a migration wants: the rows exactly as they sit on disk, so rewriting
/// one column cannot disturb another through a round trip.
///
/// # Arguments
///
/// * `path` - the file to read.
///
/// # Returns
///
/// The rows.
///
/// # Errors
///
/// Returns [`Error::Other`] for an unrecognized extension, [`Error::Csv`] or
/// [`Error::Other`] on a parse failure, and [`Error::FeatureDisabled`] for
/// Parquet without the feature.
pub fn read_flat(path: &Path) -> Result<Vec<FlatResponse>> {
match Format::from_path(path) {
Some(Format::Csv) => {
let mut r = csv::Reader::from_path(path).map_err(|e| Error::Csv {
path: path.to_path_buf(),
source: e,
})?;
let mut out = Vec::new();
for rec in r.deserialize::<FlatResponse>() {
out.push(rec.map_err(|e| Error::Csv {
path: path.to_path_buf(),
source: e,
})?);
}
Ok(out)
}
Some(Format::Parquet) => crate::store_parquet::read(path),
None => Err(Error::Other(format!(
"{} is not a response file; expected a .parquet or .csv",
path.display()
))),
}
}
/// Writes flat responses back to the file they came from.
///
/// # Arguments
///
/// * `path` - the destination, whose extension picks the format.
/// * `rows` - the rows.
///
/// # Errors
///
/// Returns [`Error::Other`] for an unrecognized extension and
/// [`Error::FeatureDisabled`] for Parquet without the feature.
pub fn write_flat(path: &Path, rows: &[FlatResponse]) -> Result<()> {
match Format::from_path(path) {
Some(Format::Csv) => write_csv(path, rows),
Some(Format::Parquet) => write_parquet(path, rows),
None => Err(Error::Other(format!(
"{} is not a response file; expected a .parquet or .csv",
path.display()
))),
}
}
/// Writes flat responses as CSV.
///
/// # Arguments
@@ -591,7 +655,9 @@ mod tests {
let back = store.read("BIOSC1540/2026s/exam-4").unwrap();
assert_eq!(back.rows.len(), 2);
assert_eq!(back.rows[0].item_ref.as_deref(), Some("bank::q-x-001"));
// Canonicalized on the way in: the row was written with a pre-2.0
// `bank::item` key, and reading it yields the item it names.
assert_eq!(back.rows[0].item_ref.as_deref(), Some("q-x-001"));
assert_eq!(back.rows[0].selected, vec!["C".to_string()]);
assert_eq!(back.rows[0].learning_targets, vec!["lo-a".to_string()]);