diff options
| author | Ellie Huxtable <ellie@elliehuxtable.com> | 2024-01-06 17:19:31 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-01-06 17:19:31 +0000 |
| commit | 4e56f5a41e5224b816e9eafed145a03cbf52fd93 (patch) | |
| tree | 3177cadbced62db723efaf6a7764f37e3c0e318a /atuin-client/src/history | |
| parent | fix(bash): prevent input to be interpreted as options for blesh auto-complete... (diff) | |
| download | atuin-4e56f5a41e5224b816e9eafed145a03cbf52fd93.zip | |
refactor: String -> HistoryId (#1512)
Diffstat (limited to '')
| -rw-r--r-- | atuin-client/src/history.rs | 29 | ||||
| -rw-r--r-- | atuin-client/src/history/builder.rs | 2 | ||||
| -rw-r--r-- | atuin-client/src/history/store.rs | 16 |
3 files changed, 32 insertions, 15 deletions
diff --git a/atuin-client/src/history.rs b/atuin-client/src/history.rs index 2b2c41ee..8c312dc2 100644 --- a/atuin-client/src/history.rs +++ b/atuin-client/src/history.rs @@ -1,6 +1,8 @@ +use core::fmt::Formatter; use rmp::decode::ValueReadError; use rmp::{decode::Bytes, Marker}; use std::env; +use std::fmt::Display; use atuin_common::record::DecryptedData; use atuin_common::utils::uuid_v7; @@ -17,6 +19,21 @@ pub mod store; const HISTORY_VERSION: &str = "v0"; const HISTORY_TAG: &str = "history"; +#[derive(Clone, Debug, Eq, PartialEq, Hash)] +pub struct HistoryId(pub String); + +impl Display for HistoryId { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.0) + } +} + +impl From<String> for HistoryId { + fn from(s: String) -> Self { + Self(s) + } +} + /// Client-side history entry. /// /// Client stores data unencrypted, and only encrypts it before sending to the server. @@ -35,7 +52,7 @@ pub struct History { /// A client-generated ID, used to identify the entry when syncing. /// /// Stored as `client_id` in the database. - pub id: String, + pub id: HistoryId, /// When the command was run. pub timestamp: OffsetDateTime, /// How long the command took to run. @@ -78,7 +95,7 @@ impl History { }); Self { - id: uuid_v7().as_simple().to_string(), + id: uuid_v7().as_simple().to_string().into(), timestamp, command, cwd, @@ -103,7 +120,7 @@ impl History { // INFO: ensure this is updated when adding new fields encode::write_array_len(&mut output, 9)?; - encode::write_str(&mut output, &self.id)?; + encode::write_str(&mut output, &self.id.0)?; encode::write_u64(&mut output, self.timestamp.unix_timestamp_nanos() as u64)?; encode::write_sint(&mut output, self.duration)?; encode::write_sint(&mut output, self.exit)?; @@ -170,7 +187,7 @@ impl History { } Ok(History { - id: id.to_owned(), + id: id.to_owned().into(), timestamp: OffsetDateTime::from_unix_timestamp_nanos(timestamp as i128)?, duration, exit, @@ -402,7 +419,7 @@ mod tests { ]; let history = History { - id: "66d16cbee7cd47538e5c5b8b44e9006e".to_owned(), + id: "66d16cbee7cd47538e5c5b8b44e9006e".to_owned().into(), timestamp: datetime!(2023-05-28 18:35:40.633872 +00:00), duration: 49206000, exit: 0, @@ -429,7 +446,7 @@ mod tests { #[test] fn test_serialize_deserialize_deleted() { let history = History { - id: "66d16cbee7cd47538e5c5b8b44e9006e".to_owned(), + id: "66d16cbee7cd47538e5c5b8b44e9006e".to_owned().into(), timestamp: datetime!(2023-05-28 18:35:40.633872 +00:00), duration: 49206000, exit: 0, diff --git a/atuin-client/src/history/builder.rs b/atuin-client/src/history/builder.rs index 27531c95..4e69cf66 100644 --- a/atuin-client/src/history/builder.rs +++ b/atuin-client/src/history/builder.rs @@ -85,7 +85,7 @@ pub struct HistoryFromDb { impl From<HistoryFromDb> for History { fn from(from_db: HistoryFromDb) -> Self { History { - id: from_db.id, + id: from_db.id.into(), timestamp: from_db.timestamp, exit: from_db.exit, command: from_db.command, diff --git a/atuin-client/src/history/store.rs b/atuin-client/src/history/store.rs index bf74a0a8..f4aa9d93 100644 --- a/atuin-client/src/history/store.rs +++ b/atuin-client/src/history/store.rs @@ -4,7 +4,7 @@ use rmp::decode::Bytes; use crate::record::{encryption::PASETO_V4, sqlite_store::SqliteStore, store::Store}; use atuin_common::record::{DecryptedData, Host, HostId, Record, RecordIdx}; -use super::{History, HISTORY_TAG, HISTORY_VERSION}; +use super::{History, HistoryId, HISTORY_TAG, HISTORY_VERSION}; #[derive(Debug)] pub struct HistoryStore { @@ -15,8 +15,8 @@ pub struct HistoryStore { #[derive(Debug, Eq, PartialEq, Clone)] pub enum HistoryRecord { - Create(History), // Create a history record - Delete(String), // Delete a history record, identified by ID + Create(History), // Create a history record + Delete(HistoryId), // Delete a history record, identified by ID } impl HistoryRecord { @@ -51,7 +51,7 @@ impl HistoryRecord { HistoryRecord::Delete(id) => { // 1 -> a history delete encode::write_u8(&mut output, 1)?; - encode::write_str(&mut output, id)?; + encode::write_str(&mut output, id.0.as_str())?; } }; @@ -92,7 +92,7 @@ impl HistoryRecord { ); } - Ok(HistoryRecord::Delete(id.to_string())) + Ok(HistoryRecord::Delete(id.to_string().into())) } n => { @@ -134,7 +134,7 @@ impl HistoryStore { Ok(idx) } - pub async fn delete(&self, id: String) -> Result<RecordIdx> { + pub async fn delete(&self, id: HistoryId) -> Result<RecordIdx> { let record = HistoryRecord::Delete(id); self.push_record(record).await @@ -171,7 +171,7 @@ mod tests { ]; let history = History { - id: "018cd4fe81757cd2aee65cd7861f9c81".to_owned(), + id: "018cd4fe81757cd2aee65cd7861f9c81".to_owned().into(), timestamp: datetime!(2024-01-04 00:00:00.000000 +00:00), duration: 100, exit: 0, @@ -203,7 +203,7 @@ mod tests { 204, 1, 217, 32, 48, 49, 56, 99, 100, 52, 102, 101, 56, 49, 55, 53, 55, 99, 100, 50, 97, 101, 101, 54, 53, 99, 100, 55, 56, 54, 49, 102, 57, 99, 56, 49, ]; - let record = HistoryRecord::Delete("018cd4fe81757cd2aee65cd7861f9c81".to_string()); + let record = HistoryRecord::Delete("018cd4fe81757cd2aee65cd7861f9c81".to_string().into()); let serialized = record.serialize().expect("failed to serialize history"); assert_eq!(serialized.0, bytes); |
