factor entry code out for later

This commit is contained in:
Damien Elmes 2020-02-08 20:57:18 +10:00
parent ce241f9756
commit 8aa2984d04

View file

@ -3,7 +3,7 @@
use crate::err::Result;
use log::debug;
use rusqlite::{params, Connection, OptionalExtension, Statement, NO_PARAMS};
use rusqlite::{params, Connection, OptionalExtension, Row, Statement, NO_PARAMS};
use std::collections::HashMap;
use std::path::Path;
@ -131,26 +131,7 @@ impl MediaDatabaseContext<'_> {
select fname, csum, mtime, dirty from media where fname=?"
);
stmt.query_row(params![fname], |row| {
// map the string checksum into bytes
let sha1_str: Option<String> = row.get(1)?;
let sha1_array = if let Some(s) = sha1_str {
let mut arr = [0; 20];
match hex::decode_to_slice(s, arr.as_mut()) {
Ok(_) => Some(arr),
_ => None,
}
} else {
None
};
// and return the entry
Ok(MediaEntry {
fname: row.get(0)?,
sha1: sha1_array,
mtime: row.get(2)?,
sync_required: row.get(3)?,
})
})
stmt.query_row(params![fname], row_to_entry)
.optional()
.map_err(Into::into)
}
@ -246,6 +227,27 @@ delete from media where fname=?"
}
}
fn row_to_entry(row: &Row) -> rusqlite::Result<MediaEntry> {
// map the string checksum into bytes
let sha1_str: Option<String> = row.get(1)?;
let sha1_array = if let Some(s) = sha1_str {
let mut arr = [0; 20];
match hex::decode_to_slice(s, arr.as_mut()) {
Ok(_) => Some(arr),
_ => None,
}
} else {
None
};
// and return the entry
Ok(MediaEntry {
fname: row.get(0)?,
sha1: sha1_array,
mtime: row.get(2)?,
sync_required: row.get(3)?,
})
}
#[cfg(test)]
mod test {
use crate::err::Result;