This repository has been archived by the owner on Jul 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
70 lines (57 loc) · 2.16 KB
/
main.rs
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
use stated::online_shop::Customer;
fn main() {
// This enables the transition `Browsing` -> `Left` via `leave()`
let has_sudden_change_of_plan = false;
// This enables the transition `Shopping` -> `Browsing` via `clear_cart()`
let is_using_mums_credit_card = false;
// This enables the transition `Checkout` -> `Shopping` via `cancel_checkout()`
let forgot_my_wallet = false;
let catalogue: Vec<u8> = vec![20, 42, 36, 13, 71, 100];
let (first, rest_of_items) = catalogue.split_first().unwrap();
// Entry point of the flow: start with "Browsing".
let mut browsing = Customer::visit_site();
if has_sudden_change_of_plan {
// One possible transition from "Browsing".
browsing.leave();
return;
}
// The other possible transition from "Browsing".
let mut shopping = browsing.add_item(*first);
for item in rest_of_items {
// This is just some arbitrary logic to exhibit using both `add_item()`
// and `pop_item()`.
if item % 2 == 0 {
shopping = shopping.add_item(*item);
} else {
shopping = shopping.pop_item();
}
}
if is_using_mums_credit_card {
// One possible "ending" to the flow, via clearing the cart and just leaving.
browsing = shopping.clear_cart();
browsing.leave();
return;
}
// The other possible "ending" to the flow, where we actually proceed with
// checkout and then leave.
let checkout = shopping.proceed_to_checkout();
if forgot_my_wallet {
// This demonstrates another branch where instead of just going forwards,
// we backtrack.
shopping = checkout.cancel_checkout();
browsing = shopping.clear_cart();
browsing.leave();
return;
}
checkout.finalise_payment();
// This "default" flow results in this output:
// Hi site!
// Added 20 to cart ([20])
// Added 42 to cart ([20, 42])
// Added 36 to cart ([20, 42, 36])
// Removed 36 from cart ([20, 42])
// Removed 42 from cart ([20])
// Added 100 to cart ([20, 100])
// Proceeding to checkout.
// Done paying for the items, bye site!
}