Skip to main content

rsnaker/
lib.rs

1#![forbid(unsafe_code)]
2#![deny(clippy::all)]
3#![deny(clippy::pedantic)]
4#![allow(clippy::missing_panics_doc)]
5// Documentation for all Clippy lints: https://github.com/rust-lang/rust-clippy/
6//! # Snake Game using Ratatui
7//!
8//! This module implements a terminal-based snake game using the Ratatui crate for rendering.
9//!
10//! ## Features
11//! - **Terminal UI**: Uses Ratatui for rendering a grid-based game.
12//! - **Game Logic**: Manages snake movement, collisions, and scoring.
13//! - **Multithreading**: Uses multiple threads for input handling, rendering at 60 FPS, and game logic execution.
14//! - **Emoji-based graphics**: Supports rendering the snake using emojis instead of ASCII.
15//! - **Configurable parameters**: With `clap` for command-line arguments.
16//!
17//! ## TODO
18//! - [ ] Add a save score (local db) with a pseudo got from cmdline
19//! - [ ] Add some performance log with tracing for example
20//! - [ ] Fix too much life display outside of screen
21//!
22//!
23//! ## References
24//! - Clippy lints: <https://github.com/rust-lang/rust-clippy/>
25//! - Ratatui tutorial: <https://ratatui.rs/tutorials/hello-world/>
26//! - Example: <https://ratatui.rs/examples/widgets/canvas/>
27//!
28//! ## Architecture
29//! - Uses `RwLock` for synchronization.
30//! - Spawns separate threads for input handling, rendering (60Hz), and game logic execution.
31//!
32//! ## Documentation generation
33//! - `cargo doc --document-private-items --no-deps --open`
34//!
35//! ## Tests
36//!  - As usual run them with `cargo test` the project is set up with a lib containing all the code, and a main.rs just calling it
37//!  - As this is a widespread pattern providing full compliance with the Rust test ecosystem, allowing doc comment to be automatically tested, for example.
38
39pub mod controls;
40pub mod game_logic;
41pub mod graphics;
42
43use crate::game_logic::logger::log_configuration::{
44    DEFAULT_LOG_CONFIG_PATH, LogLevel, init_logger, update_log_level,
45};
46use crate::game_logic::playing_thread_manager::Game;
47use clap::Parser;
48use game_logic::game_options::GameOptions;
49use tracing::{debug, info};
50
51/// # Panics
52/// If bad characters (invalid size) are provided for snake body or head
53pub fn start_snake() {
54    // get command line options and parsed them to check for errors (auto using value parser in clap)
55    let mut args = GameOptions::parse();
56    //println!("{}", args.to_structured_toml());
57    //load args from the already saved preset if its user choice
58    if let Some(preset) = args.load {
59        args = GameOptions::load_from_toml_preset(preset).unwrap_or_else(|_| {
60            panic!("Fail to load Snake configuration file for preset {preset}")
61        });
62    }
63
64    // Initialize logger: configuration (level, file name, time format and all
65    // formatting booleans) is read from a dedicated TOML file inside `init_logger`.
66    let (_guard, log_config) = init_logger(Some(DEFAULT_LOG_CONFIG_PATH));
67    // CLI log level (if explicitly set, i.e., not Off) overrides the file level.
68    if args.log_level != LogLevel::Off {
69        update_log_level(args.log_level);
70    }
71    info!("Snake game arguments parsed and configuration is successful!");
72    debug!(log_config = ?log_config);
73    // If everything is OK, inits terminal for rendering
74    let terminal = ratatui::init();
75
76    // init our own Game engine and a display greeting screen
77    Game::menu(args, terminal);
78
79    //in all cases, restore
80    ratatui::restore();
81}