1use crate::capability::{CAP_HTTP, HTTP_DELETE, HTTP_GET, HTTP_PATCH, HTTP_POST, HTTP_PUT};
20use crate::error::StdlibError;
21use crate::module::StdlibModule;
22use crate::value::Value;
23
24pub struct HttpModule;
26
27impl HttpModule {
28 pub fn new() -> Self {
29 Self
30 }
31}
32
33impl Default for HttpModule {
34 fn default() -> Self {
35 Self::new()
36 }
37}
38
39impl StdlibModule for HttpModule {
40 fn name(&self) -> &'static str {
41 "http"
42 }
43
44 fn has_function(&self, function: &str) -> bool {
45 matches!(function, "get" | "post" | "put" | "patch" | "delete")
46 }
47
48 fn call(&self, function: &str, args: Vec<Value>) -> Result<Value, StdlibError> {
49 match function {
50 "get" => self.get(args),
51 "post" => self.post(args),
52 "put" => self.put(args),
53 "patch" => self.patch(args),
54 "delete" => self.delete(args),
55 _ => Err(StdlibError::unknown_function("http", function)),
56 }
57 }
58}
59
60impl HttpModule {
61 fn get(&self, args: Vec<Value>) -> Result<Value, StdlibError> {
66 if args.is_empty() || args.len() > 2 {
67 return Err(StdlibError::wrong_args("http.get", 1, args.len()));
68 }
69 validate_string("http.get", &args[0], 1)?;
70 Err(StdlibError::capability_call(
71 "http", "get", CAP_HTTP, HTTP_GET, args,
72 ))
73 }
74
75 fn post(&self, args: Vec<Value>) -> Result<Value, StdlibError> {
80 if args.len() < 2 || args.len() > 3 {
81 return Err(StdlibError::wrong_args("http.post", 2, args.len()));
82 }
83 validate_string("http.post", &args[0], 1)?;
84 validate_string("http.post", &args[1], 2)?;
85 Err(StdlibError::capability_call(
86 "http", "post", CAP_HTTP, HTTP_POST, args,
87 ))
88 }
89
90 fn put(&self, args: Vec<Value>) -> Result<Value, StdlibError> {
95 if args.len() < 2 || args.len() > 3 {
96 return Err(StdlibError::wrong_args("http.put", 2, args.len()));
97 }
98 validate_string("http.put", &args[0], 1)?;
99 validate_string("http.put", &args[1], 2)?;
100 Err(StdlibError::capability_call(
101 "http", "put", CAP_HTTP, HTTP_PUT, args,
102 ))
103 }
104
105 fn patch(&self, args: Vec<Value>) -> Result<Value, StdlibError> {
110 if args.len() < 2 || args.len() > 3 {
111 return Err(StdlibError::wrong_args("http.patch", 2, args.len()));
112 }
113 validate_string("http.patch", &args[0], 1)?;
114 validate_string("http.patch", &args[1], 2)?;
115 Err(StdlibError::capability_call(
116 "http", "patch", CAP_HTTP, HTTP_PATCH, args,
117 ))
118 }
119
120 fn delete(&self, args: Vec<Value>) -> Result<Value, StdlibError> {
125 if args.is_empty() || args.len() > 2 {
126 return Err(StdlibError::wrong_args("http.delete", 1, args.len()));
127 }
128 validate_string("http.delete", &args[0], 1)?;
129 Err(StdlibError::capability_call(
130 "http",
131 "delete",
132 CAP_HTTP,
133 HTTP_DELETE,
134 args,
135 ))
136 }
137}
138
139fn validate_string(func: &str, val: &Value, pos: usize) -> Result<(), StdlibError> {
142 match val {
143 Value::String(_) => Ok(()),
144 _ => Err(StdlibError::type_mismatch(
145 func,
146 pos,
147 "string",
148 val.type_name(),
149 )),
150 }
151}