use std::path::{Path, PathBuf};
use rungbot_core::{Decision, Notices, State};
use serde::{Deserialize, Serialize};
pub const VERSION: u32 = 1;
#[derive(Serialize, Deserialize)]
struct OnDisk {
version: u32,
coins: State,
}
pub fn default_path() -> PathBuf {
if let Ok(p) = std::env::var("RUNGBOT_STATE") {
return PathBuf::from(p);
}
base_dir("XDG_STATE_HOME", ".local/state")
.join("rungbot")
.join("state.json")
}
pub fn default_config_path() -> PathBuf {
if let Ok(p) = std::env::var("RUNGBOT_CONFIG") {
return PathBuf::from(p);
}
base_dir("XDG_CONFIG_HOME", ".config")
.join("rungbot")
.join("watchlist.yaml")
}
pub fn default_research_dir() -> PathBuf {
if let Ok(p) = std::env::var("RUNGBOT_RESEARCH_DIR") {
return PathBuf::from(p);
}
base_dir("XDG_STATE_HOME", ".local/state")
.join("rungbot")
.join("research")
}
fn base_dir(env_key: &str, fallback: &str) -> PathBuf {
match std::env::var(env_key) {
Ok(v) if !v.trim().is_empty() => PathBuf::from(v),
_ => home().join(fallback),
}
}
fn home() -> PathBuf {
std::env::var("HOME")
.map(PathBuf::from)
.unwrap_or_else(|_| PathBuf::from("."))
}
pub fn load(path: &Path) -> State {
if !path.exists() {
return State::new();
}
let raw = match std::fs::read_to_string(path) {
Ok(r) => r,
Err(e) => {
eprintln!(
"WARN: unreadable state at {} ({e}); starting cold",
path.display()
);
return State::new();
}
};
match serde_json::from_str::<OnDisk>(&raw) {
Ok(d) => d.coins,
Err(_) => serde_json::from_str::<State>(&raw).unwrap_or_else(|e| {
eprintln!(
"WARN: unparseable state at {} ({e}); starting cold",
path.display()
);
State::new()
}),
}
}
pub fn save(path: &Path, state: &State) {
let body = match serde_json::to_string_pretty(&OnDisk {
version: VERSION,
coins: state.clone(),
}) {
Ok(b) => b,
Err(e) => {
eprintln!("WARN: could not serialise state: {e}");
return;
}
};
if let Some(parent) = path.parent() {
if let Err(e) = std::fs::create_dir_all(parent) {
eprintln!("WARN: could not create {}: {e}", parent.display());
return;
}
}
let tmp = path.with_extension("tmp");
if let Err(e) = std::fs::write(&tmp, body) {
eprintln!("WARN: could not write state to {}: {e}", tmp.display());
return;
}
if let Err(e) = std::fs::rename(&tmp, path) {
eprintln!("WARN: could not replace {}: {e}", path.display());
}
}
pub fn decisions_path(state_path: &Path) -> PathBuf {
state_path.with_file_name("decisions.jsonl")
}
pub fn notices_path(state_path: &Path) -> PathBuf {
state_path.with_file_name("notices.json")
}
pub fn append_decisions(path: &Path, decisions: &[Decision]) {
if decisions.is_empty() {
return;
}
use std::io::Write;
if let Some(parent) = path.parent() {
let _ = std::fs::create_dir_all(parent);
}
let mut body = String::new();
for d in decisions {
match serde_json::to_string(d) {
Ok(line) => {
body.push_str(&line);
body.push('\n');
}
Err(e) => eprintln!("WARN: could not serialise a decision: {e}"),
}
}
match std::fs::OpenOptions::new()
.create(true)
.append(true)
.open(path)
{
Ok(mut f) => {
if let Err(e) = f.write_all(body.as_bytes()) {
eprintln!("WARN: could not append to {}: {e}", path.display());
}
}
Err(e) => eprintln!("WARN: could not open {}: {e}", path.display()),
}
}
pub fn load_notices(path: &Path) -> Notices {
std::fs::read_to_string(path)
.ok()
.and_then(|s| serde_json::from_str(&s).ok())
.unwrap_or_default()
}
pub fn save_notices(path: &Path, n: &Notices) {
if let Some(parent) = path.parent() {
let _ = std::fs::create_dir_all(parent);
}
match serde_json::to_string_pretty(n) {
Ok(body) => {
if let Err(e) = std::fs::write(path, body) {
eprintln!("WARN: could not write {}: {e}", path.display());
}
}
Err(e) => eprintln!("WARN: could not serialise notices: {e}"),
}
}