Make the series open function accept anything that can be a path reference

This commit is contained in:
Savanni D'Gerinel 2023-12-06 23:32:33 -05:00
parent 0ac9bb74a6
commit f9db002464
2 changed files with 27 additions and 27 deletions

View File

@ -42,7 +42,7 @@ where
{ {
/// Open a time series database at the specified path. `path` is the full path and filename for /// Open a time series database at the specified path. `path` is the full path and filename for
/// the database. /// the database.
pub fn open(path: &str) -> Result<Series<T>, EmseriesReadError> { pub fn open<P: AsRef<std::path::Path>>(path: P) -> Result<Series<T>, EmseriesReadError> {
let f = OpenOptions::new() let f = OpenOptions::new()
.read(true) .read(true)
.append(true) .append(true)

View File

@ -99,8 +99,8 @@ mod test {
{ {
let tmp_file = tempfile::NamedTempFile::new().expect("temporary path created"); let tmp_file = tempfile::NamedTempFile::new().expect("temporary path created");
let tmp_path = tmp_file.into_temp_path(); let tmp_path = tmp_file.into_temp_path();
let ts: Series<BikeTrip> = Series::open(&tmp_path.to_string_lossy()) let ts: Series<BikeTrip> =
.expect("the time series should open correctly"); Series::open(&tmp_path).expect("the time series should open correctly");
test(ts); test(ts);
} }
@ -136,8 +136,8 @@ mod test {
pub fn can_search_for_an_entry_with_exact_time() { pub fn can_search_for_an_entry_with_exact_time() {
run_test(|path| { run_test(|path| {
let trips = mk_trips(); let trips = mk_trips();
let mut ts: Series<BikeTrip> = Series::open(&path.to_string_lossy()) let mut ts: Series<BikeTrip> =
.expect("expect the time series to open correctly"); Series::open(&path).expect("expect the time series to open correctly");
for trip in &trips[0..=4] { for trip in &trips[0..=4] {
ts.put(trip.clone()).expect("expect a successful put"); ts.put(trip.clone()).expect("expect a successful put");
@ -157,8 +157,8 @@ mod test {
pub fn can_get_entries_in_time_range() { pub fn can_get_entries_in_time_range() {
run_test(|path| { run_test(|path| {
let trips = mk_trips(); let trips = mk_trips();
let mut ts: Series<BikeTrip> = Series::open(&path.to_string_lossy()) let mut ts: Series<BikeTrip> =
.expect("expect the time series to open correctly"); Series::open(&path).expect("expect the time series to open correctly");
for trip in &trips[0..=4] { for trip in &trips[0..=4] {
ts.put(trip.clone()).expect("expect a successful put"); ts.put(trip.clone()).expect("expect a successful put");
@ -186,8 +186,8 @@ mod test {
let trips = mk_trips(); let trips = mk_trips();
{ {
let mut ts: Series<BikeTrip> = Series::open(&path.to_string_lossy()) let mut ts: Series<BikeTrip> =
.expect("expect the time series to open correctly"); Series::open(&path).expect("expect the time series to open correctly");
for trip in &trips[0..=4] { for trip in &trips[0..=4] {
ts.put(trip.clone()).expect("expect a successful put"); ts.put(trip.clone()).expect("expect a successful put");
@ -195,8 +195,8 @@ mod test {
} }
{ {
let ts: Series<BikeTrip> = Series::open(&path.to_string_lossy()) let ts: Series<BikeTrip> =
.expect("expect the time series to open correctly"); Series::open(&path).expect("expect the time series to open correctly");
let v: Vec<(&UniqueId, &BikeTrip)> = ts.search_sorted( let v: Vec<(&UniqueId, &BikeTrip)> = ts.search_sorted(
time_range( time_range(
DateTimeTz(UTC.with_ymd_and_hms(2011, 10, 31, 0, 0, 0).unwrap()).into(), DateTimeTz(UTC.with_ymd_and_hms(2011, 10, 31, 0, 0, 0).unwrap()).into(),
@ -220,8 +220,8 @@ mod test {
let trips = mk_trips(); let trips = mk_trips();
{ {
let mut ts: Series<BikeTrip> = Series::open(&path.to_string_lossy()) let mut ts: Series<BikeTrip> =
.expect("expect the time series to open correctly"); Series::open(&path).expect("expect the time series to open correctly");
for trip in &trips[0..=2] { for trip in &trips[0..=2] {
ts.put(trip.clone()).expect("expect a successful put"); ts.put(trip.clone()).expect("expect a successful put");
@ -229,8 +229,8 @@ mod test {
} }
{ {
let mut ts: Series<BikeTrip> = Series::open(&path.to_string_lossy()) let mut ts: Series<BikeTrip> =
.expect("expect the time series to open correctly"); Series::open(&path).expect("expect the time series to open correctly");
let v: Vec<(&UniqueId, &BikeTrip)> = ts.search_sorted( let v: Vec<(&UniqueId, &BikeTrip)> = ts.search_sorted(
time_range( time_range(
DateTimeTz(UTC.with_ymd_and_hms(2011, 10, 31, 0, 0, 0).unwrap()).into(), DateTimeTz(UTC.with_ymd_and_hms(2011, 10, 31, 0, 0, 0).unwrap()).into(),
@ -248,8 +248,8 @@ mod test {
} }
{ {
let ts: Series<BikeTrip> = Series::open(&path.to_string_lossy()) let ts: Series<BikeTrip> =
.expect("expect the time series to open correctly"); Series::open(&path).expect("expect the time series to open correctly");
let v: Vec<(&UniqueId, &BikeTrip)> = ts.search_sorted( let v: Vec<(&UniqueId, &BikeTrip)> = ts.search_sorted(
time_range( time_range(
DateTimeTz(UTC.with_ymd_and_hms(2011, 10, 31, 0, 0, 0).unwrap()).into(), DateTimeTz(UTC.with_ymd_and_hms(2011, 10, 31, 0, 0, 0).unwrap()).into(),
@ -273,8 +273,8 @@ mod test {
run_test(|path| { run_test(|path| {
let trips = mk_trips(); let trips = mk_trips();
let mut ts: Series<BikeTrip> = Series::open(&path.to_string_lossy()) let mut ts: Series<BikeTrip> =
.expect("expect the time series to open correctly"); Series::open(&path).expect("expect the time series to open correctly");
ts.put(trips[0].clone()).expect("expect a successful put"); ts.put(trips[0].clone()).expect("expect a successful put");
ts.put(trips[1].clone()).expect("expect a successful put"); ts.put(trips[1].clone()).expect("expect a successful put");
@ -310,8 +310,8 @@ mod test {
let trips = mk_trips(); let trips = mk_trips();
{ {
let mut ts: Series<BikeTrip> = Series::open(&path.to_string_lossy()) let mut ts: Series<BikeTrip> =
.expect("expect the time series to open correctly"); Series::open(&path).expect("expect the time series to open correctly");
ts.put(trips[0].clone()).expect("expect a successful put"); ts.put(trips[0].clone()).expect("expect a successful put");
ts.put(trips[1].clone()).expect("expect a successful put"); ts.put(trips[1].clone()).expect("expect a successful put");
@ -327,8 +327,8 @@ mod test {
} }
{ {
let ts: Series<BikeTrip> = Series::open(&path.to_string_lossy()) let ts: Series<BikeTrip> =
.expect("expect the time series to open correctly"); Series::open(&path).expect("expect the time series to open correctly");
let trips: Vec<(&UniqueId, &BikeTrip)> = ts.records().collect(); let trips: Vec<(&UniqueId, &BikeTrip)> = ts.records().collect();
assert_eq!(trips.len(), 3); assert_eq!(trips.len(), 3);
@ -356,8 +356,8 @@ mod test {
let trips = mk_trips(); let trips = mk_trips();
{ {
let mut ts: Series<BikeTrip> = Series::open(&path.to_string_lossy()) let mut ts: Series<BikeTrip> =
.expect("expect the time series to open correctly"); Series::open(&path).expect("expect the time series to open correctly");
let trip_id = ts.put(trips[0].clone()).expect("expect a successful put"); let trip_id = ts.put(trips[0].clone()).expect("expect a successful put");
ts.put(trips[1].clone()).expect("expect a successful put"); ts.put(trips[1].clone()).expect("expect a successful put");
ts.put(trips[2].clone()).expect("expect a successful put"); ts.put(trips[2].clone()).expect("expect a successful put");
@ -368,8 +368,8 @@ mod test {
} }
{ {
let ts: Series<BikeTrip> = Series::open(&path.to_string_lossy()) let ts: Series<BikeTrip> =
.expect("expect the time series to open correctly"); Series::open(&path).expect("expect the time series to open correctly");
let recs: Vec<(&UniqueId, &BikeTrip)> = ts.records().collect(); let recs: Vec<(&UniqueId, &BikeTrip)> = ts.records().collect();
assert_eq!(recs.len(), 2); assert_eq!(recs.len(), 2);
} }