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
//! Base traits for different implementations of JSONPath execution engines.
//!
//! Defines the [`Runner`] trait that provides different ways of retrieving
//! query results from input bytes. Result types are defined in the [result]
//! module.

pub mod result;

use aligners::{
    alignment::{self},
    AlignedBytes,
};
use cfg_if::cfg_if;

use self::result::QueryResult;

/// Input into a query engine.
pub struct Input {
    bytes: AlignedBytes<alignment::Page>,
}

impl std::ops::Deref for Input {
    type Target = AlignedBytes<alignment::Page>;

    #[inline(always)]
    fn deref(&self) -> &Self::Target {
        &self.bytes
    }
}

impl Input {
    /// Transmute a buffer into an input.
    ///
    /// The buffer must know its length, may be extended by auxiliary UTF8 characters
    /// and will be interpreted as a slice of bytes at the end.
    #[inline]
    pub fn new<T: Extend<char> + AsRef<[u8]>>(src: T) -> Self {
        cfg_if! {
            if #[cfg(feature = "simd")] {
                use aligners::alignment::Alignment;
                type A = alignment::Twice::<crate::BlockAlignment>;
                let mut contents = src;
                let rem = contents.as_ref().len() % A::size();
                let pad = if rem == 0 {
                    0
                } else {
                    A::size() - rem
                };

                let extension = std::iter::repeat('\0').take(pad + A::size());
                contents.extend(extension);

                debug_assert_eq!(contents.as_ref().len() % A::size(), 0);

                Self {
                    bytes: AlignedBytes::<alignment::Page>::from(contents.as_ref()),
                }
            }
            else {
                Self {
                    bytes: AlignedBytes::<alignment::Page>::from(src.as_ref()),
                }
            }
        }
    }

    /// Transmute a buffer into an input.
    ///
    /// The buffer must know its length, may be extended by auxiliary bytes.
    #[inline]
    pub fn new_bytes<T: Extend<u8> + AsRef<[u8]>>(src: T) -> Self {
        cfg_if! {
            if #[cfg(feature = "simd")] {
                use aligners::alignment::Alignment;
                type A = alignment::Twice::<crate::BlockAlignment>;
                let mut contents = src;
                let rem = contents.as_ref().len() % A::size();
                let pad = if rem == 0 {
                    0
                } else {
                    A::size() - rem
                };

                let extension = std::iter::repeat(0).take(pad + A::size());
                contents.extend(extension);

                debug_assert_eq!(contents.as_ref().len() % A::size(), 0);

                Self {
                    bytes: AlignedBytes::<alignment::Page>::from(contents.as_ref()),
                }
            }
            else {
                Self {
                    bytes: AlignedBytes::<alignment::Page>::from(src.as_ref()),
                }
            }
        }
    }
}

/// Trait for an engine that can run its query on a given input.
pub trait Runner {
    /// Compute the [`QueryResult`] on given [`Input`].
    fn run<R: QueryResult>(&self, input: &Input) -> R;
}