iocaine 2.2.0

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

use roto::{Runtime, Val, roto_method, roto_static_method};
use serde::Serialize;
use std::fmt::Display;
use std::sync::Arc;

use crate::means_of_production::Error;

#[derive(Debug, Clone, Serialize, PartialEq, Default, Copy)]
#[serde(rename_all = "snake_case")]
#[serde(tag = "outcome")]
pub enum Outcome {
    #[default]
    Garbage,
    Challenge,
    NotForUs,
}

impl Display for Outcome {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let outcome = match self {
            Self::Garbage => "garbage",
            Self::Challenge => "challenge",
            Self::NotForUs => "not_for_us",
        };
        write!(f, "{outcome}")
    }
}

pub fn register_outcome(runtime: &mut Runtime) -> Result<(), Error> {
    runtime.register_copy_type_with_name::<Outcome>("Outcome", "Outcome of a decision")?;

    #[roto_static_method(runtime, Outcome)]
    fn garbage() -> Val<Outcome> {
        Outcome::Garbage.into()
    }
    #[roto_static_method(runtime, Outcome)]
    fn not_for_us() -> Val<Outcome> {
        Outcome::NotForUs.into()
    }
    #[roto_static_method(runtime, Outcome)]
    fn challenge() -> Val<Outcome> {
        Outcome::Challenge.into()
    }

    #[roto_method(runtime, Outcome)]
    fn to_string(outcome: Val<Outcome>) -> Arc<str> {
        Arc::from(serde_json::to_string(&outcome.0).unwrap())
    }

    Ok(())
}