passdata 0.0.2

Authentication and authorization data in a logic programming language.
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
414
415
//! Values are stored in a `Context` and are indirectly referenced by various
//! identifier types. Values are immutable.
//!
//! # Limitations
//!
//! Values are not constructed during a program's execution. For instance,
//! strings are not concatenated. Values have to be added explicitly to the
//! context before execution via facts or rules.
//!
//! Due to the limitations on a HTTP cookie/header value's length, there is an
//! absolute limit to the amount of data that can be encoded.

#[cfg(all(feature = "alloc", not(feature = "std")))]
use alloc::borrow::Cow;
use core::fmt;
#[cfg(feature = "std")]
use std::{borrow::Cow, error};

use crate::{
    error::{Error, ErrorKind},
    ConstantTy, Section, SectionMut,
};

const BOOL_FALSE_INDEX: u16 = 0;
const BOOL_TRUE_INDEX: u16 = 1;

const MAX_DATA_LEN: usize = 4096;
const BYTES_TY_BITMASK: u16 = 3 << 13;
const NUM_TY_BITMASK: u16 = 2 << 13;

/// An interned bytes reference.
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) struct BytesId(pub(crate) u16);

/// An interned number reference.
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) struct NumId(pub(crate) u16);

/// A scalar reference.
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) struct ScalarId(u16);

impl From<bool> for ScalarId {
    fn from(value: bool) -> Self {
        if value {
            Self(1)
        } else {
            Self(0)
        }
    }
}

impl From<NumId> for ScalarId {
    fn from(value: NumId) -> Self {
        Self(value.0)
    }
}

impl From<BytesId> for ScalarId {
    fn from(value: BytesId) -> Self {
        Self(value.0)
    }
}

/// A constant reference
#[derive(Debug, Default, Clone, Copy, Hash, PartialEq, Eq)]
pub struct ConstantId(pub(crate) u16);

impl From<bool> for ConstantId {
    fn from(value: bool) -> Self {
        Self(ScalarId::from(value).0)
    }
}

impl From<NumId> for ConstantId {
    fn from(value: NumId) -> Self {
        Self(value.0)
    }
}

impl From<BytesId> for ConstantId {
    fn from(value: BytesId) -> Self {
        Self(value.0)
    }
}

impl From<ScalarId> for ConstantId {
    fn from(value: ScalarId) -> Self {
        Self(value.0)
    }
}

/// A constant value.
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub enum Constant<'a> {
    /// Boolean
    Bool(bool),
    /// Number
    Num(i64),
    /// Byte string
    Bytes(&'a [u8]),
}

impl<'a> Default for Constant<'a> {
    fn default() -> Self {
        Self::Bool(false)
    }
}

impl<'a> From<bool> for Constant<'a> {
    fn from(value: bool) -> Self {
        Constant::Bool(value)
    }
}

impl<'a> From<i64> for Constant<'a> {
    fn from(value: i64) -> Self {
        Constant::Num(value)
    }
}

impl<'a> From<&'a str> for Constant<'a> {
    fn from(value: &'a str) -> Self {
        Constant::Bytes(value.as_bytes())
    }
}

impl<'a> From<&'a [u8]> for Constant<'a> {
    fn from(value: &'a [u8]) -> Self {
        Constant::Bytes(value)
    }
}

/// Cannot convert constant value to type.
#[derive(Clone, Copy)]
pub struct InvalidType;

#[cfg(feature = "std")]
impl error::Error for InvalidType {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        None
    }
}

impl fmt::Display for InvalidType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("invalid type")
    }
}

impl fmt::Debug for InvalidType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("invalid type")
    }
}

impl<'a> TryFrom<Constant<'a>> for &'a [u8] {
    type Error = InvalidType;

    fn try_from(value: Constant<'a>) -> Result<Self, Self::Error> {
        match value {
            Constant::Bool(_) | Constant::Num(_) => Err(InvalidType),
            Constant::Bytes(bytes) => Ok(bytes),
        }
    }
}

impl<'a> TryFrom<Constant<'a>> for Cow<'a, [u8]> {
    type Error = InvalidType;

    fn try_from(value: Constant<'a>) -> Result<Self, Self::Error> {
        match value {
            Constant::Bool(_) | Constant::Num(_) => Err(InvalidType),
            Constant::Bytes(bytes) => Ok(Cow::from(bytes)),
        }
    }
}

impl<'a> TryFrom<Constant<'a>> for &'a str {
    type Error = InvalidType;

    fn try_from(value: Constant<'a>) -> Result<Self, Self::Error> {
        match value {
            Constant::Bool(_) | Constant::Num(_) => Err(InvalidType),
            Constant::Bytes(bytes) => Ok(core::str::from_utf8(bytes).map_err(|_| InvalidType)?),
        }
    }
}

impl<'a> TryFrom<Constant<'a>> for Cow<'a, str> {
    type Error = InvalidType;

    fn try_from(value: Constant<'a>) -> Result<Self, Self::Error> {
        match value {
            Constant::Bool(_) | Constant::Num(_) => Err(InvalidType),
            Constant::Bytes(bytes) => Ok(Cow::Borrowed(
                core::str::from_utf8(bytes).map_err(|_| InvalidType)?,
            )),
        }
    }
}

impl<'a> TryFrom<Constant<'a>> for i64 {
    type Error = InvalidType;

    fn try_from(value: Constant<'a>) -> Result<Self, Self::Error> {
        match value {
            Constant::Bool(_) | Constant::Bytes(_) => Err(InvalidType),
            Constant::Num(n) => Ok(n),
        }
    }
}

impl<'a> TryFrom<Constant<'a>> for bool {
    type Error = InvalidType;

    fn try_from(value: Constant<'a>) -> Result<Self, Self::Error> {
        match value {
            Constant::Bool(b) => Ok(b),
            Constant::Num(_) | Constant::Bytes(_) => Err(InvalidType),
        }
    }
}

#[derive(Debug)]
pub(crate) struct Iter<'a> {
    data: &'a [u8],
}

fn iter<'a>(ctx: &Section<'a>) -> Iter<'a> {
    let data = ctx.data();
    if data.is_empty() {
        return Iter { data: &[] };
    }
    Iter { data: &data[2..] }
}

impl<'a> Iterator for Iter<'a> {
    type Item = (ConstantTy, &'a [u8]);

    fn next(&mut self) -> Option<Self::Item> {
        if self.data.is_empty() {
            return None;
        }

        let first_len_byte = self.data[0];
        let ty = match first_len_byte >> 5 {
            2 => ConstantTy::Num,
            3 => ConstantTy::Bytes,
            _ => unreachable!(),
        };

        let first_len_byte = first_len_byte & 0x1F;
        let len = u16::from_be_bytes([first_len_byte, self.data[1]]);

        let len = usize::from(len);
        let slice = &self.data[2..2 + len];
        self.data = &self.data[2 + len..];

        Some((ty, slice))
    }
}

const SECTION_INIT: [u8; 5] = [1, 0, 2, 0, 0];

#[must_use]
#[inline]
fn elem_len(section: &SectionMut<'_>) -> u16 {
    u16::from_be_bytes([
        section.data[section.start + 3],
        section.data[section.start + 4],
    ])
}

#[inline]
fn set_elem_len(section: &mut SectionMut<'_>, len: u16) {
    let len_bytes = len.to_be_bytes();
    section.data[section.start + 3] = len_bytes[0];
    section.data[section.start + 4] = len_bytes[1];
}

#[must_use]
pub(crate) fn bytes_id(ctx: &Section<'_>, needle: &[u8]) -> Option<BytesId> {
    iter(ctx)
        .position(|(ty, value)| ty == ConstantTy::Bytes && value == needle)
        .map(|pos| {
            BytesId(2 + u16::try_from(pos).expect("greater than u16::MAX byte slices in context"))
        })
}

/// Returns the slice of bytes given the [`BytesId`].
#[must_use]
pub(crate) fn bytes<'a>(ctx: &Section<'a>, id: BytesId) -> &'a [u8] {
    let idx = usize::from(id.0 - 2);
    let Some((ty, value)) = iter(ctx).nth(idx) else {
        unreachable!()
    };
    assert_eq!(ty, ConstantTy::Bytes);
    value
}

/// Given a slice of bytes, returns a [`BytesId`].
///
/// If the slice of bytes value already exists in the context, it will return the
/// existing assigned [`BytesId`]. If the slice of bytes value does not exist in
/// the context, a unique [`BytesId`] will be assigned and returned.
pub(crate) fn get_or_insert_bytes_id(
    ctx: &mut SectionMut<'_>,
    needle: &[u8],
) -> Result<BytesId, Error> {
    if needle.len() > MAX_DATA_LEN {
        return Err(Error::with_kind(ErrorKind::InvalidContextValue));
    }

    if let Some(bytes_id) = bytes_id(&ctx.as_ref(), needle) {
        Ok(bytes_id)
    } else {
        ctx.init(&SECTION_INIT);

        let len = elem_len(ctx);
        if len >= u16::MAX - 2 {
            return Err(Error::with_kind(ErrorKind::ContextFull));
        }
        let len = len + 1;

        let needle_len = needle.len();
        assert!(needle_len <= 4096);
        let needle_len_with_ty = u16::try_from(needle_len).unwrap() | BYTES_TY_BITMASK;
        ctx.splice(needle_len_with_ty.to_be_bytes());
        ctx.splice(needle.iter().copied());

        set_elem_len(ctx, len);
        ctx.update_len();

        Ok(BytesId(2 + len - 1))
    }
}

/// Given a `i64` value, returns the existing [`NumId`], if the `i64` exists in the context.
#[must_use]
fn num_id(ctx: &Section<'_>, needle: i64) -> Option<NumId> {
    iter(ctx)
        .position(|(ty, value)| {
            ty == ConstantTy::Num
                && i64::from_be_bytes(<[u8; 8]>::try_from(value).unwrap()) == needle
        })
        .map(|pos| NumId(2 + u16::try_from(pos).expect("greater than u16::MAX numbers in context")))
}

/// Returns the number value given the [`NumId`].
#[must_use]
#[allow(unused)]
fn num(ctx: &Section<'_>, id: NumId) -> i64 {
    let idx = usize::from(id.0 - 2);
    let Some((ty, value)) = iter(ctx).nth(idx) else {
        unreachable!()
    };
    assert_eq!(ty, ConstantTy::Num);
    i64::from_be_bytes(<[u8; 8]>::try_from(value).unwrap())
}

/// Given a number, returns a [`NumId`].
///
/// If the number value already exists in the context, it will return the
/// existing assigned [`NumId`]. If the number value does not exist in
/// the context, a unique [`NumId`] will be assigned and returned.
pub(crate) fn get_or_insert_num_id(ctx: &mut SectionMut<'_>, needle: i64) -> Result<NumId, Error> {
    if let Some(id) = num_id(&ctx.as_ref(), needle) {
        Ok(id)
    } else {
        ctx.init(&SECTION_INIT);

        let len = elem_len(ctx);
        if len >= u16::MAX - 2 {
            return Err(Error::with_kind(ErrorKind::ContextFull));
        }
        let len = len + 1;

        let needle_len = 8u16;
        let needle_len = needle_len | NUM_TY_BITMASK;
        ctx.splice(needle_len.to_be_bytes());
        ctx.splice(needle.to_be_bytes());

        set_elem_len(ctx, len);
        ctx.update_len();

        Ok(NumId(2 + len - 1))
    }
}

/// Returns the value given the context.
pub(crate) fn constant<'a, T>(ctx: &Section<'a>, id: T) -> Constant<'a>
where
    ConstantId: From<T>,
{
    let id = ConstantId::from(id);

    if id.0 == BOOL_FALSE_INDEX {
        return Constant::Bool(false);
    }

    if id.0 == BOOL_TRUE_INDEX {
        return Constant::Bool(true);
    }

    let Some((ty, value)) = iter(ctx).nth(usize::from(id.0 - 2)) else {
        unreachable!()
    };

    match ty {
        ConstantTy::Unknown | ConstantTy::Bool => unreachable!(),
        ConstantTy::Num => Constant::Num(i64::from_be_bytes(<[u8; 8]>::try_from(value).unwrap())),
        ConstantTy::Bytes => Constant::Bytes(value),
    }
}