@@ -10,6 +10,8 @@ pub struct SizeOption {
10
10
pub height : u32 ,
11
11
}
12
12
13
+ /// Holds the options that were passed to the cli with a flag
14
+ /// that are relevent for rendering the game.
13
15
#[ derive( Debug , Serialize , Deserialize , Clone ) ]
14
16
#[ serde( rename_all = "camelCase" ) ]
15
17
pub struct InitOptions {
@@ -62,6 +64,7 @@ impl fmt::Display for GameState {
62
64
}
63
65
}
64
66
67
+ /// Holds the state of the game at any time
65
68
#[ derive( Debug , Deserialize , Serialize , Clone ) ]
66
69
pub struct Game {
67
70
pub snake : Snake ,
@@ -70,11 +73,11 @@ pub struct Game {
70
73
pub state : GameState ,
71
74
}
72
75
73
- /**
74
- * Accepts the iterator from `stdin().lines()`
75
- * - parses the first line into `option`
76
- * - returns an iterator of the other lines in `lines` (already parsed)
77
- */
76
+ /// Accepts the iterator from `stdin().lines()`
77
+ /// - parses the first line into `options` as [InitOptions]
78
+ /// - returns an iterator of [Game] inside `lines` (already parsed)
79
+ ///
80
+ /// Used by [parse_gamestate] under the hood.
78
81
pub struct Stream {
79
82
pub options : InitOptions ,
80
83
pub lines : Box < dyn Iterator < Item = Game > > , // std::io::Lines<T>, //Lines<T>,
@@ -101,6 +104,27 @@ impl Stream {
101
104
}
102
105
}
103
106
107
+ /// Parses a gamestate streamed into stdin
108
+ /// Example:
109
+ /// ```
110
+ /// match parse_gamestate() {
111
+ /// Ok(stream) => {
112
+ /// println!(
113
+ /// "Frame duration {}, Snake length {}, Level {}x{}",
114
+ /// stream.options.frame_duration,
115
+ /// stream.options.snake_length,
116
+ /// stream.options.size.width,
117
+ /// stream.options.size.height
118
+ /// );
119
+ /// for parsed_line in stream.lines {
120
+ /// do_something(parsed_lined);
121
+ /// }
122
+ /// }
123
+ /// Err(e) => {
124
+ /// println!("Error occurred while parsing stdin: \"{}\"", e);
125
+ /// }
126
+ /// }
127
+ /// ```
104
128
pub fn parse_gamestate ( ) -> Result < Stream , Box < dyn std:: error:: Error > > {
105
129
let lines = stdin ( ) . lines ( ) ;
106
130
Stream :: new ( lines)
0 commit comments