Skip to content

Commit

Permalink
Make history-related items (de-)serializable (#678)
Browse files Browse the repository at this point in the history
* Make history-related items (de-)serializable

* Fix: remove duplicate derive

* Fix: deserialization bound on generic type parameter

* Comment on the custom deserialize_with attribute
  • Loading branch information
ClementNerma authored Dec 6, 2023
1 parent ca2f6c8 commit 43944ee
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ version = "0.26.0"
doctest = true

[dependencies]
chrono = { version = "0.4.19", default-features = false, features = ["clock"] }
chrono = { version = "0.4.19", default-features = false, features = [
"clock",
"serde",
] }
clipboard = { version = "0.5.0", optional = true }
crossbeam = { version = "0.8.2", optional = true }
crossterm = { version = "0.27.0", features = ["serde"] }
Expand Down
10 changes: 6 additions & 4 deletions src/history/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ use serde::{de::DeserializeOwned, Deserialize, Serialize};
use std::{fmt::Display, time::Duration};

/// Unique ID for the [`HistoryItem`]. More recent items have higher ids than older ones.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct HistoryItemId(pub i64);

impl HistoryItemId {
/// Create a new `HistoryItemId` value
pub const fn new(i: i64) -> HistoryItemId {
Expand All @@ -22,7 +21,7 @@ impl Display for HistoryItemId {
}

/// Unique ID for the session in which reedline was run to disambiguate different sessions
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct HistorySessionId(pub(crate) i64);
impl HistorySessionId {
pub(crate) const fn new(i: i64) -> HistorySessionId {
Expand Down Expand Up @@ -79,7 +78,7 @@ impl<'de> Deserialize<'de> for IgnoreAllExtraInfo {
impl HistoryItemExtraInfo for IgnoreAllExtraInfo {}

/// Represents one run command with some optional additional context
#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct HistoryItem<ExtraInfo: HistoryItemExtraInfo = IgnoreAllExtraInfo> {
/// primary key, unique across one history
pub id: Option<HistoryItemId>,
Expand All @@ -99,6 +98,9 @@ pub struct HistoryItem<ExtraInfo: HistoryItemExtraInfo = IgnoreAllExtraInfo> {
/// the exit status of the command
pub exit_status: Option<i64>,
/// arbitrary additional information that might be interesting
/// NOTE: this attribute is required because of https://github.com/rust-lang/rust/issues/41617
/// (see https://github.com/serde-rs/serde/issues/1296#issuecomment-394056188 for the fix)
#[serde(deserialize_with = "Option::<ExtraInfo>::deserialize")]
pub more_info: Option<ExtraInfo>,
}

Expand Down

0 comments on commit 43944ee

Please sign in to comment.