Skip to content

Commit 3e2ddc4

Browse files
authored
feat(history): export HistoryItemExtraInfo etc. (#1011)
1 parent 99764ee commit 3e2ddc4

File tree

4 files changed

+95
-4
lines changed

4 files changed

+95
-4
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ unicode-width = "0.2"
4040
gethostname = "0.4.0"
4141
pretty_assertions = "1.4.0"
4242
rstest = { version = "0.23.0", default-features = false }
43+
serde_json = "1.0"
4344
tempfile = "3.3.0"
4445

4546
[features]

src/history/item.rs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,91 @@ impl HistoryItem {
121121
}
122122
}
123123
}
124+
125+
#[cfg(test)]
126+
mod tests {
127+
use super::*;
128+
129+
/// Example custom extra info for testing.
130+
/// Downstream crates can implement their own types like this.
131+
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
132+
struct CustomExtraInfo {
133+
mode: String,
134+
tags: Vec<String>,
135+
}
136+
137+
impl HistoryItemExtraInfo for CustomExtraInfo {}
138+
139+
#[test]
140+
fn test_history_item_with_default_extra_info() {
141+
let item = HistoryItem::from_command_line("echo hello");
142+
assert_eq!(item.command_line, "echo hello");
143+
assert!(item.more_info.is_none());
144+
}
145+
146+
#[test]
147+
fn test_history_item_with_custom_extra_info() {
148+
let item: HistoryItem<CustomExtraInfo> = HistoryItem {
149+
id: None,
150+
start_timestamp: None,
151+
command_line: "echo hello".to_string(),
152+
session_id: None,
153+
hostname: None,
154+
cwd: None,
155+
duration: None,
156+
exit_status: None,
157+
more_info: Some(CustomExtraInfo {
158+
mode: "shell".to_string(),
159+
tags: vec!["test".to_string()],
160+
}),
161+
};
162+
163+
assert_eq!(item.command_line, "echo hello");
164+
let extra = item.more_info.unwrap();
165+
assert_eq!(extra.mode, "shell");
166+
assert_eq!(extra.tags, vec!["test".to_string()]);
167+
}
168+
169+
#[test]
170+
fn test_custom_extra_info_serialization() {
171+
let item: HistoryItem<CustomExtraInfo> = HistoryItem {
172+
id: Some(HistoryItemId::new(1)),
173+
start_timestamp: None,
174+
command_line: "ls -la".to_string(),
175+
session_id: None,
176+
hostname: None,
177+
cwd: Some("/home/user".to_string()),
178+
duration: None,
179+
exit_status: Some(0),
180+
more_info: Some(CustomExtraInfo {
181+
mode: "r".to_string(),
182+
tags: vec!["data".to_string(), "analysis".to_string()],
183+
}),
184+
};
185+
186+
// Serialize to JSON
187+
let json = serde_json::to_string(&item).expect("serialization should succeed");
188+
assert!(json.contains("\"mode\":\"r\""));
189+
assert!(json.contains("\"tags\":[\"data\",\"analysis\"]"));
190+
191+
// Deserialize back
192+
let deserialized: HistoryItem<CustomExtraInfo> =
193+
serde_json::from_str(&json).expect("deserialization should succeed");
194+
assert_eq!(deserialized.command_line, "ls -la");
195+
assert_eq!(deserialized.more_info.as_ref().unwrap().mode, "r");
196+
}
197+
198+
#[test]
199+
fn test_ignore_all_extra_info_serialization() {
200+
let item = HistoryItem::from_command_line("pwd");
201+
202+
// Serialize - more_info should be null
203+
let json = serde_json::to_string(&item).expect("serialization should succeed");
204+
assert!(json.contains("\"more_info\":null"));
205+
206+
// Deserialize back
207+
let deserialized: HistoryItem =
208+
serde_json::from_str(&json).expect("deserialization should succeed");
209+
assert_eq!(deserialized.command_line, "pwd");
210+
}
211+
}

src/history/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ pub use base::{
1111
CommandLineSearch, History, HistoryNavigationQuery, SearchDirection, SearchFilter, SearchQuery,
1212
};
1313
pub use cursor::HistoryCursor;
14-
pub use item::{HistoryItem, HistoryItemId, HistorySessionId};
14+
pub use item::{
15+
HistoryItem, HistoryItemExtraInfo, HistoryItemId, HistorySessionId, IgnoreAllExtraInfo,
16+
};
1517

1618
pub use file_backed::{FileBackedHistory, HISTORY_SIZE};

src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,9 @@ mod history;
257257
#[cfg(any(feature = "sqlite", feature = "sqlite-dynlib"))]
258258
pub use history::SqliteBackedHistory;
259259
pub use history::{
260-
CommandLineSearch, FileBackedHistory, History, HistoryItem, HistoryItemId,
261-
HistoryNavigationQuery, HistorySessionId, SearchDirection, SearchFilter, SearchQuery,
262-
HISTORY_SIZE,
260+
CommandLineSearch, FileBackedHistory, History, HistoryItem, HistoryItemExtraInfo,
261+
HistoryItemId, HistoryNavigationQuery, HistorySessionId, IgnoreAllExtraInfo, SearchDirection,
262+
SearchFilter, SearchQuery, HISTORY_SIZE,
263263
};
264264

265265
mod prompt;

0 commit comments

Comments
 (0)