openai_client_cli/program/loaders/
method.rs1use crate::{Entry, Error, Result, traits::*};
2use std::str::FromStr;
3use tracing::{debug, info};
4
5pub struct Method(http::Method);
7
8impl Method {
9 pub const CONNECT: Method = Method(http::Method::CONNECT);
11 pub const DELETE: Method = Method(http::Method::DELETE);
13 pub const GET: Method = Method(http::Method::GET);
17 pub const HEAD: Method = Method(http::Method::HEAD);
19 pub const OPTIONS: Method = Method(http::Method::OPTIONS);
21 pub const PATCH: Method = Method(http::Method::PATCH);
23 pub const POST: Method = Method(http::Method::POST);
25 pub const PUT: Method = Method(http::Method::PUT);
27 pub const TRACE: Method = Method(http::Method::TRACE);
29
30 fn post_fetch_ok(self, source: &str) -> Result<Self> {
31 info!(
32 "Successfully fetched the API request method from {}: {:?}",
33 source, self.value_ref(),
34 );
35 Ok(self)
36 }
37}
38
39impl FromStr for Method {
40 type Err = Error;
41
42 fn from_str(name: &str) -> Result<Self> {
43 Ok(Self(http::Method::from_str(name)?))
44 }
45}
46
47impl Loader<http::Method> for Method {
48 fn fetch(entry: &Entry) -> Result<Self> {
49 let source_ok = "the program arguments";
50 match entry.method
51 .as_ref()
52 .ok_or(Error::msg("Not provided"))
53 .and_then(Method::try_from)
54 {
55 Ok(method) => method.post_fetch_ok(source_ok),
56 Err(err) => {
57 debug!("Failed to obtain the API request method from {source_ok}: {err:?}");
58 let (method, dep_status) = if entry._parameter.is_some() {
59 (Self::POST, "")
60 } else {
61 (Self::GET, "un")
62 };
63 debug!("The API request parameters were fetched {dep_status}successfully");
64 method.post_fetch_ok("the fallback options")
65 },
66 }
67 }
68 fn value(self) -> http::Method {
69 self.0
70 }
71 fn value_ref(&self) -> &http::Method {
72 &self.0
73 }
74}
75
76impl From<http::Method> for Method {
77 fn from(method: http::Method) -> Self {
78 Self(method)
79 }
80}
81
82impl From<&http::Method> for Method {
83 fn from(method: &http::Method) -> Self {
84 Self(method.clone())
85 }
86}
87
88impl TryFrom<&str> for Method {
89 type Error = Error;
90
91 fn try_from(text: &str) -> Result<Self> {
92 Self::from_str(text)
93 }
94}
95
96impl TryFrom<String> for Method {
97 type Error = Error;
98
99 fn try_from(text: String) -> Result<Self> {
100 Self::from_str(&text)
101 }
102}
103
104impl TryFrom<&String> for Method {
105 type Error = Error;
106
107 fn try_from(text: &String) -> Result<Self> {
108 Self::from_str(text)
109 }
110}