Initial commit

This commit is contained in:
Daniel Flanagan 2024-04-11 11:14:37 -05:00
commit 62d902550e
10 changed files with 1861 additions and 0 deletions

2
.cargo/config.toml Normal file
View file

@ -0,0 +1,2 @@
[env]
RUST_BACKTRACE = "1"

1
.envrc Normal file
View file

@ -0,0 +1 @@
use flake

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
/target
/.direnv
/.pre-commit-config.yaml

1527
Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

26
Cargo.toml Normal file
View file

@ -0,0 +1,26 @@
[package]
name = "chatbot"
version = "0.1.0"
edition = "2021"
[dependencies]
anyhow = "1.0.82"
clap = "4.5.4"
color-eyre = "0.6.3"
discord = { git = "https://github.com/SpaceManiac/discord-rs" }
tokio = "1.37.0"
tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
[profile.dev]
opt-level = 1
[profile.dev.package."*"]
opt-level = 3
[profile.release]
strip = true
opt-level = "z"
lto = true
codegen-units = 1
panic = "abort"

138
flake.lock Normal file
View file

@ -0,0 +1,138 @@
{
"nodes": {
"flake-compat": {
"flake": false,
"locked": {
"lastModified": 1696426674,
"narHash": "sha256-kvjfFW7WAETZlt09AgDn1MrtKzP7t90Vf7vypd3OL1U=",
"owner": "edolstra",
"repo": "flake-compat",
"rev": "0f9255e01c2351cc7d116c072cb317785dd33b33",
"type": "github"
},
"original": {
"owner": "edolstra",
"repo": "flake-compat",
"type": "github"
}
},
"flake-utils": {
"inputs": {
"systems": "systems"
},
"locked": {
"lastModified": 1710146030,
"narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"git-hooks": {
"inputs": {
"flake-compat": "flake-compat",
"flake-utils": "flake-utils",
"gitignore": "gitignore",
"nixpkgs": [
"nixpkgs"
],
"nixpkgs-stable": "nixpkgs-stable"
},
"locked": {
"lastModified": 1712579741,
"narHash": "sha256-igpsH+pa6yFwYOdah3cFciCk8gw+ytniG9quf5f/q84=",
"owner": "cachix",
"repo": "git-hooks.nix",
"rev": "70f504012f0a132ac33e56988e1028d88a48855c",
"type": "github"
},
"original": {
"owner": "cachix",
"repo": "git-hooks.nix",
"type": "github"
}
},
"gitignore": {
"inputs": {
"nixpkgs": [
"git-hooks",
"nixpkgs"
]
},
"locked": {
"lastModified": 1709087332,
"narHash": "sha256-HG2cCnktfHsKV0s4XW83gU3F57gaTljL9KNSuG6bnQs=",
"owner": "hercules-ci",
"repo": "gitignore.nix",
"rev": "637db329424fd7e46cf4185293b9cc8c88c95394",
"type": "github"
},
"original": {
"owner": "hercules-ci",
"repo": "gitignore.nix",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1712608508,
"narHash": "sha256-vMZ5603yU0wxgyQeHJryOI+O61yrX2AHwY6LOFyV1gM=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "4cba8b53da471aea2ab2b0c1f30a81e7c451f4b6",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs-stable": {
"locked": {
"lastModified": 1710695816,
"narHash": "sha256-3Eh7fhEID17pv9ZxrPwCLfqXnYP006RKzSs0JptsN84=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "614b4613980a522ba49f0d194531beddbb7220d3",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixos-23.11",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"git-hooks": "git-hooks",
"nixpkgs": "nixpkgs"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
}
},
"root": "root",
"version": 7
}

77
flake.nix Normal file
View file

@ -0,0 +1,77 @@
{
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
inputs.git-hooks.url = "github:cachix/git-hooks.nix";
inputs.git-hooks.inputs.nixpkgs.follows = "nixpkgs";
outputs = {
self,
git-hooks,
nixpkgs,
}: let
inherit (self) outputs;
supportedSystems = [
"x86_64-linux"
"aarch64-linux"
"x86_64-darwin"
"aarch64-darwin"
];
forEachSupportedSystem = nixpkgs.lib.genAttrs supportedSystems;
in {
formatter = forEachSupportedSystem (system: nixpkgs.legacyPackages.${system}.alejandra);
checks = forEachSupportedSystem (system: {
pre-commit-check = git-hooks.lib.${system}.run {
src = ./.;
hooks = {
alejandra.enable = true;
clippy = {
enable = true;
};
rustfmt = {
enable = true;
};
cargo-test = {
enable = true;
name = "cargo test";
entry = "cargo test";
pass_filenames = false;
};
};
};
});
devShells = forEachSupportedSystem (system: let
pkgs = import nixpkgs {inherit system;};
in {
rust-dev = pkgs.mkShell {
inherit (outputs.checks.${system}.pre-commit-check) shellHook;
buildInputs = with pkgs;
[
cargo
rustc
rustfmt
rustPackages.clippy
rust-analyzer
# linker
clang
mold
# debugger
lldb
# libs
libopus
openssl
pkg-config
]
++ outputs.checks.${system}.pre-commit-check.enabledPackages;
};
default = outputs.devShells.${system}.rust-dev;
});
};
}

48
src/config.rs Normal file
View file

@ -0,0 +1,48 @@
use std::fmt::Debug;
use crate::prelude::*;
// pub static ref CONFIG: Config = Config::load_or_defaults();
pub struct Config {
pub version: u64,
pub secret: String,
}
impl Config {
/// Loads a configuration via the predetermined methods, overlaying a
/// default configuration. Returns any relevant errors that occurred
/// during loading to be reported later to the user after logging (or other
/// reporting faculties) have been initialized.
pub fn load_or_defaults() -> (Self, Option<Error>) {
let config = Self::defaults();
(config, None)
}
pub fn defaults() -> Self {
Self {
version: 1,
secret: "this is a secret!".to_owned(),
}
}
}
impl Debug for Config {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Config")
.field("version", &self.version)
.finish()
}
}
#[cfg(test)]
mod tests {
use crate::Config;
#[test]
fn config_does_not_leak_secrets() {
let defaults = Config::defaults();
let s = format!("{defaults:?}");
assert!(!s.contains("secret"));
assert!(!s.contains("this is a secret"));
}
}

33
src/main.rs Normal file
View file

@ -0,0 +1,33 @@
mod config;
mod prelude;
use tracing::level_filters::LevelFilter;
use tracing_subscriber::EnvFilter;
use crate::prelude::*;
fn main() {
let (conf, conf_err) = Config::load_or_defaults();
color_eyre::install().expect("Failed to install color_eyre");
let filter = EnvFilter::builder()
.with_default_directive(LevelFilter::TRACE.into())
.parse_lossy("info,chatbot=trace");
tracing_subscriber::fmt().with_env_filter(filter).init();
debug!("Configuration: {conf:?}");
if let Some(err) = conf_err {
warn!("Error loading configuration: {err}");
}
// TODO: tracing stuff
// TODO: load config
// TODO: oh yeah we need an HTTP server to handle minecraft server status stuff
// TODO: family reminders?
// TODO: connect to Discord
// TODO: handle messages
// TODO: data persistence? (sqlite? sled?)
info!("Hello, world!");
}

6
src/prelude.rs Normal file
View file

@ -0,0 +1,6 @@
#![allow(unused_imports)]
// pub use color_eyre;
pub use crate::config::Config;
pub use anyhow::Error;
pub use tracing::{debug, error, info, trace, warn};