From d2f4ec97c0a787b20ab88c03331fe23a852f5549 Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Tue, 26 Dec 2023 12:33:52 -0500 Subject: [PATCH] Rename UniqueId to RecordId --- emseries/src/lib.rs | 2 +- emseries/src/series.rs | 34 +++++++++++++++++----------------- emseries/src/types.rs | 14 +++++++------- emseries/tests/test_io.rs | 18 +++++++++--------- 4 files changed, 34 insertions(+), 34 deletions(-) diff --git a/emseries/src/lib.rs b/emseries/src/lib.rs index 08d1066..3973f63 100644 --- a/emseries/src/lib.rs +++ b/emseries/src/lib.rs @@ -76,4 +76,4 @@ mod types; pub use criteria::*; pub use series::Series; -pub use types::{EmseriesReadError, EmseriesWriteError, Recordable, Timestamp, UniqueId}; +pub use types::{EmseriesReadError, EmseriesWriteError, RecordId, Recordable, Timestamp}; diff --git a/emseries/src/series.rs b/emseries/src/series.rs index 98f0cb1..22367ae 100644 --- a/emseries/src/series.rs +++ b/emseries/src/series.rs @@ -24,7 +24,7 @@ use std::io::{BufRead, BufReader, LineWriter, Write}; use std::iter::Iterator; use criteria::Criteria; -use types::{EmseriesReadError, EmseriesWriteError, Record, Recordable, UniqueId}; +use types::{EmseriesReadError, EmseriesWriteError, Record, RecordId, Recordable}; /// An open time series database. /// @@ -33,7 +33,7 @@ use types::{EmseriesReadError, EmseriesWriteError, Record, Recordable, UniqueId} pub struct Series { //path: String, writer: LineWriter, - records: HashMap, + records: HashMap, } impl Series @@ -62,8 +62,8 @@ where } /// Load a file and return all of the records in it. - fn load_file(f: &File) -> Result, EmseriesReadError> { - let mut records: HashMap = HashMap::new(); + fn load_file(f: &File) -> Result, EmseriesReadError> { + let mut records: HashMap = HashMap::new(); let reader = BufReader::new(f); for line in reader.lines() { match line { @@ -87,14 +87,14 @@ where /// Put a new record into the database. A unique id will be assigned to the record and /// returned. - pub fn put(&mut self, entry: T) -> Result { - let uuid = UniqueId::default(); + pub fn put(&mut self, entry: T) -> Result { + let uuid = RecordId::default(); self.update(uuid.clone(), entry).map(|_| uuid) } - /// Update an existing record. The `UniqueId` of the record passed into this function must match - /// the `UniqueId` of a record already in the database. - pub fn update(&mut self, uuid: UniqueId, entry: T) -> Result<(), EmseriesWriteError> { + /// Update an existing record. The [RecordId] of the record passed into this function must match + /// the [RecordId] of a record already in the database. + pub fn update(&mut self, uuid: RecordId, entry: T) -> Result<(), EmseriesWriteError> { self.records.insert(uuid.clone(), entry.clone()); let write_res = match serde_json::to_string(&Record { id: uuid, @@ -118,7 +118,7 @@ where /// Future note: while this deletes a record from the view, it only adds an entry to the /// database that indicates `data: null`. If record histories ever become important, the record /// and its entire history (including this delete) will still be available. - pub fn delete(&mut self, uuid: &UniqueId) -> Result<(), EmseriesWriteError> { + pub fn delete(&mut self, uuid: &RecordId) -> Result<(), EmseriesWriteError> { if !self.records.contains_key(uuid) { return Ok(()); }; @@ -138,7 +138,7 @@ where } /// Get all of the records in the database. - pub fn records(&self) -> impl Iterator { + pub fn records(&self) -> impl Iterator { self.records.iter() } @@ -148,29 +148,29 @@ where pub fn search<'s>( &'s self, criteria: impl Criteria + 's, - ) -> impl Iterator + 's { + ) -> impl Iterator + 's { self.records().filter(move |&tr| criteria.apply(tr.1)) } /// Perform a search and sort the resulting records based on the comparison. - pub fn search_sorted<'s, C, CMP>(&'s self, criteria: C, compare: CMP) -> Vec<(&UniqueId, &T)> + pub fn search_sorted<'s, C, CMP>(&'s self, criteria: C, compare: CMP) -> Vec<(&RecordId, &T)> where C: Criteria + 's, - CMP: FnMut(&(&UniqueId, &T), &(&UniqueId, &T)) -> Ordering, + CMP: FnMut(&(&RecordId, &T), &(&RecordId, &T)) -> Ordering, { let search_iter = self.search(criteria); - let mut records: Vec<(&UniqueId, &T)> = search_iter.collect(); + let mut records: Vec<(&RecordId, &T)> = search_iter.collect(); records.sort_by(compare); records } /// Get an exact record from the database based on unique id. - pub fn get(&self, uuid: &UniqueId) -> Option { + pub fn get(&self, uuid: &RecordId) -> Option { self.records.get(uuid).cloned() } /* - pub fn remove(&self, uuid: UniqueId) -> Result<(), EmseriesError> { + pub fn remove(&self, uuid: RecordId) -> Result<(), EmseriesError> { unimplemented!() } */ diff --git a/emseries/src/types.rs b/emseries/src/types.rs index 0f29153..61f1b8a 100644 --- a/emseries/src/types.rs +++ b/emseries/src/types.rs @@ -145,26 +145,26 @@ pub trait Recordable { /// /// This is a wrapper around a basic uuid with some extra convenience methods. #[derive(Clone, Debug, Eq, Hash, PartialEq, Deserialize, Serialize)] -pub struct UniqueId(Uuid); +pub struct RecordId(Uuid); -impl Default for UniqueId { +impl Default for RecordId { fn default() -> Self { Self(Uuid::new_v4()) } } -impl str::FromStr for UniqueId { +impl str::FromStr for RecordId { type Err = EmseriesReadError; - /// Parse a UniqueId from a string. Raise UUIDParseError if the parsing fails. + /// Parse a RecordId from a string. Raise UUIDParseError if the parsing fails. fn from_str(val: &str) -> Result { Uuid::parse_str(val) - .map(UniqueId) + .map(RecordId) .map_err(EmseriesReadError::UUIDParseError) } } -impl fmt::Display for UniqueId { +impl fmt::Display for RecordId { /// Convert to a hyphenated string fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { write!(f, "{}", self.0.to_hyphenated()) @@ -175,7 +175,7 @@ impl fmt::Display for UniqueId { /// Recordable trait. #[derive(Clone, Deserialize, Serialize)] pub struct Record { - pub id: UniqueId, + pub id: RecordId, pub data: Option, } diff --git a/emseries/tests/test_io.rs b/emseries/tests/test_io.rs index 9d120b5..2cf34c0 100644 --- a/emseries/tests/test_io.rs +++ b/emseries/tests/test_io.rs @@ -162,7 +162,7 @@ mod test { ts.put(trip.clone()).expect("expect a successful put"); } - let v: Vec<(&UniqueId, &BikeTrip)> = ts + let v: Vec<(&RecordId, &BikeTrip)> = ts .search(exact_time(Timestamp::DateTime( UTC.with_ymd_and_hms(2011, 10, 31, 0, 0, 0) .unwrap() @@ -185,7 +185,7 @@ mod test { ts.put(trip.clone()).expect("expect a successful put"); } - let v: Vec<(&UniqueId, &BikeTrip)> = ts.search_sorted( + let v: Vec<(&RecordId, &BikeTrip)> = ts.search_sorted( time_range( Timestamp::DateTime( UTC.with_ymd_and_hms(2011, 10, 31, 0, 0, 0) @@ -226,7 +226,7 @@ mod test { { let ts: Series = Series::open(&path).expect("expect the time series to open correctly"); - let v: Vec<(&UniqueId, &BikeTrip)> = ts.search_sorted( + let v: Vec<(&RecordId, &BikeTrip)> = ts.search_sorted( time_range( Timestamp::DateTime( UTC.with_ymd_and_hms(2011, 10, 31, 0, 0, 0) @@ -268,7 +268,7 @@ mod test { { let mut ts: Series = Series::open(&path).expect("expect the time series to open correctly"); - let v: Vec<(&UniqueId, &BikeTrip)> = ts.search_sorted( + let v: Vec<(&RecordId, &BikeTrip)> = ts.search_sorted( time_range( Timestamp::DateTime( UTC.with_ymd_and_hms(2011, 10, 31, 0, 0, 0) @@ -296,7 +296,7 @@ mod test { { let ts: Series = Series::open(&path).expect("expect the time series to open correctly"); - let v: Vec<(&UniqueId, &BikeTrip)> = ts.search_sorted( + let v: Vec<(&RecordId, &BikeTrip)> = ts.search_sorted( time_range( Timestamp::DateTime( UTC.with_ymd_and_hms(2011, 10, 31, 0, 0, 0) @@ -384,10 +384,10 @@ mod test { let ts: Series = Series::open(&path).expect("expect the time series to open correctly"); - let trips: Vec<(&UniqueId, &BikeTrip)> = ts.records().collect(); + let trips: Vec<(&RecordId, &BikeTrip)> = ts.records().collect(); assert_eq!(trips.len(), 3); - let trips: Vec<(&UniqueId, &BikeTrip)> = ts + let trips: Vec<(&RecordId, &BikeTrip)> = ts .search(exact_time(Timestamp::DateTime( UTC.with_ymd_and_hms(2011, 11, 02, 0, 0, 0) .unwrap() @@ -421,14 +421,14 @@ mod test { ts.put(trips[2].clone()).expect("expect a successful put"); ts.delete(&trip_id).expect("successful delete"); - let recs: Vec<(&UniqueId, &BikeTrip)> = ts.records().collect(); + let recs: Vec<(&RecordId, &BikeTrip)> = ts.records().collect(); assert_eq!(recs.len(), 2); } { let ts: Series = Series::open(&path).expect("expect the time series to open correctly"); - let recs: Vec<(&UniqueId, &BikeTrip)> = ts.records().collect(); + let recs: Vec<(&RecordId, &BikeTrip)> = ts.records().collect(); assert_eq!(recs.len(), 2); } })