aboutsummaryrefslogtreecommitdiffstats
path: root/crates/atuin-scripts/src/store/script.rs
blob: af1803208443814936cfef9bdfbc53facafa978d (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
use atuin_common::record::DecryptedData;
use eyre::{Result, bail, ensure};
use uuid::Uuid;

use rmp::{
    decode::{self, Bytes},
    encode,
};
use typed_builder::TypedBuilder;

pub const SCRIPT_VERSION: &str = "v0";
pub const SCRIPT_TAG: &str = "script";
pub const SCRIPT_LEN: usize = 20000; // 20kb max total len

#[derive(Debug, Clone, PartialEq, Eq, TypedBuilder)]
/// A script is a set of commands that can be run, with the specified shebang
pub struct Script {
    /// The id of the script
    #[builder(default = uuid::Uuid::new_v4())]
    pub id: Uuid,

    /// The name of the script
    pub name: String,

    /// The description of the script
    #[builder(default = String::new())]
    pub description: String,

    /// The interpreter of the script
    #[builder(default = String::new())]
    pub shebang: String,

    /// The tags of the script
    #[builder(default = Vec::new())]
    pub tags: Vec<String>,

    /// The script content
    pub script: String,
}

impl Script {
    pub fn serialize(&self) -> Result<DecryptedData> {
        // sort the tags first, to ensure consistent ordering
        let mut tags = self.tags.clone();
        tags.sort();

        let mut output = vec![];

        encode::write_array_len(&mut output, 6)?;
        encode::write_str(&mut output, &self.id.to_string())?;
        encode::write_str(&mut output, &self.name)?;
        encode::write_str(&mut output, &self.description)?;
        encode::write_str(&mut output, &self.shebang)?;
        encode::write_array_len(&mut output, self.tags.len() as u32)?;

        for tag in &tags {
            encode::write_str(&mut output, tag)?;
        }

        encode::write_str(&mut output, &self.script)?;

        Ok(DecryptedData(output))
    }

    pub fn deserialize(bytes: &[u8]) -> Result<Self> {
        let mut bytes = decode::Bytes::new(bytes);
        let nfields = decode::read_array_len(&mut bytes).unwrap();

        ensure!(nfields == 6, "too many entries in v0 script record");

        let bytes = bytes.remaining_slice();

        let (id, bytes) = decode::read_str_from_slice(bytes).unwrap();
        let (name, bytes) = decode::read_str_from_slice(bytes).unwrap();
        let (description, bytes) = decode::read_str_from_slice(bytes).unwrap();
        let (shebang, bytes) = decode::read_str_from_slice(bytes).unwrap();

        let mut bytes = Bytes::new(bytes);
        let tags_len = decode::read_array_len(&mut bytes).unwrap();

        let mut bytes = bytes.remaining_slice();

        let mut tags = Vec::new();
        for _ in 0..tags_len {
            let (tag, remaining) = decode::read_str_from_slice(bytes).unwrap();
            tags.push(tag.to_owned());
            bytes = remaining;
        }

        let (script, bytes) = decode::read_str_from_slice(bytes).unwrap();

        if !bytes.is_empty() {
            bail!("trailing bytes in encoded script record. malformed")
        }

        Ok(Script {
            id: Uuid::parse_str(id).unwrap(),
            name: name.to_owned(),
            description: description.to_owned(),
            shebang: shebang.to_owned(),
            tags,
            script: script.to_owned(),
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_serialize() {
        let script = Script {
            id: uuid::Uuid::parse_str("0195c825a35f7982bdb016168881cbc6").unwrap(),
            name: "test".to_string(),
            description: "test".to_string(),
            shebang: "test".to_string(),
            tags: vec!["test".to_string()],
            script: "test".to_string(),
        };

        let serialized = script.serialize().unwrap();
        assert_eq!(
            serialized.0,
            vec![
                150, 217, 36, 48, 49, 57, 53, 99, 56, 50, 53, 45, 97, 51, 53, 102, 45, 55, 57, 56,
                50, 45, 98, 100, 98, 48, 45, 49, 54, 49, 54, 56, 56, 56, 49, 99, 98, 99, 54, 164,
                116, 101, 115, 116, 164, 116, 101, 115, 116, 164, 116, 101, 115, 116, 145, 164,
                116, 101, 115, 116, 164, 116, 101, 115, 116
            ]
        );
    }

    #[test]
    fn test_serialize_deserialize() {
        let script = Script {
            id: uuid::Uuid::new_v4(),
            name: "test".to_string(),
            description: "test".to_string(),
            shebang: "test".to_string(),
            tags: vec!["test".to_string()],
            script: "test".to_string(),
        };

        let serialized = script.serialize().unwrap();

        let deserialized = Script::deserialize(&serialized.0).unwrap();

        assert_eq!(script, deserialized);
    }
}