realizar 0.8.5

Pure Rust ML inference engine built from scratch - model serving for GGUF and safetensors

impl RequestCapture {
    /// Create new request capture
    #[must_use]
    pub fn new() -> Self {
        Self {
            input: String::new(),
            params: HashMap::new(),
        }
    }

    /// Set input
    #[must_use]
    pub fn with_input(mut self, input: &str) -> Self {
        self.input = input.to_string();
        self
    }

    /// Add parameter
    #[must_use]
    pub fn with_params(mut self, key: &str, value: &str) -> Self {
        self.params.insert(key.to_string(), value.to_string());
        self
    }

    /// Get input
    #[must_use]
    pub fn input(&self) -> &str {
        &self.input
    }

    /// Get params
    #[must_use]
    pub fn params(&self) -> &HashMap<String, String> {
        &self.params
    }

    /// Serialize to JSON
    #[must_use]
    pub fn to_json(&self) -> String {
        let params_json: Vec<String> = self
            .params
            .iter()
            .map(|(k, v)| format!("\"{}\":\"{}\"", k, v))
            .collect();
        format!(
            "{{\"input\":\"{}\",\"params\":{{{}}}}}",
            self.input,
            params_json.join(",")
        )
    }

    /// Deserialize from JSON (simple implementation)
    ///
    /// # Errors
    /// Returns error if JSON is malformed or missing input field.
    pub fn from_json(json: &str) -> std::result::Result<Self, &'static str> {
        // Simple extraction - production would use serde
        let input_start = json.find("\"input\":\"").ok_or("Missing input")?;
        let input_rest = &json[input_start + 9..];
        let input_end = input_rest.find('"').ok_or("Invalid input")?;
        let input = &input_rest[..input_end];

        Ok(Self {
            input: input.to_string(),
            params: HashMap::new(),
        })
    }
}

impl Default for RequestCapture {
    fn default() -> Self {
        Self::new()
    }
}

/// State dump for debugging (IMP-081)
#[derive(Debug, Clone)]
pub struct StateDump {
    error: String,
    stack_trace: String,
    state: HashMap<String, String>,
}

impl StateDump {
    /// Create new state dump
    #[must_use]
    pub fn new() -> Self {
        Self {
            error: String::new(),
            stack_trace: String::new(),
            state: HashMap::new(),
        }
    }

    /// Set error
    #[must_use]
    pub fn with_error(mut self, error: &str) -> Self {
        self.error = error.to_string();
        self
    }

    /// Set stack trace
    #[must_use]
    pub fn with_stack_trace(mut self, trace: &str) -> Self {
        self.stack_trace = trace.to_string();
        self
    }

    /// Add state
    #[must_use]
    pub fn with_state(mut self, key: &str, value: &str) -> Self {
        self.state.insert(key.to_string(), value.to_string());
        self
    }

    /// Get error
    #[must_use]
    pub fn error(&self) -> &str {
        &self.error
    }

    /// Get stack trace
    #[must_use]
    pub fn stack_trace(&self) -> &str {
        &self.stack_trace
    }

    /// Get state
    #[must_use]
    pub fn state(&self) -> &HashMap<String, String> {
        &self.state
    }

    /// Convert to JSON
    #[must_use]
    pub fn to_json(&self) -> String {
        let state_json: Vec<String> = self
            .state
            .iter()
            .map(|(k, v)| format!("\"{}\":\"{}\"", k, v))
            .collect();
        format!(
            "{{\"error\":\"{}\",\"stack_trace\":\"{}\",\"state\":{{{}}}}}",
            self.error,
            self.stack_trace.replace('\n', "\\n"),
            state_json.join(",")
        )
    }
}

impl Default for StateDump {
    fn default() -> Self {
        Self::new()
    }
}