iocaine 2.0.0

The deadliest poison known to AI
Documentation
// SPDX-FileCopyrightText: 2025 Gergely Nagy
// SPDX-FileContributor: Gergely Nagy
//
// SPDX-License-Identifier: MIT

use minijinja::{
    value::{Kwargs, Value},
    Error, State,
};
use rand_pcg::Pcg64;
use rand_seeder::Seeder;

#[derive(Debug)]
pub struct GobbledyGook {
    pub gen: Pcg64,
    pub min: u32,
    pub max: u32,
}

impl GobbledyGook {
    pub fn from(state: &State, options: &Kwargs, fallback_group: &str) -> Result<Self, Error> {
        let group = options
            .get::<Option<String>>("group")?
            .unwrap_or(fallback_group.to_string());
        let pos = options.get("pos")?;

        let temp = &format!("rng_pos_{}", group);

        let idx = if let Some(v) = pos {
            v
        } else {
            state
                .get_temp(temp)
                .unwrap_or_else(|| Value::from(0usize))
                .as_usize()
                .unwrap()
        };

        if pos.is_none() {
            state.set_temp(temp, Value::from(idx + 1));
        }

        let static_seed = state.lookup("static_seed").unwrap();
        let static_seed = static_seed.as_str().unwrap();

        let rng = Seeder::from(format!("iocaine://{}/{}#{}", static_seed, group, idx)).into_rng();

        Ok(Self {
            gen: rng,
            min: options.get::<Option<u32>>("min")?.unwrap_or(1),
            max: options.get("max")?,
        })
    }
}