ragit_pdl/
role.rs

1use crate::error::Error;
2use std::str::FromStr;
3
4#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
5pub enum PdlRole {
6    User,
7    System,
8    Assistant,
9    Schema,
10    Reasoning,
11}
12
13impl From<&str> for PdlRole {
14    fn from(s: &str) -> PdlRole {
15        match s {
16            "user" => PdlRole::User,
17            "system" => PdlRole::System,
18            "assistant" => PdlRole::Assistant,
19            "schema" => PdlRole::Schema,
20            "reasoning" => PdlRole::Reasoning,
21            _ => unreachable!(),
22        }
23    }
24}
25
26#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
27pub enum Role {
28    User,
29    System,
30    Assistant,
31    Reasoning,
32}
33
34impl Role {
35    // google api uses different terms. I'd really want it to
36    // take `ApiProvider` as an input, but it cannot. It's my
37    // mistake to separate ragit-api crate and ragit-pdl crate.
38    pub fn to_api_string(&self, google: bool) -> &'static str {
39        match self {
40            Role::User => "user",
41            Role::System => "system",
42            Role::Assistant => if google { "model" } else { "assistant" },
43            Role::Reasoning => "reasoning",
44        }
45    }
46}
47
48impl FromStr for Role {
49    type Err = Error;
50
51    fn from_str(s: &str) -> Result<Role, Error> {
52        match s.to_ascii_lowercase().as_str() {
53            "user" => Ok(Role::User),
54            "system" => Ok(Role::System),
55            "reasoning" => Ok(Role::Reasoning),
56
57            "model"  // google ai
58            | "chatbot"  // legacy cohere api
59            | "assistant" => Ok(Role::Assistant),
60            _ => Err(Error::InvalidRole(s.to_string())),
61        }
62    }
63}
64
65impl From<PdlRole> for Role {
66    fn from(r: PdlRole) -> Role {
67        match r {
68            PdlRole::User => Role::User,
69            PdlRole::System => Role::System,
70            PdlRole::Assistant => Role::Assistant,
71            PdlRole::Reasoning => Role::Reasoning,
72            PdlRole::Schema => unreachable!(),
73        }
74    }
75}