ollama-oxide 0.2.0

A Rust library for integrating with Ollama's native API, providing low-level inference and high-level conveniences.
Documentation
//! Chat role enum for message authors.

use serde::{Deserialize, Serialize};

/// Role of a message in the chat conversation.
///
/// Determines who authored a particular message in the conversation history.
///
/// # Examples
///
/// ```no_run
/// use ollama_oxide::ChatRole;
///
/// let role = ChatRole::User;
/// assert_eq!(serde_json::to_string(&role).unwrap(), "\"user\"");
///
/// // Deserialize from JSON
/// let role: ChatRole = serde_json::from_str("\"assistant\"").unwrap();
/// assert_eq!(role, ChatRole::Assistant);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ChatRole {
    /// System message that sets behavior and context for the conversation.
    ///
    /// System messages are typically used to provide instructions to the model
    /// about how it should behave, what persona to adopt, or what constraints
    /// to follow.
    System,

    /// User message representing human input.
    ///
    /// These are messages from the end user interacting with the model.
    User,

    /// Assistant message representing model responses.
    ///
    /// These are messages generated by the model in previous turns of the
    /// conversation.
    Assistant,

    /// Tool message containing function call results.
    ///
    /// When the model requests a tool call and your application executes it,
    /// the result should be sent back as a tool message.
    Tool,
}

impl Default for ChatRole {
    /// Returns the default role, which is `User`.
    fn default() -> Self {
        Self::User
    }
}

impl std::fmt::Display for ChatRole {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ChatRole::System => write!(f, "system"),
            ChatRole::User => write!(f, "user"),
            ChatRole::Assistant => write!(f, "assistant"),
            ChatRole::Tool => write!(f, "tool"),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_chat_role_serialize() {
        assert_eq!(
            serde_json::to_string(&ChatRole::System).unwrap(),
            "\"system\""
        );
        assert_eq!(serde_json::to_string(&ChatRole::User).unwrap(), "\"user\"");
        assert_eq!(
            serde_json::to_string(&ChatRole::Assistant).unwrap(),
            "\"assistant\""
        );
        assert_eq!(serde_json::to_string(&ChatRole::Tool).unwrap(), "\"tool\"");
    }

    #[test]
    fn test_chat_role_deserialize() {
        assert_eq!(
            serde_json::from_str::<ChatRole>("\"system\"").unwrap(),
            ChatRole::System
        );
        assert_eq!(
            serde_json::from_str::<ChatRole>("\"user\"").unwrap(),
            ChatRole::User
        );
        assert_eq!(
            serde_json::from_str::<ChatRole>("\"assistant\"").unwrap(),
            ChatRole::Assistant
        );
        assert_eq!(
            serde_json::from_str::<ChatRole>("\"tool\"").unwrap(),
            ChatRole::Tool
        );
    }

    #[test]
    fn test_chat_role_default() {
        assert_eq!(ChatRole::default(), ChatRole::User);
    }

    #[test]
    fn test_chat_role_display() {
        assert_eq!(ChatRole::System.to_string(), "system");
        assert_eq!(ChatRole::User.to_string(), "user");
        assert_eq!(ChatRole::Assistant.to_string(), "assistant");
        assert_eq!(ChatRole::Tool.to_string(), "tool");
    }

    #[test]
    fn test_chat_role_equality() {
        assert_eq!(ChatRole::User, ChatRole::User);
        assert_ne!(ChatRole::User, ChatRole::Assistant);
    }

    #[test]
    fn test_chat_role_clone() {
        let role = ChatRole::Assistant;
        let cloned = role.clone();
        assert_eq!(role, cloned);
    }

    #[test]
    fn test_chat_role_copy() {
        let role = ChatRole::System;
        let copied: ChatRole = role; // Copy semantics
        assert_eq!(role, copied);
    }
}