pylon-runtime 0.3.23

Pylon — realtime backend as a single Rust binary. Schema, policies, server functions, live queries, auth — one process.
Documentation
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
//! RESP (Redis Serialization Protocol) parser and serializer.
//!
//! Implements RESP2, the wire protocol used by Redis. This allows pylon's
//! cache to be accessed by any standard Redis client library.
//!
//! # Wire format
//!
//! - Simple strings: `+OK\r\n`
//! - Errors:         `-ERR message\r\n`
//! - Integers:       `:1000\r\n`
//! - Bulk strings:   `$5\r\nhello\r\n`  (length-prefixed)
//! - Arrays:         `*2\r\n$3\r\nfoo\r\n$3\r\nbar\r\n`
//! - Null:           `$-1\r\n`

use std::io::BufRead;

/// A RESP value.
#[derive(Debug, Clone, PartialEq)]
pub enum RespValue {
    SimpleString(String),
    Error(String),
    Integer(i64),
    BulkString(Option<String>),    // None = null bulk string
    Array(Option<Vec<RespValue>>), // None = null array
}

impl RespValue {
    /// Serialize to RESP wire format.
    pub fn serialize(&self) -> Vec<u8> {
        match self {
            RespValue::SimpleString(s) => format!("+{s}\r\n").into_bytes(),
            RespValue::Error(s) => format!("-{s}\r\n").into_bytes(),
            RespValue::Integer(n) => format!(":{n}\r\n").into_bytes(),
            RespValue::BulkString(None) => b"$-1\r\n".to_vec(),
            RespValue::BulkString(Some(s)) => {
                let mut buf = format!("${}\r\n", s.len()).into_bytes();
                buf.extend_from_slice(s.as_bytes());
                buf.extend_from_slice(b"\r\n");
                buf
            }
            RespValue::Array(None) => b"*-1\r\n".to_vec(),
            RespValue::Array(Some(items)) => {
                let mut buf = format!("*{}\r\n", items.len()).into_bytes();
                for item in items {
                    buf.extend_from_slice(&item.serialize());
                }
                buf
            }
        }
    }

    /// Create a bulk string value.
    pub fn bulk(s: &str) -> Self {
        RespValue::BulkString(Some(s.to_string()))
    }

    /// Create a null bulk string.
    pub fn null() -> Self {
        RespValue::BulkString(None)
    }

    /// Create a simple string "OK".
    pub fn ok() -> Self {
        RespValue::SimpleString("OK".to_string())
    }

    /// Create an integer value.
    pub fn int(n: i64) -> Self {
        RespValue::Integer(n)
    }

    /// Create an error value with the standard "ERR" prefix.
    pub fn err(msg: &str) -> Self {
        RespValue::Error(format!("ERR {msg}"))
    }

    /// Create an array value.
    pub fn array(items: Vec<RespValue>) -> Self {
        RespValue::Array(Some(items))
    }
}

/// Parse a single RESP value from a buffered reader.
///
/// Returns `Err` on I/O errors, malformed input, or EOF.
pub fn parse_resp<R: BufRead>(reader: &mut R) -> Result<RespValue, String> {
    let mut line = String::new();
    let bytes_read = reader
        .read_line(&mut line)
        .map_err(|e| format!("Read error: {e}"))?;

    if bytes_read == 0 {
        return Err("Connection closed".into());
    }

    // Must have at least type byte + \r\n.
    if line.len() < 3 || !line.ends_with("\r\n") {
        return Err(format!("Malformed RESP line: {:?}", line));
    }

    let content = &line[1..line.len() - 2]; // strip type byte and \r\n

    match line.as_bytes()[0] {
        b'+' => Ok(RespValue::SimpleString(content.to_string())),
        b'-' => Ok(RespValue::Error(content.to_string())),
        b':' => {
            let n: i64 = content
                .parse()
                .map_err(|_| format!("Invalid integer: {content:?}"))?;
            Ok(RespValue::Integer(n))
        }
        b'$' => {
            let len: i64 = content
                .parse()
                .map_err(|_| format!("Invalid bulk length: {content:?}"))?;
            if len < 0 {
                return Ok(RespValue::BulkString(None));
            }
            let len = len as usize;
            let mut buf = vec![0u8; len + 2]; // data + trailing \r\n
            reader
                .read_exact(&mut buf)
                .map_err(|e| format!("Read error: {e}"))?;
            if buf[len] != b'\r' || buf[len + 1] != b'\n' {
                return Err("Missing \\r\\n after bulk string data".into());
            }
            let s = String::from_utf8(buf[..len].to_vec())
                .map_err(|_| "Invalid UTF-8 in bulk string")?;
            Ok(RespValue::BulkString(Some(s)))
        }
        b'*' => {
            let count: i64 = content
                .parse()
                .map_err(|_| format!("Invalid array length: {content:?}"))?;
            if count < 0 {
                return Ok(RespValue::Array(None));
            }
            let mut items = Vec::with_capacity(count as usize);
            for _ in 0..count {
                items.push(parse_resp(reader)?);
            }
            Ok(RespValue::Array(Some(items)))
        }
        other => Err(format!("Unknown RESP type byte: {:?}", other as char)),
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    /// Helper: parse a RESP value from raw bytes.
    fn parse(input: &[u8]) -> Result<RespValue, String> {
        let mut reader = BufReader::new(input);
        parse_resp(&mut reader)
    }

    /// Helper: serialize then re-parse, asserting roundtrip equality.
    fn roundtrip(value: &RespValue) {
        let bytes = value.serialize();
        let parsed = parse(&bytes).expect("roundtrip parse failed");
        assert_eq!(&parsed, value, "roundtrip mismatch");
    }

    // -- Simple strings --

    #[test]
    fn parse_simple_string() {
        let val = parse(b"+OK\r\n").unwrap();
        assert_eq!(val, RespValue::SimpleString("OK".into()));
    }

    #[test]
    fn serialize_simple_string() {
        let val = RespValue::SimpleString("hello world".into());
        assert_eq!(val.serialize(), b"+hello world\r\n");
    }

    #[test]
    fn roundtrip_simple_string() {
        roundtrip(&RespValue::SimpleString("PONG".into()));
        roundtrip(&RespValue::ok());
    }

    // -- Errors --

    #[test]
    fn parse_error() {
        let val = parse(b"-ERR unknown command\r\n").unwrap();
        assert_eq!(val, RespValue::Error("ERR unknown command".into()));
    }

    #[test]
    fn serialize_error() {
        let val = RespValue::err("bad key");
        assert_eq!(val.serialize(), b"-ERR bad key\r\n");
    }

    #[test]
    fn roundtrip_error() {
        roundtrip(&RespValue::err("something went wrong"));
    }

    // -- Integers --

    #[test]
    fn parse_integer() {
        assert_eq!(parse(b":1000\r\n").unwrap(), RespValue::Integer(1000));
        assert_eq!(parse(b":-42\r\n").unwrap(), RespValue::Integer(-42));
        assert_eq!(parse(b":0\r\n").unwrap(), RespValue::Integer(0));
    }

    #[test]
    fn serialize_integer() {
        assert_eq!(RespValue::int(99).serialize(), b":99\r\n");
        assert_eq!(RespValue::int(-1).serialize(), b":-1\r\n");
    }

    #[test]
    fn roundtrip_integer() {
        roundtrip(&RespValue::int(0));
        roundtrip(&RespValue::int(i64::MAX));
        roundtrip(&RespValue::int(i64::MIN));
    }

    // -- Bulk strings --

    #[test]
    fn parse_bulk_string() {
        let val = parse(b"$5\r\nhello\r\n").unwrap();
        assert_eq!(val, RespValue::BulkString(Some("hello".into())));
    }

    #[test]
    fn parse_null_bulk_string() {
        let val = parse(b"$-1\r\n").unwrap();
        assert_eq!(val, RespValue::BulkString(None));
    }

    #[test]
    fn parse_empty_bulk_string() {
        let val = parse(b"$0\r\n\r\n").unwrap();
        assert_eq!(val, RespValue::BulkString(Some(String::new())));
    }

    #[test]
    fn serialize_bulk_string() {
        assert_eq!(RespValue::bulk("foo").serialize(), b"$3\r\nfoo\r\n");
    }

    #[test]
    fn serialize_null_bulk_string() {
        assert_eq!(RespValue::null().serialize(), b"$-1\r\n");
    }

    #[test]
    fn serialize_empty_bulk_string() {
        assert_eq!(
            RespValue::BulkString(Some(String::new())).serialize(),
            b"$0\r\n\r\n"
        );
    }

    #[test]
    fn roundtrip_bulk_string() {
        roundtrip(&RespValue::bulk("hello"));
        roundtrip(&RespValue::null());
        roundtrip(&RespValue::BulkString(Some(String::new())));
    }

    #[test]
    fn large_bulk_string() {
        let large = "x".repeat(100_000);
        let val = RespValue::bulk(&large);
        roundtrip(&val);
    }

    // -- Arrays --

    #[test]
    fn parse_array() {
        let input = b"*2\r\n$3\r\nfoo\r\n$3\r\nbar\r\n";
        let val = parse(input).unwrap();
        assert_eq!(
            val,
            RespValue::Array(Some(vec![RespValue::bulk("foo"), RespValue::bulk("bar"),]))
        );
    }

    #[test]
    fn parse_null_array() {
        let val = parse(b"*-1\r\n").unwrap();
        assert_eq!(val, RespValue::Array(None));
    }

    #[test]
    fn parse_empty_array() {
        let val = parse(b"*0\r\n").unwrap();
        assert_eq!(val, RespValue::Array(Some(vec![])));
    }

    #[test]
    fn serialize_array() {
        let val = RespValue::array(vec![RespValue::bulk("a"), RespValue::int(1)]);
        let bytes = val.serialize();
        assert_eq!(bytes, b"*2\r\n$1\r\na\r\n:1\r\n");
    }

    #[test]
    fn serialize_null_array() {
        assert_eq!(RespValue::Array(None).serialize(), b"*-1\r\n");
    }

    #[test]
    fn roundtrip_array() {
        roundtrip(&RespValue::array(vec![
            RespValue::bulk("SET"),
            RespValue::bulk("key"),
            RespValue::bulk("value"),
        ]));
        roundtrip(&RespValue::Array(None));
        roundtrip(&RespValue::array(vec![]));
    }

    #[test]
    fn nested_arrays() {
        let inner = RespValue::array(vec![RespValue::int(1), RespValue::int(2)]);
        let outer = RespValue::array(vec![inner.clone(), RespValue::bulk("end")]);
        roundtrip(&outer);
    }

    #[test]
    fn deeply_nested_arrays() {
        let mut val = RespValue::int(42);
        for _ in 0..10 {
            val = RespValue::array(vec![val]);
        }
        roundtrip(&val);
    }

    // -- Mixed types in arrays --

    #[test]
    fn mixed_type_array() {
        let val = RespValue::array(vec![
            RespValue::SimpleString("OK".into()),
            RespValue::err("bad"),
            RespValue::int(42),
            RespValue::bulk("hello"),
            RespValue::null(),
        ]);
        roundtrip(&val);
    }

    // -- Error cases --

    #[test]
    fn empty_input() {
        assert!(parse(b"").is_err());
    }

    #[test]
    fn malformed_line() {
        assert!(parse(b"x\r\n").is_err());
    }

    #[test]
    fn invalid_integer() {
        assert!(parse(b":notanumber\r\n").is_err());
    }

    #[test]
    fn truncated_bulk_string() {
        // Says length 10 but only provides 3 bytes.
        assert!(parse(b"$10\r\nfoo\r\n").is_err());
    }

    // -- Helpers --

    #[test]
    fn helper_constructors() {
        assert_eq!(RespValue::ok(), RespValue::SimpleString("OK".into()));
        assert_eq!(RespValue::null(), RespValue::BulkString(None));
        assert_eq!(RespValue::int(5), RespValue::Integer(5));
        assert_eq!(RespValue::err("fail"), RespValue::Error("ERR fail".into()));
        assert_eq!(
            RespValue::array(vec![RespValue::int(1)]),
            RespValue::Array(Some(vec![RespValue::Integer(1)]))
        );
    }

    // -- Multiple values in sequence --

    #[test]
    fn parse_multiple_values_from_stream() {
        let input = b"+OK\r\n:42\r\n$5\r\nhello\r\n";
        let mut reader = BufReader::new(&input[..]);

        let v1 = parse_resp(&mut reader).unwrap();
        assert_eq!(v1, RespValue::SimpleString("OK".into()));

        let v2 = parse_resp(&mut reader).unwrap();
        assert_eq!(v2, RespValue::Integer(42));

        let v3 = parse_resp(&mut reader).unwrap();
        assert_eq!(v3, RespValue::bulk("hello"));
    }
}