ingraph 0.1.0

A declaration-driven indexing framework: records and nodes are written as Rust types, and the storage rows, the dataflow between them and the GraphQL query surface are emitted from the declaration.
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
//! TWO TOY SINKS against one set of rows, which is the whole claim under test: a row speaks columns
//! and the engine decides what a column IS.
//!
//! The sinks themselves are [`testkit`](super::testkit)'s — [`Fallback`] implements the required
//! kinds and nothing else, so every defaulted method's own body runs and each landed column is
//! opaque bytes, and [`Native`] overrides the same kinds to land a column per width. The rows below
//! are written once and both sinks read them back.

use core::{
  net::{IpAddr, Ipv4Addr, Ipv6Addr},
  num::NonZeroU64,
};

use uuid::Uuid;

use super::{
  ColumnError, ColumnReader, ColumnWriter,
  testkit::{Col, Cursor, Fallback, FallbackRead, Native, NativeRead, Refusal},
};

/// The uuid every row below carries, so a failure names a value rather than a fresh one.
fn identity() -> Uuid {
  Uuid::from_bytes([
    0x9f, 0x1c, 0x2d, 0x3e, 0x4a, 0x5b, 0x6c, 0x7d, 0x8e, 0x9f, 0xa0, 0xb1, 0xc2, 0xd3, 0xe4, 0xf5,
  ])
}

/// EVERY REQUIRED KIND, one column each, in one key's order.
fn write_required(w: &mut impl ColumnWriter) {
  w.i64(-9_007_199_254_740_993);
  w.u64(u64::MAX);
  w.text("a caption");
  w.bytes(&[0xde, 0xad, 0xbe, 0xef]);
  w.uuid(identity());
}

/// The same key, read back — the tuple a round-trip compares.
#[expect(clippy::type_complexity, reason = "the key under test IS this tuple")]
fn read_required<R: ColumnReader>(
  r: &mut R,
) -> Result<(i64, u64, String, Vec<u8>, Uuid), R::Error> {
  Ok((
    r.i64()?,
    r.u64()?,
    r.text()?.to_owned(),
    r.bytes()?.to_vec(),
    r.uuid()?,
  ))
}

/// EVERY DEFAULTED KIND — the eight widths that arrive with a fallback.
fn write_widths(w: &mut impl ColumnWriter) {
  w.i8(i8::MIN);
  w.i16(i16::MIN);
  w.i32(i32::MIN);
  w.i128(i128::MIN);
  w.u8(u8::MAX);
  w.u16(u16::MAX);
  w.u32(u32::MAX);
  w.u128(u128::MAX);
}

/// The same eight, read back.
#[expect(clippy::type_complexity, reason = "the key under test IS this tuple")]
fn read_widths<R: ColumnReader>(
  r: &mut R,
) -> Result<(i8, i16, i32, i128, u8, u16, u32, u128), R::Error> {
  Ok((
    r.i8()?,
    r.i16()?,
    r.i32()?,
    r.i128()?,
    r.u8()?,
    r.u16()?,
    r.u32()?,
    r.u128()?,
  ))
}

/// THE `NonZero*` ROW, transcribed: the inner integer's own column, narrowed on the way out — where
/// a zero is well-formed and inadmissible, and the row raises [`ColumnError::domain`] for the engine
/// to word.
fn write_nonzero(w: &mut impl ColumnWriter, value: NonZeroU64) {
  w.u64(value.get());
}

fn read_nonzero<R: ColumnReader>(r: &mut R) -> Result<NonZeroU64, R::Error> {
  let raw = r.u64()?;
  NonZeroU64::new(raw).ok_or_else(|| R::Error::domain(raw))
}

/// THE UNTAGGED `IpAddr` ROW, transcribed: the WIDTH is the discriminant, so the arms are read apart
/// rather than tagged apart and a width outside the set is the row's own refusal.
fn write_host(w: &mut impl ColumnWriter, host: IpAddr) {
  match host {
    IpAddr::V4(address) => w.bytes(&address.octets()),
    IpAddr::V6(address) => w.bytes(&address.octets()),
  }
}

fn read_host<R: ColumnReader>(r: &mut R) -> Result<IpAddr, R::Error> {
  let value = r.bytes()?;
  match value.len() {
    4 => Ok(IpAddr::V4(Ipv4Addr::from(
      <[u8; 4]>::try_from(value).expect("four octets"),
    ))),
    16 => Ok(IpAddr::V6(Ipv6Addr::from(
      <[u8; 16]>::try_from(value).expect("sixteen octets"),
    ))),
    got => Err(R::Error::width(got, &[4, 16])),
  }
}

/// A SECOND TOY ERROR — the same refusals, worded by an engine that keeps the width one STRUCTURED
/// instead of taking the sentence its default writes.
///
/// It is the other half of a defaulted method: [`Refusal`] takes both defaults, this one overrides
/// `width` and leaves `domain`, and one row refused against both is what shows the two roads are
/// live at once.
#[derive(Debug, Clone, PartialEq, Eq)]
enum Structured {
  /// What the cursor itself refused, carried through unchanged.
  Engine(Refusal),
  /// A row's refusal that came through the open floor.
  Custom(String),
  /// A row's width refusal, kept as the two numbers rather than as a sentence about them.
  Width {
    got: usize,
    expected: &'static [usize],
  },
}

impl ColumnError for Structured {
  fn custom<T: core::fmt::Display>(msg: T) -> Self {
    Self::Custom(msg.to_string())
  }

  fn width(got: usize, expected: &'static [usize]) -> Self {
    Self::Width { got, expected }
  }
}

/// [`Fallback`]'s cursor under the structured error — the same columns, a different vocabulary, and
/// nothing else about the engine changed.
#[derive(Debug)]
struct StructuredRead(FallbackRead);

impl ColumnReader for StructuredRead {
  type Error = Structured;

  fn i64(&mut self) -> Result<i64, Self::Error> {
    self.0.i64().map_err(Structured::Engine)
  }

  fn u64(&mut self) -> Result<u64, Self::Error> {
    self.0.u64().map_err(Structured::Engine)
  }

  fn text(&mut self) -> Result<&str, Self::Error> {
    self.0.text().map_err(Structured::Engine)
  }

  fn bytes(&mut self) -> Result<&[u8], Self::Error> {
    self.0.bytes().map_err(Structured::Engine)
  }

  fn uuid(&mut self) -> Result<Uuid, Self::Error> {
    self.0.uuid().map_err(Structured::Engine)
  }

  fn fixed<const N: usize>(&mut self) -> Result<[u8; N], Self::Error> {
    self.0.fixed().map_err(Structured::Engine)
  }

  fn finish(self) -> Result<(), Self::Error> {
    self.0.finish().map_err(Structured::Engine)
  }
}

/// One column landed, and the structured engine's cursor over it.
fn structured(write: impl FnOnce(&mut Fallback)) -> StructuredRead {
  let mut sink = Fallback::default();
  write(&mut sink);
  StructuredRead(sink.read())
}

#[test]
fn every_required_kind_crosses_and_comes_back() {
  let mut sink = Fallback::default();
  write_required(&mut sink);

  let mut read = FallbackRead(Cursor::new(sink.0));
  assert_eq!(
    read_required(&mut read).expect("the required kinds read back"),
    (
      -9_007_199_254_740_993,
      u64::MAX,
      "a caption".to_owned(),
      vec![0xde, 0xad, 0xbe, 0xef],
      identity(),
    )
  );
  read.finish().expect("the key took every column");
}

#[test]
fn a_defaulted_kind_lands_as_bytes_and_reads_back_through_them() {
  let mut sink = Fallback::default();
  write_widths(&mut sink);

  // The evidence that the DEFAULT bodies ran: this engine implements none of these kinds, so every
  // one of them arrived through the fallback.
  assert!(
    sink.0.iter().all(|col| matches!(col, Col::Bytes(_))),
    "an engine with no native width landed one: {:?}",
    sink.0
  );

  let mut read = FallbackRead(Cursor::new(sink.0));
  assert_eq!(
    read_widths(&mut read).expect("the widths read back"),
    (
      i8::MIN,
      i16::MIN,
      i32::MIN,
      i128::MIN,
      u8::MAX,
      u16::MAX,
      u32::MAX,
      u128::MAX,
    )
  );
  read.finish().expect("the key took every column");
}

#[test]
fn an_engine_that_overrides_a_kind_lands_its_own_column() {
  let mut sink = Native::default();
  write_widths(&mut sink);

  assert_eq!(
    sink.0,
    vec![
      Col::I8(i8::MIN),
      Col::I16(i16::MIN),
      Col::I32(i32::MIN),
      Col::I128(i128::MIN),
      Col::U8(u8::MAX),
      Col::U16(u16::MAX),
      Col::U32(u32::MAX),
      Col::U128(u128::MAX),
    ]
  );

  let mut read = NativeRead(Cursor::new(sink.0));
  assert_eq!(
    read_widths(&mut read).expect("the widths read back"),
    (
      i8::MIN,
      i16::MIN,
      i32::MIN,
      i128::MIN,
      u8::MAX,
      u16::MAX,
      u32::MAX,
      u128::MAX,
    )
  );
  read.finish().expect("the key took every column");
}

#[test]
fn the_same_row_reads_back_off_either_engine() {
  let mut native = Native::default();
  write_widths(&mut native);
  let mut fallback = Fallback::default();
  write_widths(&mut fallback);

  // The columns landed differ; what the row gets back does not.
  assert_ne!(native.0, fallback.0);
  assert_eq!(
    read_widths(&mut NativeRead(Cursor::new(native.0))).expect("native reads back"),
    read_widths(&mut FallbackRead(Cursor::new(fallback.0))).expect("the fallback reads back")
  );
}

#[test]
fn a_key_that_reads_short_is_refused_at_finish() {
  let mut sink = Fallback::default();
  sink.i64(7);
  sink.i64(42);
  sink.i64(3);

  let mut read = FallbackRead(Cursor::new(sink.0));
  assert_eq!(read.i64().expect("the first column"), 7);
  assert_eq!(read.i64().expect("the second column"), 42);

  // `(7, 42)` would otherwise pose as the address of `(7, 42, 3)`.
  assert_eq!(read.finish(), Err(Refusal::Trailing(1)));
}

#[test]
fn a_column_taken_as_the_wrong_kind_is_a_named_refusal() {
  let mut sink = Fallback::default();
  sink.i64(7);

  let mut read = FallbackRead(Cursor::new(sink.0));
  assert_eq!(
    read.text().expect_err("an i64 column is not text"),
    Refusal::Kind {
      wanted: "text",
      found: "i64",
    }
  );
}

#[test]
fn a_fixed_width_read_at_another_width_is_the_engines_refusal() {
  let mut sink = Fallback::default();
  sink.bytes(&[1, 2, 3]);

  let mut read = FallbackRead(Cursor::new(sink.0));
  assert_eq!(
    read.fixed::<4>().expect_err("three bytes are not four"),
    Refusal::Octets {
      wanted: 4,
      found: 3,
    }
  );
}

#[test]
fn a_column_asked_for_past_the_last_is_the_engines_refusal() {
  let mut read = FallbackRead(Cursor::new(Vec::new()));
  assert_eq!(read.i64().expect_err("there is no column"), Refusal::End);
}

#[test]
fn a_row_words_a_domain_refusal_through_the_engine() {
  let mut sink = Fallback::default();
  write_nonzero(&mut sink, NonZeroU64::new(9).expect("9 is not zero"));
  sink.u64(0);

  let mut read = FallbackRead(Cursor::new(sink.0));
  assert_eq!(
    read_nonzero(&mut read).expect("a nonzero column reads back"),
    NonZeroU64::new(9).expect("9 is not zero")
  );
  // This engine leaves `domain` defaulted, so what it holds is the sentence the default wrote —
  // carrying the value that was refused, which is the one thing wrong with a well-formed column.
  assert_eq!(
    read_nonzero(&mut read).expect_err("zero is not a NonZeroU64"),
    Refusal::Custom("value 0 outside the type's domain".to_owned())
  );
}

#[test]
fn an_untagged_row_words_a_width_refusal_through_the_engine() {
  let mut sink = Fallback::default();
  write_host(&mut sink, IpAddr::V4(Ipv4Addr::new(10, 0, 0, 1)));
  write_host(&mut sink, IpAddr::V6(Ipv6Addr::LOCALHOST));
  sink.bytes(&[1, 2, 3, 4, 5]);

  let mut read = FallbackRead(Cursor::new(sink.0));
  assert_eq!(
    read_host(&mut read).expect("four octets are a v4 host"),
    IpAddr::V4(Ipv4Addr::new(10, 0, 0, 1))
  );
  assert_eq!(
    read_host(&mut read).expect("sixteen octets are a v6 host"),
    IpAddr::V6(Ipv6Addr::LOCALHOST)
  );
  assert_eq!(
    read_host(&mut read).expect_err("five octets are no host"),
    Refusal::Custom("width 5, expected one of [4, 16]".to_owned())
  );
}

#[test]
fn an_engine_that_overrides_a_named_refusal_words_it_itself() {
  // BOTH ROADS, one row: the sentence a defaulted `width` writes, and the record an engine that
  // overrode it keeps. Same columns, same row, and the refusal is the engine's to shape.
  let mut defaulted = Fallback::default();
  defaulted.bytes(&[1, 2, 3, 4, 5]);
  assert_eq!(
    read_host(&mut defaulted.read()).expect_err("five octets are no host"),
    Refusal::Custom("width 5, expected one of [4, 16]".to_owned())
  );

  assert_eq!(
    read_host(&mut structured(|sink| sink.bytes(&[1, 2, 3, 4, 5])))
      .expect_err("five octets are no host"),
    Structured::Width {
      got: 5,
      expected: &[4, 16],
    }
  );

  // The floor is still the floor on the overriding engine: `domain` was NOT overridden, so it
  // arrives as its default's sentence — an engine words the refusals it cares about and no others.
  assert_eq!(
    read_nonzero(&mut structured(|sink| sink.u64(0))).expect_err("zero is not a NonZeroU64"),
    Structured::Custom("value 0 outside the type's domain".to_owned())
  );

  // And what the CURSOR refuses is untouched by either: a missing column is the engine's own.
  assert_eq!(
    structured(|_| ()).i64().expect_err("there is no column"),
    Structured::Engine(Refusal::End)
  );
}