1use anyhow::Result;
2use std::{path::PathBuf, str::FromStr};
3
4#[derive(Debug, Default, Copy, Clone, PartialEq, PartialOrd)]
5pub enum Version {
6 #[default]
7 V1,
8}
9impl AsRef<str> for Version {
10 fn as_ref(&self) -> &'static str {
11 match self {
12 Version::V1 => "https://api.openai.com/v1/",
13 }
14 }
15}
16
17#[derive(Debug, Default, Copy, Clone, PartialEq, PartialOrd)]
18pub enum Function {
19 #[default]
20 CreateChatCompletion,
21 CreateImage,
22}
23
24impl AsRef<str> for Function {
25 fn as_ref(&self) -> &'static str {
26 match self {
27 Function::CreateChatCompletion => "chat/completions",
28 Function::CreateImage => "images/generations",
29 }
30 }
31}
32
33#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
34pub struct EntryPoint {
35 pub function: Function,
36 pub version: Version,
37}
38impl Default for EntryPoint {
39 fn default() -> Self {
40 Self {
41 function: Default::default(),
42 version: Default::default(),
43 }
44 }
45}
46impl EntryPoint {
47 pub fn set_function(mut self, function: Function) -> Self {
48 self.function = function;
49 self
50 }
51 pub fn set_version(mut self, version: Version) -> Self {
52 self.version = version;
53 self
54 }
55 pub fn path(&self) -> String {
56 let base_path = self.version.as_ref().to_string();
57 let function_path = self.function.as_ref().to_string();
58 format!("{base_path}{function_path}").to_string()
59 }
60}
61
62#[cfg(test)]
63mod tests {
64 use super::*;
65
66 #[test]
67 fn entry_point() -> Result<()> {
68 let entry_point = EntryPoint::default();
69 assert_eq!(
70 entry_point.path().as_str(),
71 "https://api.openai.com/v1/chat/completions"
72 );
73 assert_eq!(
74 entry_point
75 .set_function(Function::CreateImage)
76 .path()
77 .as_str(),
78 "https://api.openai.com/v1/images/generations"
79 );
80
81 Ok(())
82 }
83}