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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
use super::*;

/// **THE CONSTRUCTOR REFUSES NOTHING** — the census `NormCoord`'s own tests read from the other
/// side: every `f32`, including the three that are not numbers, is an angle this type admits.
#[test]
fn new_refuses_nothing_not_even_what_is_not_a_number() {
  assert_eq!(Radians::new(0.0_f32).get(), 0.0);
  assert_eq!(Radians::new(-10.0_f32).get(), -10.0);
  assert_eq!(Radians::new(f32::MAX).get(), f32::MAX);
  assert!(Radians::new(f32::NAN).get().is_nan());
  assert_eq!(Radians::new(f32::INFINITY).get(), f32::INFINITY);
  assert_eq!(Radians::new(f32::NEG_INFINITY).get(), f32::NEG_INFINITY);
}

/// **THE TWO CONVERSIONS ARE INVERSES** at the angles a caller actually names — the four quadrant
/// points and their negatives, where a sign or a quadrant error is easiest to spot.
#[test]
fn to_degrees_and_from_degrees_are_inverses_at_the_quadrant_points() {
  use core::f32::consts::PI;

  let cases: [(f32, f32); 6] = [
    (0.0, 0.0),
    (PI / 2.0, 90.0),
    (PI, 180.0),
    (3.0 * PI / 2.0, 270.0),
    (2.0 * PI, 360.0),
    (-PI / 2.0, -90.0),
  ];

  for (radians, degrees) in cases {
    let angle = Radians::new(radians);
    assert!(
      (angle.to_degrees() - degrees).abs() < 1e-4,
      "{radians} rad should read back near {degrees} deg, got {}",
      angle.to_degrees()
    );

    let from_deg = Radians::from_degrees(degrees);
    assert!(
      (from_deg.get() - radians).abs() < 1e-4,
      "{degrees} deg should read back near {radians} rad, got {}",
      from_deg.get()
    );
  }
}

/// `to_degrees`/`from_degrees` are pure [`RadianMath`] arithmetic and nothing more — the same answer
/// a caller holding the bare value would get, which is what makes them a convenience rather than a
/// second source of truth.
#[test]
fn the_conversions_are_exactly_the_widths_own_arithmetic() {
  let value = 1.234_f32;
  assert_eq!(Radians::new(value).to_degrees(), value.to_degrees());
  assert_eq!(Radians::from_degrees(value).get(), value.to_radians());
}

/// The angle renders as the plain value it wraps — no unit suffix, no parens — which is
/// [`NormCoord`](super::super::NormCoord)'s own posture at [`core::fmt::Display`], delegated rather
/// than repeated.
#[test]
fn displays_as_the_plain_value() {
  assert_eq!(Radians::new(1.5_f32).to_string(), "1.5");
  assert_eq!(Radians::new(-0.25_f32).to_string(), "-0.25");
}

/// **THE ONE EXTRACTION THIS GENERIC FORM PUBLISHES** — the concrete form's `From<Radians> for f32`
/// has no generic counterpart (the module's own note explains the orphan-rule wall), so
/// [`get`](Radians::get) is where a caller reaches the wrapped value now, and it survives unchanged
/// from whatever [`new`](Radians::new) was given.
#[test]
fn widens_through_get_and_the_value_survives_unchanged() {
  let angle = Radians::new(0.75_f32);
  assert_eq!(angle.get(), 0.75);
}

/// **THE CURSOR ROUND TRIP**, with no refusal on the way back — unlike
/// [`NormCoord`](super::super::NormCoord)'s own cursor, EVERY byte sequence the width's codec can
/// produce is an angle here, including the ones a normalized coordinate's read would have refused.
#[test]
fn a_radian_round_trips_through_a_cursor() {
  for value in [0.0_f32, -1.5, core::f32::consts::PI, f32::MIN, f32::MAX] {
    let mut bytes = Vec::new();
    let written = Radians::new(value);
    CursorValue::write_cursor(&written, &mut bytes);

    assert_eq!(
      <Radians<f32> as CursorValue>::read_cursor(&bytes),
      Some(written)
    );
  }

  // The forged cursor `NormCoord` would have refused — 2.0 is outside its interval — is an ordinary
  // angle here, because there is no interval to be outside of.
  let mut forged = Vec::new();
  CursorValue::write_cursor(&2.0_f32, &mut forged);
  assert_eq!(
    <Radians<f32> as CursorValue>::read_cursor(&forged),
    Some(Radians::new(2.0_f32))
  );
}

/// **ORDER — STRUCTURAL, and now a DELEGATION**: an angle states no `SEGMENTS`/`ORDER` of its own —
/// it reads whatever `T` (here `f32`) already states, which for every scalar width in this crate is
/// [`ColumnKind`]'s own default: one leaf, on [`crate::OrderLevel`]'s own note calling that answering
/// [`crate::OrderLeaves::Leaf`] with itself.
#[test]
fn radians_states_no_order_sequence_of_its_own_and_mirrors_the_components() {
  assert_eq!(
    <Radians<f32> as ColumnKind>::SEGMENTS,
    <f32 as ColumnKind>::SEGMENTS
  );
  assert_eq!(
    <Radians<f32> as ColumnKind>::ORDER,
    <f32 as ColumnKind>::ORDER
  );
  assert_eq!(
    <Radians<f32> as ColumnKind>::SEGMENTS,
    &[] as &[crate::ColumnShape]
  );
  assert_eq!(
    <Radians<f32> as ColumnKind>::ORDER,
    &[] as &[crate::OrderLevel]
  );
}

/// **ORDER — THE ENGINE'S, not the cursor's**: `cursor::value`'s own note on the float row states
/// this plainly and this test is what pins the crate against it rather than assuming it —
/// `to_be_bytes` buys FAITHFULNESS, not order, and a signed angle is exactly the case that note
/// warns about: a negative angle's cursor bytes sort ABOVE a positive one's, which is backwards from
/// the angles themselves and is not a defect, because a keyset page never compares cursor bytes
/// against each other — it binds the decoded value back into the engine's own `>`/`<`, which reads
/// the float correctly on every dialect. `NormCoord` and `Confidence` never meet this: both are
/// bounded to `[0.0, 1.0]`, where the byte order and the numeric order happen to agree.
#[test]
fn a_negative_angles_cursor_bytes_sort_above_a_positive_ones_and_that_is_not_the_orders_job() {
  let mut negative = Vec::new();
  CursorValue::write_cursor(&Radians::new(-1.0_f32), &mut negative);
  let mut positive = Vec::new();
  CursorValue::write_cursor(&Radians::new(1.0_f32), &mut positive);

  assert!(
    negative > positive,
    "IEEE 754's sign bit puts every negative bit pattern above every non-negative one; the \
     engine's own numeric comparison is where -1.0 rad correctly reads as less than 1.0 rad"
  );

  // Within the NON-NEGATIVE range the two orders coincide, which is the property
  // `NormCoord`'s own cursor relies on and the reason it never needed this note.
  let mut smaller = Vec::new();
  CursorValue::write_cursor(&Radians::new(0.5_f32), &mut smaller);
  let mut larger = Vec::new();
  CursorValue::write_cursor(&Radians::new(1.5_f32), &mut larger);
  assert!(smaller < larger);
}

/// **THE FILTER FACE** — [`ScalarFilter<Radians<f32>>`] takes every operator a total order over the
/// admitted values has, including `in`/`notIn`: unlike [`crate::ConfidenceFilter`], there is no
/// interval here for a membership list to be dishonest about, so the full eight-operator face is the
/// one this type takes.
#[test]
fn the_filter_carries_every_operator_and_round_trips_them() {
  let filter = crate::ScalarFilter::<Radians<f32>>::new()
    .with_eq(Radians::new(0.0_f32))
    .with_ne(Radians::new(1.0_f32))
    .with_gt(Radians::new(-1.0_f32))
    .with_gte(Radians::new(-2.0_f32))
    .with_lt(Radians::new(3.0_f32))
    .with_lte(Radians::new(4.0_f32))
    .with_in_([Radians::new(0.0_f32), Radians::new(1.0_f32)])
    .with_not_in([Radians::new(2.0_f32)]);

  assert!(!crate::GraphqlFilter::is_empty(&filter));
  assert_eq!(filter.eq(), Some(&Radians::new(0.0_f32)));
  assert_eq!(filter.ne(), Some(&Radians::new(1.0_f32)));
  assert_eq!(filter.gt(), Some(&Radians::new(-1.0_f32)));
  assert_eq!(filter.gte(), Some(&Radians::new(-2.0_f32)));
  assert_eq!(filter.lt(), Some(&Radians::new(3.0_f32)));
  assert_eq!(filter.lte(), Some(&Radians::new(4.0_f32)));
  assert_eq!(
    filter.in_(),
    Some(&[Radians::new(0.0_f32), Radians::new(1.0_f32)][..])
  );
  assert_eq!(filter.not_in(), Some(&[Radians::new(2.0_f32)][..]));
}

/// The empty filter asks nothing, exactly as every other [`ScalarFilter`](crate::ScalarFilter) does.
#[test]
fn an_empty_filter_asks_nothing() {
  let filter = crate::ScalarFilter::<Radians<f32>>::new();
  assert!(crate::GraphqlFilter::is_empty(&filter));
}

/// **MONGO, ROUND TRIP**: an angle crosses as whatever BSON its width crosses as — a `Double`, since
/// BSON's only binary float is 64 bits — and nothing about the value is narrowed or checked on the
/// way, because there is no invariant here for a decode to re-establish.
#[cfg(feature = "mongo")]
#[test]
fn a_radian_crosses_to_mongo_as_a_double() {
  let value = Radians::new(-1.75_f32);
  assert_eq!(
    crate::MongoCarrier::to_bson(&value),
    bson::Bson::Double(-1.75)
  );
}

/// **SQL, ROUND TRIP**: bound through [`Carrier`](crate::Carrier) into a column declared through
/// [`StoredAs`](crate::StoredAs), read back through an ordinary query — the same shape
/// `column::stored::tests::a_declared_column_carries_and_reads_back` proves for the framework's
/// numeric widths, spent here on the newtype that delegates straight to `f32`'s own row.
#[cfg(feature = "sqlite")]
#[tokio::test]
async fn a_radian_binds_into_sqlite_and_reads_back_as_the_same_float() {
  use sqlx::{Connection as _, Row as _};

  use crate::query::sql::AsyncSchemaDialect;

  let mut db = sqlx::SqliteConnection::connect("sqlite::memory:")
    .await
    .expect("an in-memory database opens");
  let create = format!(
    "CREATE TABLE pose (roll {})",
    <Radians<f32> as crate::StoredAs<sqlx::Sqlite>>::WORDS[0],
  );
  <sqlx::Sqlite as AsyncSchemaDialect>::execute(&create, &mut db)
    .await
    .expect("the declaration is legal");

  let mut qb = sqlx::QueryBuilder::<sqlx::Sqlite>::new("INSERT INTO pose VALUES (");
  crate::Carrier::push_bind(&Radians::new(-1.5_f32), &mut qb);
  qb.push(")");
  qb.build()
    .execute(&mut db)
    .await
    .expect("the bind fits the column it declared");

  let row = sqlx::query("SELECT roll FROM pose")
    .fetch_one(&mut db)
    .await
    .expect("the row reads");
  assert_eq!(row.get::<f32, _>(0), -1.5);
}

/// **THE STORED WORD IS THE WIDTH's**, on every dialect at once — the claim
/// [`StoredAs`](crate::StoredAs)'s delegation makes, checked rather than assumed.
#[test]
fn every_dialect_declares_a_radian_as_it_declares_an_f32() {
  #[cfg(feature = "sqlite")]
  assert_eq!(
    <Radians<f32> as crate::StoredAs<sqlx::Sqlite>>::WORDS,
    <f32 as crate::StoredAs<sqlx::Sqlite>>::WORDS
  );
  #[cfg(feature = "postgres")]
  assert_eq!(
    <Radians<f32> as crate::StoredAs<sqlx::Postgres>>::WORDS,
    <f32 as crate::StoredAs<sqlx::Postgres>>::WORDS
  );
  #[cfg(feature = "mysql")]
  assert_eq!(
    <Radians<f32> as crate::StoredAs<sqlx::MySql>>::WORDS,
    <f32 as crate::StoredAs<sqlx::MySql>>::WORDS
  );
}

/// **THE GRAPHQL CROSSING ROUND TRIPS**, both directions agreeing about one value — the property the
/// wire face's own note calls the mirror law: what a column returns and what a filter reads must be
/// the same form for the same value.
#[cfg(feature = "async-graphql")]
#[test]
fn the_graphql_crossing_round_trips_through_f32() {
  let value = Radians::new(2.5_f32);
  let carried = <Radians<f32> as crate::ToGraphqlOutput>::to_graphql_output(&value);
  assert_eq!(f32::from(carried), 2.5);

  let crossed = <Radians<f32> as crate::ToGraphqlInput>::cross(crate::F32::from(2.5))
    .expect("the crossing cannot fail");
  assert_eq!(crossed, value);
}

/// **THE FILTER'S NAME IS ITS OWN**, and this is the row that pins it: [`Count`](crate::Count)'s
/// `CardinalityFilter` one width over, so a schema holding both a plain `f32` column and a `Radians`
/// one never asks `Registry::create_type` to seat two Rust types under `F32Filter`. The SDL shape
/// itself is pinned in `graphql::tests::wire`, where every other citizen's is.
#[cfg(feature = "async-graphql")]
#[test]
fn the_scalar_wire_row_names_its_own_filter_and_crosses_as_f32() {
  assert_eq!(
    <crate::Builtin as crate::ScalarWire<Radians<f32>>>::NAME,
    "RadiansFilter"
  );

  let carrier = <crate::Builtin as crate::ScalarWire<Radians<f32>>>::carry(&Radians::new(0.5_f32));
  assert_eq!(f32::from(carrier), 0.5);
  assert_eq!(
    <crate::Builtin as crate::ScalarWire<Radians<f32>>>::cross(crate::F32::from(0.5))
      .expect("the crossing cannot fail"),
    Radians::new(0.5_f32)
  );
}

// ---------------------------------------------------------------------------------------------
// THE SECOND WIDTH — one smoke per face above, at `f64`, proving every one of them is a genuine
// generic delegation rather than an `f32`-shaped body that merely COMPILES for another `T`. Nothing
// in `radians.rs` special-cases `f64` anywhere: every assertion below exercises the SAME generic
// code the `f32` tests above already ran.
// ---------------------------------------------------------------------------------------------

/// `new`/`get`/`to_degrees`/`from_degrees`/`Display`, all at `f64`.
#[test]
fn at_f64_new_get_to_degrees_from_degrees_and_display_all_delegate_generically() {
  let angle = Radians::new(1.25_f64);
  assert_eq!(angle.get(), 1.25);
  assert_eq!(angle.to_degrees(), 1.25_f64.to_degrees());
  assert_eq!(Radians::from_degrees(90.0_f64).get(), 90.0_f64.to_radians());
  assert_eq!(Radians::new(-0.5_f64).to_string(), "-0.5");
  assert!(Radians::new(f64::NAN).get().is_nan());
}

/// `ColumnKind` and `CursorValue`, both delegated to `f64`'s own — ungated, exactly as the `f32`
/// rows are.
#[test]
fn at_f64_the_column_shape_and_cursor_delegate_to_f64s_own() {
  assert_eq!(
    <Radians<f64> as ColumnKind>::SEGMENTS,
    <f64 as ColumnKind>::SEGMENTS
  );
  assert_eq!(
    <Radians<f64> as ColumnKind>::ORDER,
    <f64 as ColumnKind>::ORDER
  );

  let mut bytes = Vec::new();
  let written = Radians::new(-3.5_f64);
  CursorValue::write_cursor(&written, &mut bytes);
  assert_eq!(
    <Radians<f64> as CursorValue>::read_cursor(&bytes),
    Some(written)
  );
}

/// `StoredAs`/`Carrier`/`Assemble`, all at `f64`, through the same SQLite round trip the `f32` test
/// runs.
#[cfg(feature = "sqlite")]
#[tokio::test]
async fn at_f64_binds_into_sqlite_and_reads_back_as_the_same_double() {
  use sqlx::{Connection as _, Row as _};

  use crate::query::sql::AsyncSchemaDialect;

  let mut db = sqlx::SqliteConnection::connect("sqlite::memory:")
    .await
    .expect("an in-memory database opens");
  let create = format!(
    "CREATE TABLE tilt (angle {})",
    <Radians<f64> as crate::StoredAs<sqlx::Sqlite>>::WORDS[0],
  );
  <sqlx::Sqlite as AsyncSchemaDialect>::execute(&create, &mut db)
    .await
    .expect("the declaration is legal");

  let mut qb = sqlx::QueryBuilder::<sqlx::Sqlite>::new("INSERT INTO tilt VALUES (");
  crate::Carrier::push_bind(&Radians::new(-2.5_f64), &mut qb);
  qb.push(")");
  qb.build()
    .execute(&mut db)
    .await
    .expect("the bind fits the column it declared");

  let row = sqlx::query("SELECT angle FROM tilt")
    .fetch_one(&mut db)
    .await
    .expect("the row reads");
  assert_eq!(row.get::<f64, _>(0), -2.5);
}

/// `MongoCarrier`, at `f64` — still a BSON `Double`, the same as `f32` widens into.
#[cfg(feature = "mongo")]
#[test]
fn at_f64_crosses_to_mongo_as_a_double() {
  let value = Radians::new(-1.75_f64);
  assert_eq!(
    crate::MongoCarrier::to_bson(&value),
    bson::Bson::Double(-1.75)
  );
}

/// `ToGraphqlInput`/`ToGraphqlOutput` and the `ScalarWire` row, at `f64` — the VALUE crosses on
/// `f64`'s own roster reading (`F64`, not `F32`), and the FILTER's name stays `RadiansFilter`, fixed
/// across widths exactly as the module's own note says it must.
#[cfg(feature = "async-graphql")]
#[test]
fn at_f64_the_graphql_crossing_delegates_to_f64s_own_reading_and_the_filter_name_stays_fixed() {
  let value = Radians::new(2.5_f64);
  let carried = <Radians<f64> as crate::ToGraphqlOutput>::to_graphql_output(&value);
  assert_eq!(f64::from(carried), 2.5);

  let crossed = <Radians<f64> as crate::ToGraphqlInput>::cross(crate::F64::from(2.5))
    .expect("the crossing cannot fail");
  assert_eq!(crossed, value);

  assert_eq!(
    <crate::Builtin as crate::ScalarWire<Radians<f64>>>::NAME,
    "RadiansFilter"
  );
  let filter_carrier = <crate::Builtin as crate::ScalarWire<Radians<f64>>>::carry(&value);
  assert_eq!(f64::from(filter_carrier), 2.5);
  assert_eq!(
    <crate::Builtin as crate::ScalarWire<Radians<f64>>>::cross(crate::F64::from(2.5))
      .expect("the crossing cannot fail"),
    value
  );
}

/// The scalar filter marker road, at `f64` — `ScalarFilter<Radians<f64>>` resolves and round-trips
/// an operator exactly as `ScalarFilter<Radians<f32>>` does.
#[cfg(feature = "async-graphql")]
#[test]
fn at_f64_the_filter_marker_resolves_and_the_filter_round_trips() {
  let filter = crate::ScalarFilter::<Radians<f64>>::new()
    .with_eq(Radians::new(0.5_f64))
    .with_gte(Radians::new(-1.0_f64));

  assert!(!crate::GraphqlFilter::is_empty(&filter));
  assert_eq!(filter.eq(), Some(&Radians::new(0.5_f64)));
  assert_eq!(filter.gte(), Some(&Radians::new(-1.0_f64)));
}