Use oneshot functions instead of File handle
parent
410740ea21
commit
7ea944cb0a
|
@ -17,15 +17,6 @@ dependencies = [
|
|||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ansi_term"
|
||||
version = "0.12.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2"
|
||||
dependencies = [
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anyhow"
|
||||
version = "1.0.69"
|
||||
|
@ -59,6 +50,17 @@ dependencies = [
|
|||
"webpki-roots",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "atty"
|
||||
version = "0.2.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
|
||||
dependencies = [
|
||||
"hermit-abi 0.1.19",
|
||||
"libc",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.1.0"
|
||||
|
@ -109,8 +111,8 @@ dependencies = [
|
|||
"serde",
|
||||
"tokio",
|
||||
"tracing",
|
||||
"tracing-forest",
|
||||
"tracing-subscriber",
|
||||
"tracing-tree",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -144,12 +146,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
checksum = "16b0a3d9ed01224b22057780a37bb8c5dbfe1be8ba48678e7bf57ec4b385411f"
|
||||
dependencies = [
|
||||
"iana-time-zone",
|
||||
"js-sys",
|
||||
"num-integer",
|
||||
"num-traits",
|
||||
"serde",
|
||||
"time 0.1.45",
|
||||
"wasm-bindgen",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
|
@ -440,7 +439,7 @@ checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31"
|
|||
dependencies = [
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"wasi 0.11.0+wasi-snapshot-preview1",
|
||||
"wasi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -468,6 +467,15 @@ version = "0.12.3"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
|
||||
|
||||
[[package]]
|
||||
name = "hermit-abi"
|
||||
version = "0.1.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hermit-abi"
|
||||
version = "0.2.6"
|
||||
|
@ -707,7 +715,7 @@ checksum = "5b9d9a46eff5b4ff64b45a9e316a6d1e0bc719ef429cbec4dc630684212bfdf9"
|
|||
dependencies = [
|
||||
"libc",
|
||||
"log",
|
||||
"wasi 0.11.0+wasi-snapshot-preview1",
|
||||
"wasi",
|
||||
"windows-sys 0.45.0",
|
||||
]
|
||||
|
||||
|
@ -746,7 +754,7 @@ version = "1.15.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0fac9e2da13b5eb447a6ce3d392f23a29d8694bff781bf03a16cd9ac8697593b"
|
||||
dependencies = [
|
||||
"hermit-abi",
|
||||
"hermit-abi 0.2.6",
|
||||
"libc",
|
||||
]
|
||||
|
||||
|
@ -1131,7 +1139,7 @@ dependencies = [
|
|||
"serde",
|
||||
"serde-value",
|
||||
"serde_json",
|
||||
"time 0.3.20",
|
||||
"time",
|
||||
"tokio",
|
||||
"tracing",
|
||||
"typemap_rev",
|
||||
|
@ -1254,17 +1262,6 @@ dependencies = [
|
|||
"once_cell",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "time"
|
||||
version = "0.1.45"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1b797afad3f312d1c66a56d11d0316f916356d11bd158fbc6ca6389ff6bf805a"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"wasi 0.10.0+wasi-snapshot-preview1",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "time"
|
||||
version = "0.3.20"
|
||||
|
@ -1403,23 +1400,6 @@ dependencies = [
|
|||
"valuable",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tracing-forest"
|
||||
version = "0.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "119324027fc01804d9f83aefb7d80fda2e8fbe7c28e0acc59187cbd751a12915"
|
||||
dependencies = [
|
||||
"ansi_term",
|
||||
"chrono",
|
||||
"serde",
|
||||
"smallvec",
|
||||
"thiserror",
|
||||
"tokio",
|
||||
"tracing",
|
||||
"tracing-subscriber",
|
||||
"uuid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tracing-log"
|
||||
version = "0.1.3"
|
||||
|
@ -1438,15 +1418,25 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
checksum = "a6176eae26dd70d0c919749377897b54a9276bd7061339665dd68777926b5a70"
|
||||
dependencies = [
|
||||
"matchers",
|
||||
"nu-ansi-term",
|
||||
"once_cell",
|
||||
"regex",
|
||||
"sharded-slab",
|
||||
"smallvec",
|
||||
"thread_local",
|
||||
"tracing",
|
||||
"tracing-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tracing-tree"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "758e983ab7c54fee18403994507e7f212b9005e957ce7984996fac8d11facedb"
|
||||
dependencies = [
|
||||
"atty",
|
||||
"nu-ansi-term",
|
||||
"tracing-core",
|
||||
"tracing-log",
|
||||
"tracing-subscriber",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -1548,16 +1538,6 @@ version = "0.7.6"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9"
|
||||
|
||||
[[package]]
|
||||
name = "uuid"
|
||||
version = "1.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1674845326ee10d37ca60470760d4288a6f80f304007d92e5c53bab78c9cfd79"
|
||||
dependencies = [
|
||||
"getrandom",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "valuable"
|
||||
version = "0.1.0"
|
||||
|
@ -1580,12 +1560,6 @@ dependencies = [
|
|||
"try-lock",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasi"
|
||||
version = "0.10.0+wasi-snapshot-preview1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f"
|
||||
|
||||
[[package]]
|
||||
name = "wasi"
|
||||
version = "0.11.0+wasi-snapshot-preview1"
|
||||
|
|
|
@ -13,5 +13,5 @@ ron = "0.8.0"
|
|||
serde = { version = "1.0.152", features = ["derive"] }
|
||||
tokio = { version = "1.25.0", features = ["full"] }
|
||||
tracing = "0.1.37"
|
||||
tracing-forest = { version = "0.1.5", features = ["full"] }
|
||||
tracing-subscriber = "0.3.16"
|
||||
tracing-subscriber = { version = "0.3.16", default-features = false, features = ["std", "env-filter", "registry"] }
|
||||
tracing-tree = "0.2.2"
|
||||
|
|
|
@ -1,16 +1,14 @@
|
|||
use std::collections::HashMap;
|
||||
use std::path::Path;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use anyhow::{Error, Result};
|
||||
use poise::serenity_prelude as serenity;
|
||||
use rand::seq::SliceRandom;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tokio::fs::{File, OpenOptions};
|
||||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
/// a brain stores data for a single guild
|
||||
#[derive(Debug, Default, Serialize, Deserialize)]
|
||||
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
||||
pub struct Brain {
|
||||
facts: HashMap<String, String>,
|
||||
}
|
||||
|
@ -18,21 +16,14 @@ pub struct Brain {
|
|||
/// top level data
|
||||
#[derive(Debug)]
|
||||
pub struct Data {
|
||||
file: Mutex<File>,
|
||||
path: PathBuf,
|
||||
brain: Mutex<HashMap<serenity::GuildId, Brain>>,
|
||||
}
|
||||
|
||||
impl Data {
|
||||
#[tracing::instrument(fields(path = %path.as_ref().display()))]
|
||||
pub async fn from_path(path: impl AsRef<Path>) -> Result<Data> {
|
||||
let mut file = OpenOptions::new()
|
||||
.read(true)
|
||||
.write(true)
|
||||
.create(true)
|
||||
.open(&path)
|
||||
.await?;
|
||||
let mut contents = String::new();
|
||||
file.read_to_string(&mut contents).await?;
|
||||
let contents = tokio::fs::read_to_string(&path).await?;
|
||||
tracing::debug!("loaded brain contents: {contents}");
|
||||
let brain = if contents.is_empty() {
|
||||
tracing::info!("initializing brain at {}", path.as_ref().display());
|
||||
|
@ -42,8 +33,8 @@ impl Data {
|
|||
tracing::info!("loaded brain from {}", path.as_ref().display());
|
||||
Mutex::new(brain)
|
||||
};
|
||||
let file = Mutex::new(file);
|
||||
Ok(Data { file, brain })
|
||||
let path = path.as_ref().to_path_buf();
|
||||
Ok(Data { path, brain })
|
||||
}
|
||||
|
||||
#[tracing::instrument]
|
||||
|
@ -56,9 +47,11 @@ impl Data {
|
|||
&mut contents,
|
||||
Some(Default::default()),
|
||||
)?)?;
|
||||
let mut file = self.file.lock().await;
|
||||
file.set_len(0).await?;
|
||||
file.write_all(&contents).await?;
|
||||
tracing::debug!(
|
||||
"committing brain contents: {}",
|
||||
String::from_utf8_lossy(&contents)
|
||||
);
|
||||
tokio::fs::write(&self.path, &contents).await?;
|
||||
tracing::info!("committed brain to disk");
|
||||
Ok(())
|
||||
}
|
||||
|
|
21
src/main.rs
21
src/main.rs
|
@ -2,8 +2,10 @@ mod commands;
|
|||
|
||||
use anyhow::{Error, Result};
|
||||
use poise::serenity_prelude as serenity;
|
||||
use tracing_forest::{util::EnvFilter, ForestLayer};
|
||||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, Registry};
|
||||
use tracing_subscriber::layer::SubscriberExt;
|
||||
use tracing_subscriber::util::SubscriberInitExt;
|
||||
use tracing_subscriber::{EnvFilter, Registry};
|
||||
use tracing_tree::HierarchicalLayer;
|
||||
|
||||
const DISCORD_TOKEN: &str = "DISCORD_TOKEN";
|
||||
const BUSCEMI_DATA: &str = "BUSCEMI_DATA";
|
||||
|
@ -21,7 +23,7 @@ async fn main() -> Result<()> {
|
|||
fn init_tracing() {
|
||||
Registry::default()
|
||||
.with(EnvFilter::from_default_env())
|
||||
.with(ForestLayer::default())
|
||||
.with(HierarchicalLayer::default())
|
||||
.init()
|
||||
}
|
||||
|
||||
|
@ -60,11 +62,16 @@ fn init_discord() -> poise::FrameworkBuilder<commands::Data, Error> {
|
|||
.setup(|ctx, ready, framework| {
|
||||
Box::pin(async move {
|
||||
for guild in &ready.guilds {
|
||||
poise::builtins::register_in_guild(ctx, &framework.options().commands, guild.id)
|
||||
.await
|
||||
.unwrap_or_else(|e| tracing::error!("failed to register commands: {e}"))
|
||||
poise::builtins::register_in_guild(
|
||||
ctx,
|
||||
&framework.options().commands,
|
||||
guild.id,
|
||||
)
|
||||
.await
|
||||
.unwrap_or_else(|e| tracing::error!("failed to register commands: {e}"));
|
||||
tracing::debug!("registered commands on {guild:?}")
|
||||
}
|
||||
tracing::debug!("registered commands");
|
||||
tracing::info!("registered commands");
|
||||
let data_path =
|
||||
std::env::var(BUSCEMI_DATA).unwrap_or_else(|_| String::from(BUSCEMI_DATA_PATH));
|
||||
commands::Data::from_path(data_path).await
|
||||
|
|
Loading…
Reference in New Issue