Skip to main content

cartel_core/sql/
mod.rs

1use core::{convert, marker, str};
2use sealed::Sealed;
3
4pub mod raw;
5mod sealed;
6pub mod tokens;
7
8/// The exact field type carried only by generated, uninhabited query checks.
9pub struct Target<T>(convert::Infallible, marker::PhantomData<fn(T) -> T>);
10
11/// Keeps ordinary Rust assignment coercions for every non-text field.
12pub trait Assign<T>: Sealed<Target<T>> {
13    fn assign(self, value: T);
14}
15
16impl<T> Assign<T> for Target<T> {
17    fn assign(self, _: T) {
18        match self.0 {}
19    }
20}
21
22impl Assign<&str> for Target<String> {
23    fn assign(self, _: &str) {
24        match self.0 {}
25    }
26}
27
28impl<T> Sealed<Target<T>> for Target<T> {}
29impl Sealed<Target<&str>> for Target<String> {}
30
31/// SQL text assembled from complete UTF-8 fragments.
32pub struct Text<const N: usize> {
33    bytes: [u8; N],
34}
35
36impl<const N: usize> Text<N> {
37    const fn new(bytes: [u8; N]) -> Self {
38        Self { bytes }
39    }
40
41    pub const fn as_str(&self) -> &str {
42        // SAFETY: `Parts::concat` copies complete `str` fragments, and the
43        // private constructor is its only source.
44        unsafe { str::from_utf8_unchecked(&self.bytes) }
45    }
46}