@@ -10,6 +10,8 @@ pub struct SizeOption {
1010 pub height : u32 ,
1111}
1212
13+ /// Holds the options that were passed to the cli with a flag
14+ /// that are relevent for rendering the game.
1315#[ derive( Debug , Serialize , Deserialize , Clone ) ]
1416#[ serde( rename_all = "camelCase" ) ]
1517pub struct InitOptions {
@@ -62,6 +64,7 @@ impl fmt::Display for GameState {
6264 }
6365}
6466
67+ /// Holds the state of the game at any time
6568#[ derive( Debug , Deserialize , Serialize , Clone ) ]
6669pub struct Game {
6770 pub snake : Snake ,
@@ -70,11 +73,11 @@ pub struct Game {
7073 pub state : GameState ,
7174}
7275
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.
7881pub struct Stream {
7982 pub options : InitOptions ,
8083 pub lines : Box < dyn Iterator < Item = Game > > , // std::io::Lines<T>, //Lines<T>,
@@ -101,6 +104,27 @@ impl Stream {
101104 }
102105}
103106
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+ /// ```
104128pub fn parse_gamestate ( ) -> Result < Stream , Box < dyn std:: error:: Error > > {
105129 let lines = stdin ( ) . lines ( ) ;
106130 Stream :: new ( lines)
0 commit comments