aboutsummaryrefslogtreecommitdiffstats
path: root/crates/turtle/src/atuin_client/history/builder.rs
blob: daa4ef49f587ac20abcda435eb67e8dd4c05206b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
use typed_builder::TypedBuilder;

use super::History;

/// Builder for a history entry that is imported from shell history.
///
/// The only two required fields are `timestamp` and `command`.
#[derive(Debug, Clone, TypedBuilder)]
pub(crate) struct HistoryImported {
    timestamp: time::OffsetDateTime,
    #[builder(setter(into))]
    command: String,
    #[builder(default = "unknown".into(), setter(into))]
    cwd: String,
    #[builder(default = -1)]
    exit: i64,
    #[builder(default = -1)]
    duration: i64,
    #[builder(default, setter(strip_option, into))]
    session: Option<String>,
    #[builder(default, setter(strip_option, into))]
    hostname: Option<String>,
    #[builder(default, setter(strip_option, into))]
    author: Option<String>,
    #[builder(default, setter(strip_option, into))]
    intent: Option<String>,
}

impl From<HistoryImported> for History {
    fn from(imported: HistoryImported) -> Self {
        Self::new(
            imported.timestamp,
            imported.command,
            imported.cwd,
            imported.exit,
            imported.duration,
            imported.session,
            imported.hostname,
            imported.author,
            imported.intent,
            None,
        )
    }
}

/// Builder for a history entry that is captured via hook.
///
/// This builder is used only at the `start` step of the hook,
/// so it doesn't have any fields which are known only after
/// the command is finished, such as `exit` or `duration`.
#[derive(Debug, Clone, TypedBuilder)]
pub(crate) struct HistoryCaptured {
    timestamp: time::OffsetDateTime,
    #[builder(setter(into))]
    command: String,
    #[builder(setter(into))]
    cwd: String,
    #[builder(default, setter(strip_option, into))]
    author: Option<String>,
    #[builder(default, setter(strip_option, into))]
    intent: Option<String>,
}

impl From<HistoryCaptured> for History {
    fn from(captured: HistoryCaptured) -> Self {
        Self::new(
            captured.timestamp,
            captured.command,
            captured.cwd,
            -1,
            -1,
            None,
            None,
            captured.author,
            captured.intent,
            None,
        )
    }
}

/// Builder for a history entry that is loaded from the database.
///
/// All fields are required, as they are all present in the database.
#[derive(Debug, Clone, TypedBuilder)]
pub(crate) struct HistoryFromDb {
    id: String,
    timestamp: time::OffsetDateTime,
    command: String,
    cwd: String,
    exit: i64,
    duration: i64,
    session: String,
    hostname: String,
    author: String,
    intent: Option<String>,
    deleted_at: Option<time::OffsetDateTime>,
}

impl From<HistoryFromDb> for History {
    fn from(from_db: HistoryFromDb) -> Self {
        Self {
            id: from_db.id.into(),
            timestamp: from_db.timestamp,
            exit: from_db.exit,
            command: from_db.command,
            cwd: from_db.cwd,
            duration: from_db.duration,
            session: from_db.session,
            hostname: from_db.hostname,
            author: from_db.author,
            intent: from_db.intent,
            deleted_at: from_db.deleted_at,
        }
    }
}

/// Builder for a history entry that is captured via hook and sent to the daemon
///
/// This builder is similar to Capture, but we just require more information up front.
/// For the old setup, we could just rely on `History::new` to read some of the missing
/// data. This is no longer the case.
#[derive(Debug, Clone, TypedBuilder)]
pub(crate) struct HistoryDaemonCapture {
    timestamp: time::OffsetDateTime,
    #[builder(setter(into))]
    command: String,
    #[builder(setter(into))]
    cwd: String,
    #[builder(setter(into))]
    session: String,
    #[builder(setter(into))]
    hostname: String,
    #[builder(default, setter(strip_option, into))]
    author: Option<String>,
    #[builder(default, setter(strip_option, into))]
    intent: Option<String>,
}

impl From<HistoryDaemonCapture> for History {
    fn from(captured: HistoryDaemonCapture) -> Self {
        Self::new(
            captured.timestamp,
            captured.command,
            captured.cwd,
            -1,
            -1,
            Some(captured.session),
            Some(captured.hostname),
            captured.author,
            captured.intent,
            None,
        )
    }
}