Prepare to make a template

This commit is contained in:
Sridhar Ratnakumar 2021-04-10 12:10:57 -04:00
parent 14ace3e43d
commit 88b0ffd668
6 changed files with 41 additions and 154 deletions

16
Cargo.lock generated
View file

@ -31,7 +31,6 @@ name = "bouncy"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"clap", "clap",
"toml",
] ]
[[package]] [[package]]
@ -64,12 +63,6 @@ version = "0.2.93"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9385f66bf6105b241aa65a61cb923ef20efc665cb9f9bb50ac2f0c4b7f378d41" checksum = "9385f66bf6105b241aa65a61cb923ef20efc665cb9f9bb50ac2f0c4b7f378d41"
[[package]]
name = "serde"
version = "1.0.125"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "558dc50e1a5a5fa7112ca2ce4effcb321b0300c0d4ccf0776a9f60cd89031171"
[[package]] [[package]]
name = "strsim" name = "strsim"
version = "0.8.0" version = "0.8.0"
@ -85,15 +78,6 @@ dependencies = [
"unicode-width", "unicode-width",
] ]
[[package]]
name = "toml"
version = "0.5.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a31142970826733df8241ef35dc040ef98c679ab14d7c3e54d827099b3acecaa"
dependencies = [
"serde",
]
[[package]] [[package]]
name = "unicode-width" name = "unicode-width"
version = "0.1.8" version = "0.1.8"

View file

@ -1,11 +1,10 @@
[package] [package]
name = "bouncy"
version = "0.1.0"
authors = ["Sridhar Ratnakumar <srid@srid.ca>"] authors = ["Sridhar Ratnakumar <srid@srid.ca>"]
edition = "2018" edition = "2018"
name = "bouncy"
version = "0.1.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
clap = "2.33.3" clap = "2.33.3"
toml = "0.5.8"

View file

@ -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 nix develop
# or just run directly # or just run directly
nix run
# or run via cargo
nix develop -c cargo run nix develop -c cargo run
# build
nix build
```
## Development (Legacy Nix)
```
# Dev shell
nix-shell
# run via cargo
nix-shell --run 'cargo run'
# build
nix-build
``` ```

View file

@ -49,11 +49,11 @@
}, },
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1617636226, "lastModified": 1617899217,
"narHash": "sha256-iZhBWrOR2DoDs1C+0FlnM9AQLMol/qoGQ+d+S43CKJM=", "narHash": "sha256-gd5JHH7IkeoIQ/oiGZSqDpGdGt7DMRJTQ8JiD8+hdOQ=",
"owner": "nixos", "owner": "nixos",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "3d1a7716d7f1fccbd7d30ab3b2ed3db831f43bde", "rev": "9e377a6ce42dccd9b624ae4ce8f978dc892ba0e2",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -93,11 +93,11 @@
"nixpkgs": "nixpkgs_2" "nixpkgs": "nixpkgs_2"
}, },
"locked": { "locked": {
"lastModified": 1617848844, "lastModified": 1618021386,
"narHash": "sha256-nhPuATSHRrzfZNjtl8zmMhZklYFCHiQd7+uf+jQbd+o=", "narHash": "sha256-jxuzRfJZs4+WqnbvduTOFBnPHM0CsusSV+plzP+QG9c=",
"owner": "oxalica", "owner": "oxalica",
"repo": "rust-overlay", "repo": "rust-overlay",
"rev": "70330a767d25daa6063e89e38d68b94b2d971831", "rev": "6642000a09ac2d0a1a8d91849e28a36f2c6c35cf",
"type": "github" "type": "github"
}, },
"original": { "original": {

View file

@ -2,7 +2,7 @@
# only `name` and `description` below. # only `name` and `description` below.
{ {
description = "..."; description = "My awesome Rust project";
inputs = { inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";

View file

@ -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() { fn main() {
let matches = App::new("bouncy") println!("Hello World!");
.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);
}
}
} }