easerx 0.1.1

A reactive MVI (Model-View-Intent) framework for Rust.
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
use std::hash::{Hash, Hasher};
use crate::async_error::AsyncError;
use crate::Async;

#[test]
fn test_uninitialized() {
    let uninitialized: Async<i32> = Async::default();
    assert!(!uninitialized.is_complete());
    assert!(uninitialized.should_load());
    assert!(uninitialized.is_incomplete());
    assert!(uninitialized.is_uninitialized());
    assert!(!uninitialized.is_loading());
    assert!(!uninitialized.is_success());
    assert!(!uninitialized.is_fail());
    assert!(!uninitialized.is_fail_with_timeout());
    assert!(!uninitialized.is_fail_with_none());
    assert!(!uninitialized.is_fail_with_error());
    assert!(!uninitialized.is_fail_with_canceled());
    assert!(uninitialized.value_ref().is_none());
    assert!(uninitialized.value().is_none());
}

#[test]
fn test_loading() {
    let loading = Async::loading(Some(7));
    assert!(!loading.is_complete());
    assert!(!loading.should_load());
    assert!(loading.is_incomplete());
    assert!(!loading.is_uninitialized());
    assert!(loading.is_loading());
    assert!(!loading.is_success());
    assert!(!loading.is_fail());
    assert!(!loading.is_fail_with_timeout());
    assert!(!loading.is_fail_with_none());
    assert!(!loading.is_fail_with_error());
    assert!(!loading.is_fail_with_canceled());
    assert!(loading.value_ref().is_some());
    assert_eq!(loading.value_ref(), Some(7).as_ref());
    assert_eq!(loading.value(), Some(7));

    let loading = Async::loading(None::<i32>);
    assert!(loading.value_ref().is_none());
    assert_eq!(loading.value_ref(), None);
    assert_eq!(loading.value(), None);
}

#[test]
fn test_success() {
    let success = Async::success(8);
    assert!(success.is_complete());
    assert!(!success.should_load());
    assert!(!success.is_incomplete());
    assert!(!success.is_uninitialized());
    assert!(!success.is_loading());
    assert!(success.is_success());
    assert!(!success.is_fail());
    assert!(!success.is_fail_with_timeout());
    assert!(!success.is_fail_with_none());
    assert!(!success.is_fail_with_error());
    assert!(!success.is_fail_with_canceled());
    assert!(success.value_ref().is_some());
    assert_eq!(success.value_ref(), Some(&8));
    assert_eq!(success.value(), Some(8));
}

#[test]
fn test_fail() {
    let fail = Async::fail(AsyncError::error("Connection failed"), Some(50));
    assert!(fail.is_complete());
    assert!(fail.should_load());
    assert!(!fail.is_incomplete());
    assert!(!fail.is_uninitialized());
    assert!(!fail.is_loading());
    assert!(!fail.is_success());
    assert!(fail.is_fail());
    assert!(fail.value_ref().is_some());
    assert_eq!(fail.value_ref(), Some(&50));
    assert_eq!(fail.value(), Some(50));
}

#[cfg(feature = "serde")]
#[test]
fn test_async_state_serde() {
    use serde_json;

    let uninitialized = Async::<i32>::Uninitialized;
    let serialized_uninitialized = serde_json::to_string(&uninitialized).unwrap();
    assert_eq!(serialized_uninitialized, r#""uninitialized""#);

    let success = Async::success(42);
    let serialized = serde_json::to_string(&success).unwrap();
    assert_eq!(serialized, r#"{"success":{"value":42}}"#);

    let deserialized: Async<i32> = serde_json::from_str(&serialized).unwrap();
    assert_eq!(deserialized, success);

    let loading = Async::loading(Some(42));
    let serialized_loading = serde_json::to_string(&loading).unwrap();
    assert_eq!(serialized_loading, r#"{"loading":{"value":42}}"#);

    let deserialized_loading: Async<i32> = serde_json::from_str(&serialized_loading).unwrap();
    assert_eq!(deserialized_loading, loading);

    let fail_err = Async::fail(AsyncError::error("test"), Some(42));
    let serialized_fail_err = serde_json::to_string(&fail_err).unwrap();
    assert_eq!(
        serialized_fail_err,
        r#"{"fail":{"error":{"error":"test"},"value":42}}"#
    );

    let deserialized_fail: Async<i32> = serde_json::from_str(&serialized_fail_err).unwrap();
    assert_eq!(deserialized_fail, fail_err);

    let fail_none = Async::fail(AsyncError::None, Some(42));
    let serialized_fail_none = serde_json::to_string(&fail_none).unwrap();
    assert_eq!(
        serialized_fail_none,
        r#"{"fail":{"error":"none","value":42}}"#
    );

    let deserialized_fail: Async<i32> = serde_json::from_str(&serialized_fail_none).unwrap();
    assert_eq!(deserialized_fail, fail_none);

    let fail_cancelled = Async::fail(AsyncError::Cancelled, Some(42));
    let serialized_fail_cancelled = serde_json::to_string(&fail_cancelled).unwrap();
    assert_eq!(
        serialized_fail_cancelled,
        r#"{"fail":{"error":"cancelled","value":42}}"#
    );

    let deserialized_fail: Async<i32> = serde_json::from_str(&serialized_fail_cancelled).unwrap();
    assert_eq!(deserialized_fail, fail_cancelled);

    let fail_timeout = Async::fail(AsyncError::Timeout, Some(42));
    let serialized_fail_timeout = serde_json::to_string(&fail_timeout).unwrap();
    assert_eq!(
        serialized_fail_timeout,
        r#"{"fail":{"error":"timeout","value":42}}"#
    );

    let deserialized_fail: Async<i32> = serde_json::from_str(&serialized_fail_timeout).unwrap();
    assert_eq!(deserialized_fail, fail_timeout);
}

#[test]
fn test_async_state_marco_debug() {
    let uninitialized: Async<i32> = Async::default();
    let debug_str = format!("{:?}", uninitialized);
    assert_eq!(debug_str, "Uninitialized");
    
    let loading = Async::loading(Some(42));
    let debug_str = format!("{:?}", loading);
    assert_eq!(debug_str, "Loading { value: Some(42) }");
    
    let success = Async::success(42);
    let debug_str = format!("{:?}", success);
    assert_eq!(debug_str, "Success { value: 42 }");
    
    let fail = Async::fail(AsyncError::error("test"), Some(42));
    let debug_str = format!("{:?}", fail);
    assert_eq!(debug_str, "Fail { error: Error(\"test\"), value: Some(42) }");
}
#[test]
fn test_async_state_hash(){

    let uninitialized1: Async<i32> = Async::default();
    let uninitialized2: Async<i32> = Async::default();
    let unintialized1_hash={
        let mut hasher = std::collections::hash_map::DefaultHasher::new();
        uninitialized1.hash(&mut hasher);
        hasher.finish()
    };
    let unintialized2_hash={
        let mut hasher = std::collections::hash_map::DefaultHasher::new();
        uninitialized2.hash(&mut hasher);
        hasher.finish()
    };
    assert_eq!(unintialized1_hash, unintialized2_hash);

    let loading1 = Async::loading(Some(42));
    let loading2 = Async::loading(Some(42));
    let loading1_hash = {
        let mut hasher = std::collections::hash_map::DefaultHasher::new();
        loading1.hash(&mut hasher);
        hasher.finish()
    };
    let loading2_hash = {
        let mut hasher = std::collections::hash_map::DefaultHasher::new();
        loading2.hash(&mut hasher);
        hasher.finish()
    };
    assert_eq!(loading1_hash, loading2_hash);

    let success1 = Async::success(42);
    let success2 = Async::success(42);
    let success1_hash = {
        let mut hasher = std::collections::hash_map::DefaultHasher::new();
        success1.hash(&mut hasher);
        hasher.finish()
    };
    let success2_hash = {
        let mut hasher = std::collections::hash_map::DefaultHasher::new();
        success2.hash(&mut hasher);
        hasher.finish()
    };
    assert_eq!(success1_hash, success2_hash);

    let fail1 = Async::fail(AsyncError::error("test"), Some(42));
    let fail2 = Async::fail(AsyncError::error("test"), Some(42));
    let fail1_hash = {
        let mut hasher = std::collections::hash_map::DefaultHasher::new();
        fail1.hash(&mut hasher);
        hasher.finish()
    };
    let fail2_hash = {
        let mut hasher = std::collections::hash_map::DefaultHasher::new();
        fail2.hash(&mut hasher);
        hasher.finish()
    };
    assert_eq!(fail1_hash, fail2_hash);

    assert_ne!(unintialized1_hash,loading1_hash);
    assert_ne!(unintialized1_hash, success1_hash);
    assert_ne!(unintialized1_hash, fail1_hash);
}

// Test factory methods
#[test]
fn test_fail_factory_methods() {
    // Test fail_with_error
    let fail = Async::fail_with_none(None::<i32>);
    assert_eq!(
        fail,
        Async::Fail {
            error: AsyncError::None,
            value: None
        }
    );
    // Test fail_with_cancelled
    let fail = Async::<i32>::fail_with_cancelled(Some(42));
    assert_eq!(
        fail,
        Async::Fail {
            error: AsyncError::Cancelled,
            value: Some(42)
        }
    );

    // Test fail_with_timeout
    let fail = Async::<i32>::fail_with_timeout(Some(42));
    assert_eq!(
        fail,
        Async::Fail {
            error: AsyncError::Timeout,
            value: Some(42)
        }
    );

    // Test fail_with_message
    let fail = Async::<i32>::fail_with_message("custom error", Some(42));
    assert_eq!(
        fail,
        Async::Fail {
            error: AsyncError::error("custom error"),
            value: Some(42)
        }
    );
}

// Test error state helpers
#[test]
fn test_error_state_helpers() {
    // Test is_fail_with_error
    let fail = Async::fail(AsyncError::error("error"), None::<i32>);
    assert!(fail.is_fail_with_error());

    let fail = Async::fail(AsyncError::None, None::<i32>);
    assert!(!fail.is_fail_with_error());

    // Test is_fail_with_none
    let fail = Async::fail(AsyncError::None, None::<i32>);
    assert!(fail.is_fail_with_none());

    let fail = Async::fail(AsyncError::error("error"), None::<i32>);
    assert!(!fail.is_fail_with_none());

    // Test is_fail_with_canceled
    let fail = Async::fail(AsyncError::Cancelled, None::<i32>);
    assert!(fail.is_fail_with_canceled());

    let fail = Async::fail(AsyncError::error("error"), None::<i32>);
    assert!(!fail.is_fail_with_canceled());

    // Test is_fail_with_timeout
    let fail = Async::fail(AsyncError::Timeout, None::<i32>);
    assert!(fail.is_fail_with_timeout());

    let fail = Async::fail(AsyncError::error("error"), None::<i32>);
    assert!(!fail.is_fail_with_timeout());
}

#[test]
fn test_value_ref_clone_from_async_ref() {
    // Test with Success
    let success = Async::success(42);
    let option: Option<i32> = success.value_ref_clone();
    assert_eq!(option, Some(42));

    // Test with Loading
    let loading = Async::loading(Some(42));
    let option: Option<i32> = loading.value_ref_clone();
    assert_eq!(option, Some(42));

    // Test with Loading (None)
    let loading = Async::loading(None::<i32>);
    let option: Option<i32> = loading.value_ref_clone();
    assert_eq!(option, None);

    // Test with Fail (with value)
    let fail = Async::fail(AsyncError::error("error"), Some(42));
    let option: Option<i32> = fail.value_ref_clone();
    assert_eq!(option, Some(42));

    // Test with Fail (without value)
    let fail = Async::fail(AsyncError::error("error"), None::<i32>);
    let option: Option<i32> = fail.value_ref_clone();
    assert_eq!(option, None);

    // Test with Uninitialized
    let uninitialized = Async::<i32>::Uninitialized;
    let option: Option<i32> = uninitialized.value_ref_clone();
    assert_eq!(option, None);
}

#[test]
fn test_set_retain_value() {
    // Test with Loading
    let mut loading = Async::loading(Some(42));
    loading = loading.set_retain_value(Some(100));
    assert!(loading.is_loading());
    assert_eq!(loading.value_ref(), Some(&100));

    // Test with Fail
    let mut fail = Async::fail(AsyncError::error("error"), Some(42));
    fail = fail.set_retain_value(Some(100));
    assert!(fail.is_fail());
    assert_eq!(fail.value_ref(), Some(&100));

    // Test with Success
    let mut success = Async::success(42);
    success = success.set_retain_value(Some(100));
    assert!(success.is_success());
    assert_eq!(success.value_ref(), Some(&42));

    // Test with Uninitialized
    let mut uninitialized: Async<i32> = Async::default();
    uninitialized = uninitialized.set_retain_value(Some(100));
    assert!(uninitialized.is_uninitialized());
    assert_eq!(uninitialized.value_ref(), None);
}

// Test complex state transitions
#[test]
fn test_complex_state_transitions() {
    // Create a complex type for testing
    #[derive(Clone, Debug, PartialEq)]
    struct User {
        id: i32,
        name: String,
    }

    // Start with uninitialized
    let mut state = Async::<User>::Uninitialized;
    assert!(state.is_incomplete());
    assert!(state.should_load());

    // Transition to loading
    let user = User {
        id: 1,
        name: "John".to_string(),
    };
    state = Async::loading(Some(user.clone()));
    assert!(state.is_incomplete());
    assert!(!state.should_load());
    assert!(state.is_loading());

    // Transition to success
    let updated_user = User {
        id: 1,
        name: "John Doe".to_string(),
    };
    state = Async::success(updated_user.clone());
    assert!(state.is_complete());
    assert!(!state.should_load());
    assert!(!state.is_incomplete());
    assert_eq!(state.value_ref(), Some(&updated_user));

    // Transition to fail with retained value
    state = Async::fail(
        AsyncError::error("Update failed"),
        Some(updated_user.clone()),
    );
    assert!(state.is_complete());
    assert!(state.should_load());
    assert!(!state.is_incomplete());
    assert_eq!(state.value_ref(), Some(&updated_user));

    // Transition back to loading
    state = Async::loading(None);
    assert!(state.is_incomplete());
    assert!(!state.should_load());
    assert!(state.is_loading());
    assert!(state.value_ref().is_none());
}