From 88b0ffd668fbdfb6d9d92699e4e78554ad55f415 Mon Sep 17 00:00:00 2001 From: Sridhar Ratnakumar Date: Sat, 10 Apr 2021 12:10:57 -0400 Subject: [PATCH] Prepare to make a template --- Cargo.lock | 16 ------- Cargo.toml | 5 +-- README.md | 33 ++++++++++++-- flake.lock | 12 ++--- flake.nix | 2 +- src/main.rs | 127 +--------------------------------------------------- 6 files changed, 41 insertions(+), 154 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f26b390..39ea5bc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -31,7 +31,6 @@ name = "bouncy" version = "0.1.0" dependencies = [ "clap", - "toml", ] [[package]] @@ -64,12 +63,6 @@ version = "0.2.93" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9385f66bf6105b241aa65a61cb923ef20efc665cb9f9bb50ac2f0c4b7f378d41" -[[package]] -name = "serde" -version = "1.0.125" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "558dc50e1a5a5fa7112ca2ce4effcb321b0300c0d4ccf0776a9f60cd89031171" - [[package]] name = "strsim" version = "0.8.0" @@ -85,15 +78,6 @@ dependencies = [ "unicode-width", ] -[[package]] -name = "toml" -version = "0.5.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a31142970826733df8241ef35dc040ef98c679ab14d7c3e54d827099b3acecaa" -dependencies = [ - "serde", -] - [[package]] name = "unicode-width" version = "0.1.8" diff --git a/Cargo.toml b/Cargo.toml index 256aeda..e4c3e94 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,11 +1,10 @@ [package] -name = "bouncy" -version = "0.1.0" authors = ["Sridhar Ratnakumar "] edition = "2018" +name = "bouncy" +version = "0.1.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] clap = "2.33.3" -toml = "0.5.8" \ No newline at end of file diff --git a/README.md b/README.md index c5d8c51..d8a3153 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,36 @@ -Requires Nix Flakes. +A template Rust project with fully functional and no-frills Nix support, as well as builtin VSCode configuration to get IDE support without doing anything (open in VSCode and accept the suggestions). + +## Adapting this template + +Change `name` in Cargo.toml and flake.nix. Also change `description` in flake.nix. + +## Development (Flakes) + +This repo uses [Flakes](https://nixos.wiki/wiki/Flakes) from the get-go, but compat is provided for traditional nix-shell/nix-build as well (see the section below). ``` -# dev shell +# Dev shell nix develop # or just run directly +nix run + +# or run via cargo nix develop -c cargo run -``` \ No newline at end of file + +# build +nix build +``` + +## Development (Legacy Nix) + +``` +# Dev shell +nix-shell + +# run via cargo +nix-shell --run 'cargo run' + +# build +nix-build +``` diff --git a/flake.lock b/flake.lock index 7a153b1..6dc9c77 100644 --- a/flake.lock +++ b/flake.lock @@ -49,11 +49,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1617636226, - "narHash": "sha256-iZhBWrOR2DoDs1C+0FlnM9AQLMol/qoGQ+d+S43CKJM=", + "lastModified": 1617899217, + "narHash": "sha256-gd5JHH7IkeoIQ/oiGZSqDpGdGt7DMRJTQ8JiD8+hdOQ=", "owner": "nixos", "repo": "nixpkgs", - "rev": "3d1a7716d7f1fccbd7d30ab3b2ed3db831f43bde", + "rev": "9e377a6ce42dccd9b624ae4ce8f978dc892ba0e2", "type": "github" }, "original": { @@ -93,11 +93,11 @@ "nixpkgs": "nixpkgs_2" }, "locked": { - "lastModified": 1617848844, - "narHash": "sha256-nhPuATSHRrzfZNjtl8zmMhZklYFCHiQd7+uf+jQbd+o=", + "lastModified": 1618021386, + "narHash": "sha256-jxuzRfJZs4+WqnbvduTOFBnPHM0CsusSV+plzP+QG9c=", "owner": "oxalica", "repo": "rust-overlay", - "rev": "70330a767d25daa6063e89e38d68b94b2d971831", + "rev": "6642000a09ac2d0a1a8d91849e28a36f2c6c35cf", "type": "github" }, "original": { diff --git a/flake.nix b/flake.nix index 088f803..59b6f45 100644 --- a/flake.nix +++ b/flake.nix @@ -2,7 +2,7 @@ # only `name` and `description` below. { - description = "..."; + description = "My awesome Rust project"; inputs = { nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; diff --git a/src/main.rs b/src/main.rs index e5af86a..47ad8c6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,126 +1,3 @@ -use std::fmt::{Display, Formatter}; -use clap::{App, SubCommand}; - - -enum VertDir { - Up, - Down, -} - -enum HorizDir { - Left, - Right, -} - -struct Ball { - x: u32, - y: u32, - vert_dir: VertDir, - horiz_dir: HorizDir, -} - -struct Frame { - width: u32, - height: u32, -} - -struct Game { - frame: Frame, - ball: Ball, -} - -impl Game { - fn new() -> Game { - let frame = Frame { - width: 60, - height: 30, - }; - let ball = Ball { - x: 2, - y: 4, - vert_dir: VertDir::Up, - horiz_dir: HorizDir::Left, - }; - Game {frame, ball} - } - - fn step(&mut self) { - self.ball.bounce(&self.frame); - self.ball.mv(); - } -} - -impl Ball { - fn bounce(&mut self, frame: &Frame) { - if self.x == 0 { - self.horiz_dir = HorizDir::Right; - } else if self.x == frame.width - 1 { - self.horiz_dir = HorizDir::Left; - } - - if self.y == 0 { - self.vert_dir = VertDir::Down; - } else if self.y == frame.height - 1 { - self.vert_dir = VertDir::Up; - } - } - - fn mv(&mut self) { - match self.horiz_dir { - HorizDir::Left => self.x -= 1, - HorizDir::Right => self.x += 1, - } - match self.vert_dir { - VertDir::Up => self.y -= 1, - VertDir::Down => self.y += 1, - } - } -} - -impl Display for Game { - fn fmt(&self, fmt: &mut Formatter) -> std::fmt::Result { - let top_bottom = |fmt: &mut Formatter| { - write!(fmt, "+"); - for _ in 0..self.frame.width { - write!(fmt, "-"); - } - write!(fmt, "+\n") - }; - - top_bottom(fmt)?; - - for row in 0..self.frame.height { - write!(fmt, "|"); - - for column in 0..self.frame.width { - let c = if row == self.ball.y && column == self.ball.x { - 'o' - } else { - ' ' - }; - write!(fmt, "{}", c); - } - - write!(fmt, "|\n"); - } - - top_bottom(fmt) - } -} - -fn main () { - let matches = App::new("bouncy") - .version("1.0") - .subcommand(SubCommand::with_name("ping")).get_matches(); - if let Some(_matches) = matches.subcommand_matches("ping") { - println!("pong") - } else { - let mut game = Game::new(); - let sleep_duration = std::time::Duration::from_millis(33); - loop { - println!("{}", game); - game.step(); - std::thread::sleep(sleep_duration); - } - } +fn main() { + println!("Hello World!"); }