sqll 0.13.4

Efficient interface to SQLite that doesn't get in your way
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
use core::fmt;
use core::hash::{Hash, Hasher};
use core::ops::Deref;
use core::str;

use crate::{CapacityError, FixedBlob, Text};

/// A [`Text`] type which can store at most `N` bytes from a column.
///
/// The data is stored inline the type which typically means on the stack.
///
/// # Examples
///
/// ```
/// use sqll::{Connection, FixedText, Result};
///
/// let c = Connection::open_in_memory()?;
///
/// c.execute(r#"
///     CREATE TABLE users (name TEXT);
///
///     INSERT INTO users (name) VALUES ('Alice'), ('Bob');
/// "#)?;
///
/// let mut stmt = c.prepare("SELECT name FROM users")?;
///
/// let ids = stmt.iter::<FixedText<10>>().collect::<Result<Vec<_>>>()?;
/// assert_eq!(&ids[0], "Alice");
/// assert_eq!(&ids[1], "Bob");
/// # Ok::<_, sqll::Error>(())
/// ```
pub struct FixedText<const N: usize> {
    inner: FixedBlob<N>,
}

impl<const N: usize> FixedText<N> {
    /// Construct a new empty [`FixedText`].
    ///
    /// # Examples
    ///
    /// ```
    /// use sqll::FixedText;
    ///
    /// let s = FixedText::<5>::new();
    /// assert_eq!(s.as_text(), "");
    /// ```
    pub const fn new() -> Self {
        Self {
            inner: FixedBlob::new(),
        }
    }

    /// Converts a vector of bytes to a String without checking that the string
    /// contains valid UTF-8.
    ///
    /// # Examples
    ///
    /// ```
    /// use sqll::{FixedBlob, FixedText};
    ///
    /// let bytes = FixedBlob::<16>::try_from(&b"Hello World"[..])?;
    /// let s = unsafe { FixedText::from_inner(bytes) };
    /// assert_eq!(s.as_text(), "Hello World");
    /// # Ok::<_, sqll::CapacityError>(())
    /// ```
    pub const fn from_inner(inner: FixedBlob<N>) -> Self {
        Self { inner }
    }

    /// Coerce into the initialized string slice.
    ///
    /// # Examples
    ///
    /// ```
    /// use sqll::{Connection, FixedText};
    ///
    /// let c = Connection::open_in_memory()?;
    ///
    /// c.execute(r#"
    ///     CREATE TABLE users (name BLOB);
    ///
    ///     INSERT INTO users (name) VALUES ('Alice'), ('Bob');
    /// "#)?;
    ///
    /// let mut stmt = c.prepare("SELECT name FROM users")?;
    ///
    /// assert_eq! {
    ///     stmt.iter::<FixedText<6>>().collect::<Vec<_>>(),
    ///     [Ok(FixedText::<6>::try_from("Alice")?), Ok(FixedText::<6>::try_from("Bob")?)]
    /// };
    /// # Ok::<_, Box<dyn core::error::Error>>(())
    /// ```
    pub fn as_text(&self) -> &Text {
        Text::new(self.inner.as_slice())
    }
}

/// Deref to `Text`.
///
/// # Examples
///
/// ```
/// use sqll::FixedText;
///
/// let ft = FixedText::from(*b"invalid: \xF0\x90\x80\xF0\x90\x80");
/// assert_eq!(ft.as_bytes(), b"invalid: \xF0\x90\x80\xF0\x90\x80");
/// assert_eq!(ft.to_string(), "invalid: ��");
/// ```
impl<const N: usize> Deref for FixedText<N> {
    type Target = Text;

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

/// Format as `Text`.
///
/// # Examples
///
/// ```
/// use sqll::FixedText;
///
/// let ft = FixedText::<5>::try_from("Hello")?;
/// assert_eq!(format!("{:?}", ft), "\"Hello\"");
/// assert_eq!(format!("{}", ft), "Hello");
/// # Ok::<_, sqll::CapacityError>(())
/// ```
impl<const N: usize> fmt::Debug for FixedText<N> {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.as_text().fmt(f)
    }
}

/// The display implementation for `Text` will convert it into a UTF-8 string
/// lossily, replacing invalid sequences with the replacement character `�`.
///
/// # Examples
///
/// ```
/// use sqll::FixedText;
///
/// let text = FixedText::from(b"before\xF0\x90\x80after");
/// assert_eq!(text.to_string(), "before�after");
///
/// let text = FixedText::from(b"before\xF0\x90\x80\xF0\x90\x80");
/// assert_eq!(text.to_string(), "before��");
/// # Ok::<_, sqll::CapacityError>(())
/// ```
impl<const N: usize> fmt::Display for FixedText<N> {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.as_text().fmt(f)
    }
}

/// Coerce into [`Text`].
///
/// # Examples
///
/// ```
/// use sqll::{FixedText, Text};
///
/// let text = FixedText::from(*b"example");
/// let text: &Text = text.as_ref();
/// assert_eq!(text, "example");
/// ```
impl<const N: usize> AsRef<Text> for FixedText<N> {
    #[inline]
    fn as_ref(&self) -> &Text {
        self.as_text()
    }
}

/// Compare the text for equality with another `Text`. This performs a byte-wise
/// comparison.
///
/// # Examples
///
/// ```
/// use sqll::FixedText;
///
/// let t1 = FixedText::from(*b"example");
/// let t2 = FixedText::from(*b"example");
/// let t3 = FixedText::from(*b"different");
///
/// assert_eq!(t1, t2);
/// assert_ne!(t1, t3);
/// ```
impl<const N: usize, const U: usize> PartialEq<FixedText<U>> for FixedText<N> {
    #[inline]
    fn eq(&self, other: &FixedText<U>) -> bool {
        self.as_text() == other.as_text()
    }
}

impl<const N: usize> Eq for FixedText<N> {}

/// Compare the text for equality with a `str`. This performs a byte-wise
/// comparison.
///
/// # Examples
///
/// ```
/// use sqll::FixedText;
///
/// let t1 = FixedText::from(*b"example");
/// let t2 = "example";
/// let t3 = "different";
///
/// assert_eq!(t1, *t2);
/// assert_ne!(t1, *t3);
/// ```
impl<const N: usize> PartialEq<str> for FixedText<N> {
    #[inline]
    fn eq(&self, other: &str) -> bool {
        self.as_text() == other
    }
}

/// Compare the text for equality with a `Text`. This performs a byte-wise
/// comparison.
///
/// # Examples
///
/// ```
/// use sqll::{FixedText, Text};
///
/// let t1 = FixedText::from(*b"example");
/// let t2 = Text::new("example");
/// let t3 = Text::new("different");
///
/// assert_eq!(t1, *t2);
/// assert_ne!(t1, *t3);
/// ```
impl<const N: usize> PartialEq<Text> for FixedText<N> {
    #[inline]
    fn eq(&self, other: &Text) -> bool {
        self.as_text() == other
    }
}

/// Compare for ordering.
///
/// # Examples
///
/// ```
/// use sqll::FixedText;
///
/// let a = FixedText::<16>::try_from("Apple")?;
/// let b = FixedText::<16>::try_from("Banana")?;
///
/// assert!(a < b);
/// assert!(b > a);
/// # Ok::<_, sqll::CapacityError>(())
/// ```
impl<const N: usize> PartialOrd for FixedText<N> {
    #[inline]
    fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

/// Compare for ordering.
///
/// # Examples
///
/// ```
/// use sqll::FixedText;
/// use std::collections::BTreeSet;
///
/// let a = FixedText::<16>::try_from("Apple")?;
/// let b = FixedText::<16>::try_from("Banana")?;
///
/// let set = BTreeSet::from([a, b]);
/// # Ok::<_, sqll::CapacityError>(())
/// ```
impl<const N: usize> Ord for FixedText<N> {
    #[inline]
    fn cmp(&self, other: &Self) -> core::cmp::Ordering {
        self.as_text().cmp(other.as_text())
    }
}

/// Hash the `FixedText<N>`.
///
/// # Examples
///
/// ```
/// use sqll::FixedText;
/// use std::collections::HashSet;
///
/// let a = FixedText::<16>::try_from("Apple")?;
/// let b = FixedText::<16>::try_from("Banana")?;
///
/// let mut set = HashSet::from([a, b]);
///
/// let c = FixedText::<16>::try_from("Banana")?;
/// assert!(set.contains(&c));
/// assert!(!set.insert(c));
/// # Ok::<_, sqll::CapacityError>(())
/// ```
impl<const N: usize> Hash for FixedText<N> {
    #[inline]
    fn hash<H>(&self, state: &mut H)
    where
        H: Hasher,
    {
        self.as_text().hash(state)
    }
}

/// Clone the `FixedText<N>`.
///
/// # Examples
///
/// ```
/// use sqll::FixedText;
///
/// let ft1 = FixedText::<5>::try_from("Hello")?;
/// let ft2 = ft1.clone();
/// assert_eq!(ft1, ft2);
/// # Ok::<_, sqll::CapacityError>(())
/// ```
impl<const N: usize> Clone for FixedText<N> {
    #[inline]
    fn clone(&self) -> Self {
        Self {
            inner: self.inner.clone(),
        }
    }
}

/// Attempt to convert a string slice into a `FixedText<N>`.
///
/// # Examples
///
/// ```
/// use sqll::FixedText;
/// let s = FixedText::<5>::try_from("Hello")?;
/// assert_eq!(s.as_text(), "Hello");
/// # Ok::<_, sqll::CapacityError>(())
/// ```
impl<const N: usize> TryFrom<&str> for FixedText<N> {
    type Error = CapacityError;

    #[inline]
    fn try_from(value: &str) -> Result<Self, Self::Error> {
        Ok(Self::from_inner(FixedBlob::try_from(value.as_bytes())?))
    }
}

/// Attempt to convert a byte slice into a `FixedText<N>`.
///
/// # Examples
///
/// ```
/// use sqll::FixedText;
///
/// let ft = FixedText::<5>::try_from(&b"Hello"[..])?;
/// assert_eq!(ft.as_text(), "Hello");
/// # Ok::<_, sqll::CapacityError>(())
/// ```
impl<const N: usize> TryFrom<&[u8]> for FixedText<N> {
    type Error = CapacityError;

    #[inline]
    fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
        Ok(Self::from_inner(FixedBlob::try_from(value)?))
    }
}

/// Attempt to convert a byte array into a `FixedText<N>`.
///
/// # Examples
///
/// ```
/// use sqll::FixedText;
///
/// let ft = FixedText::from(b"Hello");
/// assert_eq!(ft.as_bytes(), b"Hello");
/// assert_eq!(ft.as_text(), "Hello");
/// ```
impl<const N: usize> From<&[u8; N]> for FixedText<N> {
    #[inline]
    fn from(value: &[u8; N]) -> Self {
        Self::from_inner(FixedBlob::from(value))
    }
}

/// Attempt to convert a byte array into a `FixedText<N>`.
///
/// # Examples
///
/// ```
/// use sqll::FixedText;
///
/// let ft = FixedText::from(*b"Hello");
/// assert_eq!(ft.as_text(), "Hello");
/// ```
impl<const N: usize> From<[u8; N]> for FixedText<N> {
    #[inline]
    fn from(value: [u8; N]) -> Self {
        Self::from_inner(FixedBlob::from(value))
    }
}