forked from shramee/starklings-cairo1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
enums3.cairo
88 lines (74 loc) · 2.53 KB
/
enums3.cairo
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
// enums3.cairo
// Address all the TODOs to make the tests pass!
// Execute `starklings hint enums3` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
use debug::PrintTrait;
#[derive(Drop, Copy)]
enum Message { // TODO: implement the message variant types based on their usage below
}
#[derive(Drop, Copy)]
struct Point {
x: u8,
y: u8,
}
#[derive(Copy)]
struct State {
color: (u8, u8, u8),
position: Point,
quit: bool,
}
trait StateTrait {
fn change_color(ref self: State, new_color: (u8, u8, u8));
fn quit(ref self: State);
fn echo(ref self: State, s: felt252);
fn move_position(ref self: State, p: Point);
fn process(ref self: State, message: Message);
}
impl StateImpl of StateTrait {
fn change_color(ref self: State, new_color: (u8, u8, u8)) {
let State{color, position, quit, } = self;
self = State { color: new_color, position: position, quit: quit, };
}
fn quit(ref self: State) {
let State{color, position, quit, } = self;
self = State { color: color, position: position, quit: true, };
}
fn echo(ref self: State, s: felt252) {
s.print();
}
fn move_position(ref self: State, p: Point) {
let State{color, position, quit, } = self;
self = State { color: color, position: p, quit: quit, };
}
fn process(
ref self: State, message: Message
) { // TODO: create a match expression to process the different message variants
// Remember: When passing a tuple as a function argument, you'll need extra parentheses: fn function((t, u, p, l, e))
}
}
#[test]
fn test_match_message_call() {
let mut state = State {
quit: false, position: Point { x: 0_u8, y: 0_u8 }, color: (0_u8, 0_u8, 0_u8),
};
state.process(Message::ChangeColor((255_u8, 0_u8, 255_u8)));
state.process(Message::Echo('hello world'));
state.process(Message::Move(Point { x: 10_u8, y: 15_u8 }));
state.process(Message::Quit(()));
assert(state.color == (255_u8, 0_u8, 255_u8), 'wrong color');
assert(state.position.x == 10_u8, 'wrong x position');
assert(state.position.y == 15_u8, 'wrong y position');
assert(state.quit == true, 'quit should be true');
}
impl TripleTuplePartialEq of PartialEq::<(u8, u8, u8)> {
#[inline(always)]
fn eq(a: (u8, u8, u8), b: (u8, u8, u8)) -> bool {
let (a0, a1, a2) = a;
let (b0, b1, b2) = b;
a0 == b0 & a1 == b1 & a2 == b2
}
#[inline(always)]
fn ne(a: (u8, u8, u8), b: (u8, u8, u8)) -> bool {
!(a == b)
}
}