1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use anyhow::Result;
use std::{path::PathBuf, str::FromStr};

#[derive(Debug, Default, Copy, Clone, PartialEq, PartialOrd)]
pub enum Version {
    #[default]
    V1,
}
impl AsRef<str> for Version {
    fn as_ref(&self) -> &'static str {
        match self {
            Version::V1 => "https://api.openai.com/v1/",
        }
    }
}

#[derive(Debug, Default, Copy, Clone, PartialEq, PartialOrd)]
pub enum Function {
    #[default]
    CreateChatCompletion,
    CreateImage,
}

impl AsRef<str> for Function {
    fn as_ref(&self) -> &'static str {
        match self {
            Function::CreateChatCompletion => "chat/completions",
            Function::CreateImage => "images/generations",
        }
    }
}

#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
pub struct EntryPoint {
    pub function: Function,
    pub version: Version,
}
impl Default for EntryPoint {
    fn default() -> Self {
        Self {
            function: Default::default(),
            version: Default::default(),
        }
    }
}
impl EntryPoint {
    pub fn set_function(mut self, function: Function) -> Self {
        self.function = function;
        self
    }
    pub fn set_version(mut self, version: Version) -> Self {
        self.version = version;
        self
    }
    pub fn path(&self) -> String {
        let base_path = self.version.as_ref().to_string();
        let function_path = self.function.as_ref().to_string();
        format!("{base_path}{function_path}").to_string()
    }
}

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

    #[test]
    fn entry_point() -> Result<()> {
        let entry_point = EntryPoint::default();
        assert_eq!(
            entry_point.path().as_str(),
            "https://api.openai.com/v1/chat/completions"
        );
        assert_eq!(
            entry_point
                .set_function(Function::CreateImage)
                .path()
                .as_str(),
            "https://api.openai.com/v1/images/generations"
        );

        Ok(())
    }
}