id_effect 0.2.0

Effect<A, E, R> (sync + async), context/layers, pipe — interpreter-style, no bundled executor
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
426
427
428
429
430
431
432
433
//! Composable predicates — mirrors Effect.ts `Predicate` namespace.
//!
//! `Predicate<A>` is a heap-allocated, `Send + Sync` boolean function over `&A`.
//! All combinators return new `Predicate<A>` values, enabling runtime composition.

// ── Predicate<A> ─────────────────────────────────────────────────────────────

/// A runtime-composable boolean function — mirrors Effect.ts `Predicate<A>`.
///
/// Heap-allocated and `Send + Sync` so predicates can be stored and shared
/// across threads without lifetime constraints.
pub type Predicate<A> = Box<dyn Fn(&A) -> bool + Send + Sync>;

// ── predicate module ─────────────────────────────────────────────────────────

/// Constructors and combinators for `Predicate<A>`.
#[allow(clippy::module_inception)]
pub mod predicate {
  use super::Predicate;

  // ── Boolean combinators ───────────────────────────────────────────────────

  /// `p AND q` — true when both predicates hold.
  pub fn and<A: 'static>(p: Predicate<A>, q: Predicate<A>) -> Predicate<A> {
    Box::new(move |a| p(a) && q(a))
  }

  /// `p OR q` — true when at least one predicate holds.
  pub fn or<A: 'static>(p: Predicate<A>, q: Predicate<A>) -> Predicate<A> {
    Box::new(move |a| p(a) || q(a))
  }

  /// `NOT p` — true when the predicate does not hold.
  pub fn not<A: 'static>(p: Predicate<A>) -> Predicate<A> {
    Box::new(move |a| !p(a))
  }

  /// `p XOR q` — true when exactly one predicate holds.
  pub fn xor<A: 'static>(p: Predicate<A>, q: Predicate<A>) -> Predicate<A> {
    Box::new(move |a| p(a) ^ q(a))
  }

  /// `p IMPLIES q` — equivalent to `NOT p OR q`.
  pub fn implies<A: 'static>(p: Predicate<A>, q: Predicate<A>) -> Predicate<A> {
    Box::new(move |a| !p(a) || q(a))
  }

  /// `p XNOR q` (equivalence) — true when both hold or neither holds.
  pub fn eqv<A: 'static>(p: Predicate<A>, q: Predicate<A>) -> Predicate<A> {
    Box::new(move |a| p(a) == q(a))
  }

  // ── Contramap ────────────────────────────────────────────────────────────

  /// Contramap: apply `f` to the input before testing with `p`.
  ///
  /// Mirrors Effect.ts `Predicate.contramap`.
  pub fn contramap<A: 'static, B: 'static>(
    p: Predicate<A>,
    f: impl Fn(&B) -> A + Send + Sync + 'static,
  ) -> Predicate<B> {
    Box::new(move |b| p(&f(b)))
  }

  // ── Product ──────────────────────────────────────────────────────────────

  /// Test a tuple `(A, B)` by applying `p` to the first element and `q` to the second.
  pub fn product<A: 'static, B: 'static>(p: Predicate<A>, q: Predicate<B>) -> Predicate<(A, B)> {
    Box::new(move |(a, b)| p(a) && q(b))
  }

  // ── All ──────────────────────────────────────────────────────────────────

  /// Test a `Vec<A>` element-wise: true when every element satisfies its corresponding predicate.
  ///
  /// If `predicates` is shorter than `values`, the remaining values are ignored.
  pub fn all<A: 'static>(predicates: Vec<Predicate<A>>) -> Predicate<Vec<A>> {
    Box::new(move |values| predicates.iter().zip(values.iter()).all(|(p, v)| p(v)))
  }

  // ── Built-in refinements ──────────────────────────────────────────────────

  /// True when `Option<A>` is `Some`.
  pub fn is_some<A: 'static>() -> Predicate<Option<A>> {
    Box::new(|o| o.is_some())
  }

  /// True when `Option<A>` is `None`.
  pub fn is_none<A: 'static>() -> Predicate<Option<A>> {
    Box::new(|o| o.is_none())
  }

  /// True when `Result<A, E>` is `Ok`.
  pub fn is_ok<A: 'static, E: 'static>() -> Predicate<Result<A, E>> {
    Box::new(|r| r.is_ok())
  }

  /// True when `Result<A, E>` is `Err`.
  pub fn is_err<A: 'static, E: 'static>() -> Predicate<Result<A, E>> {
    Box::new(|r| r.is_err())
  }

  /// True when `String` is empty.
  pub fn is_empty() -> Predicate<String> {
    Box::new(|s: &String| s.is_empty())
  }

  /// True when `String` is non-empty.
  pub fn is_non_empty() -> Predicate<String> {
    Box::new(|s: &String| !s.is_empty())
  }

  /// True when `&str` is empty.
  pub fn str_is_empty() -> Predicate<str> {
    Box::new(|s: &str| s.is_empty())
  }

  /// True when a numeric value is zero.
  pub fn is_zero_i64() -> Predicate<i64> {
    Box::new(|n| *n == 0)
  }

  /// True when a numeric value is positive (> 0).
  pub fn is_positive_i64() -> Predicate<i64> {
    Box::new(|n| *n > 0)
  }

  /// True when a numeric value is negative (< 0).
  pub fn is_negative_i64() -> Predicate<i64> {
    Box::new(|n| *n < 0)
  }
}

// ── Tests ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
  use super::predicate;
  use rstest::rstest;

  // Helper: a predicate that tests whether an i64 is > 0
  fn positive() -> super::Predicate<i64> {
    Box::new(|n: &i64| *n > 0)
  }

  // Helper: a predicate that tests whether an i64 is even
  fn even() -> super::Predicate<i64> {
    Box::new(|n: &i64| *n % 2 == 0)
  }

  // ── and ──────────────────────────────────────────────────────────────────

  mod and {
    use super::*;

    #[rstest]
    #[case::both_true(2_i64, true)] // positive AND even
    #[case::first_false(-2_i64, false)] // not positive, even → false
    #[case::second_false(3_i64, false)] // positive, not even → false
    #[case::both_false(-3_i64, false)] // not positive, not even
    fn truth_table(#[case] input: i64, #[case] expected: bool) {
      let p = predicate::and(positive(), even());
      assert_eq!(p(&input), expected);
    }
  }

  // ── or ───────────────────────────────────────────────────────────────────

  mod or {
    use super::*;

    #[rstest]
    #[case::both_true(2_i64, true)]
    #[case::first_only(3_i64, true)]
    #[case::second_only(-2_i64, true)]
    #[case::neither(-3_i64, false)]
    fn truth_table(#[case] input: i64, #[case] expected: bool) {
      let p = predicate::or(positive(), even());
      assert_eq!(p(&input), expected);
    }
  }

  // ── not ──────────────────────────────────────────────────────────────────

  mod not {
    use super::*;

    #[test]
    fn not_positive_is_true_for_zero() {
      let p = predicate::not(positive());
      assert!(p(&0_i64));
    }

    #[test]
    fn not_positive_is_true_for_negative() {
      let p = predicate::not(positive());
      assert!(p(&-5_i64));
    }

    #[test]
    fn not_positive_is_false_for_positive() {
      let p = predicate::not(positive());
      assert!(!p(&1_i64));
    }
  }

  // ── xor ──────────────────────────────────────────────────────────────────

  mod xor {
    use super::*;

    #[rstest]
    #[case::both_true(2_i64, false)]
    #[case::first_only(3_i64, true)]
    #[case::second_only(-2_i64, true)]
    #[case::neither(-3_i64, false)]
    fn truth_table(#[case] input: i64, #[case] expected: bool) {
      let p = predicate::xor(positive(), even());
      assert_eq!(p(&input), expected);
    }
  }

  // ── implies ──────────────────────────────────────────────────────────────

  mod implies {
    use super::*;

    #[test]
    fn true_implies_true_is_true() {
      let p = predicate::implies(positive(), even());
      assert!(p(&2_i64)); // positive → even: T → T = T
    }

    #[test]
    fn true_implies_false_is_false() {
      let p = predicate::implies(positive(), even());
      assert!(!p(&3_i64)); // positive → even: T → F = F
    }

    #[test]
    fn false_implies_anything_is_true() {
      let p = predicate::implies(positive(), even());
      assert!(p(&-3_i64)); // positive → even: F → F = T
      assert!(p(&-2_i64)); // positive → even: F → T = T
    }
  }

  // ── eqv ──────────────────────────────────────────────────────────────────

  mod eqv {
    use super::*;

    #[rstest]
    #[case::both_true(2_i64, true)]
    #[case::both_false(-3_i64, true)]
    #[case::first_only(3_i64, false)]
    #[case::second_only(-2_i64, false)]
    fn truth_table(#[case] input: i64, #[case] expected: bool) {
      let p = predicate::eqv(positive(), even());
      assert_eq!(p(&input), expected);
    }
  }

  // ── contramap ────────────────────────────────────────────────────────────

  mod contramap {
    use super::*;

    #[test]
    fn test_string_by_length_positive() {
      // Map string → its length, then test positive
      let p = predicate::contramap(positive(), |s: &String| s.len() as i64);
      assert!(p(&"hello".to_string()));
    }

    #[test]
    fn test_empty_string_length_not_positive() {
      let p = predicate::contramap(positive(), |s: &String| s.len() as i64);
      assert!(!p(&"".to_string()));
    }
  }

  // ── product ──────────────────────────────────────────────────────────────

  mod product {
    use super::*;

    #[test]
    fn both_hold_returns_true() {
      let p = predicate::product(positive(), even());
      assert!(p(&(4_i64, 6_i64)));
    }

    #[test]
    fn first_fails_returns_false() {
      let p = predicate::product(positive(), even());
      assert!(!p(&(-1_i64, 4_i64)));
    }

    #[test]
    fn second_fails_returns_false() {
      let p = predicate::product(positive(), even());
      assert!(!p(&(3_i64, 3_i64)));
    }

    #[test]
    fn both_fail_returns_false() {
      let p = predicate::product(positive(), even());
      assert!(!p(&(-1_i64, 3_i64)));
    }
  }

  // ── all ──────────────────────────────────────────────────────────────────

  mod all {
    use super::*;

    #[test]
    fn all_positive_values_returns_true() {
      let ps = vec![positive(), positive(), positive()];
      let p = predicate::all(ps);
      assert!(p(&vec![1_i64, 2_i64, 3_i64]));
    }

    #[test]
    fn one_failing_value_returns_false() {
      let ps = vec![positive(), positive(), positive()];
      let p = predicate::all(ps);
      assert!(!p(&vec![1_i64, -1_i64, 3_i64]));
    }

    #[test]
    fn empty_predicates_returns_true_for_any_vec() {
      let ps: Vec<super::super::Predicate<i64>> = vec![];
      let p = predicate::all(ps);
      assert!(p(&vec![1_i64, 2_i64]));
    }
  }

  // ── built-in refinements ─────────────────────────────────────────────────

  mod builtins {
    use super::*;

    #[test]
    fn is_some_true_for_some() {
      assert!(predicate::is_some::<i32>()(&Some(1)));
    }

    #[test]
    fn is_some_false_for_none() {
      assert!(!predicate::is_some::<i32>()(&None));
    }

    #[test]
    fn is_none_true_for_none() {
      assert!(predicate::is_none::<i32>()(&None));
    }

    #[test]
    fn is_none_false_for_some() {
      assert!(!predicate::is_none::<i32>()(&Some(5)));
    }

    #[test]
    fn is_ok_true_for_ok() {
      let p = predicate::is_ok::<i32, &str>();
      assert!(p(&Ok(1)));
    }

    #[test]
    fn is_ok_false_for_err() {
      let p = predicate::is_ok::<i32, &str>();
      assert!(!p(&Err("oops")));
    }

    #[test]
    fn is_err_true_for_err() {
      let p = predicate::is_err::<i32, &str>();
      assert!(p(&Err("bad")));
    }

    #[test]
    fn is_err_false_for_ok() {
      let p = predicate::is_err::<i32, &str>();
      assert!(!p(&Ok(42)));
    }

    #[test]
    fn is_empty_true_for_empty_string() {
      assert!(predicate::is_empty()(&"".to_string()));
    }

    #[test]
    fn is_empty_false_for_non_empty_string() {
      assert!(!predicate::is_empty()(&"hi".to_string()));
    }

    #[test]
    fn is_non_empty_true_for_non_empty() {
      assert!(predicate::is_non_empty()(&"x".to_string()));
    }

    #[test]
    fn is_non_empty_false_for_empty() {
      assert!(!predicate::is_non_empty()(&"".to_string()));
    }

    #[rstest]
    #[case::zero(0_i64, true)]
    #[case::positive(1_i64, false)]
    #[case::negative(-1_i64, false)]
    fn is_zero_i64(#[case] input: i64, #[case] expected: bool) {
      assert_eq!(predicate::is_zero_i64()(&input), expected);
    }

    #[rstest]
    #[case::positive(5_i64, true)]
    #[case::zero(0_i64, false)]
    #[case::negative(-1_i64, false)]
    fn is_positive_i64(#[case] input: i64, #[case] expected: bool) {
      assert_eq!(predicate::is_positive_i64()(&input), expected);
    }

    #[rstest]
    #[case::negative(-1_i64, true)]
    #[case::zero(0_i64, false)]
    #[case::positive(1_i64, false)]
    fn is_negative_i64(#[case] input: i64, #[case] expected: bool) {
      assert_eq!(predicate::is_negative_i64()(&input), expected);
    }
  }
}