solagent_parameters/
lib.rs

1// Copyright 2025 zTgx
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#[macro_export]
16macro_rules! parameters {
17    ($($name:ident: $type:ty),* $(,)?) => {{
18        use serde_json::json;
19
20        let mut properties = serde_json::Map::new();
21        $(
22            let property = match stringify!($type) {
23                "String" => json!({
24                    "type": "string"
25                }),
26                "i32" | "i64" | "u64" | "u32" => json!({
27                    "type": "number"
28                }),
29                "bool" => json!({
30                    "type": "boolean"
31                }),
32
33                s if s.starts_with("Vec<") && s.ends_with(">") => {
34
35                    let inner_type_str = &s[4..s.len() - 1];
36                    let inner_type = match inner_type_str {
37                        "String" => json!({
38                            "type": "string"
39                        }),
40                        "i32" | "i64" | "u64" | "u32" => json!({
41                            "type": "number"
42                        }),
43                        "bool" => json!({
44                            "type": "boolean"
45                        }),
46                        _ => json!({
47                            "type": "object"
48                        }),
49                    };
50                    json!({
51                        "type": "array",
52                        "items": inner_type
53                    })
54                }
55                _ => {
56                    json!({
57                        "type": "object"
58                    })
59                }
60            };
61            properties.insert(stringify!($name).to_string(), property);
62        )*
63        json!({
64            "type": "object",
65            "properties": properties,
66        })
67    }};
68}
69
70
71#[cfg(test)]
72mod tests {
73    use super::*;
74    use serde_json::json;
75    
76    #[test]
77    fn test_parameters_with_option_string() {
78        let schema = parameters!(name: Option<String>);
79        let expected_schema = json!({
80            "type": "object",
81            "properties": {
82                "name": {
83                    "type": "object" // Since Option<T> is treated as an object
84                }
85            }
86        });
87        assert_eq!(schema, expected_schema);
88    }
89
90    #[test]
91    fn test_parameters_with_string() {
92        let schema = parameters!(name: String);
93        let expected_schema = json!({
94            "type": "object",
95            "properties": {
96                "name": {
97                    "type": "string"
98                }
99            }
100        });
101        assert_eq!(schema, expected_schema);
102    }
103
104    #[test]
105    fn test_parameters_with_i32() {
106        let schema = parameters!(age: i32);
107        let expected_schema = json!({
108            "type": "object",
109            "properties": {
110                "age": {
111                    "type": "number"
112                }
113            }
114        });
115        assert_eq!(schema, expected_schema);
116    }
117
118    #[test]
119    fn test_parameters_with_vec_string() {
120        let schema = parameters!(tags: Vec<String>);
121        let expected_schema = json!({
122            "type": "object",
123            "properties": {
124                "tags": {
125                    "type": "array",
126                    "items": {
127                        "type": "string"
128                    }
129                }
130            }
131        });
132        assert_eq!(schema, expected_schema);
133    }
134
135    #[test]
136    fn test_parameters_with_option_i32() {
137        let schema = parameters!(score: Option<i32>);
138        let expected_schema = json!({
139            "type": "object",
140            "properties": {
141                "score": {
142                    "type": "object" // Since Option<T> is treated as an object
143                }
144            }
145        });
146        assert_eq!(schema, expected_schema);
147    }
148
149    #[test]
150    fn test_parameters_with_vec_option_bool() {
151        let schema = parameters!(flags: Vec<Option<bool>>);
152        let expected_schema = json!({
153            "type": "object",
154            "properties": {
155                "flags": {
156                    "type": "array",
157                    "items": {
158                        "type": "object" // Since Option<T> is treated as an object
159                    }
160                }
161            }
162        });
163        assert_eq!(schema, expected_schema);
164    }
165}