thulp-core 0.3.3

Core types and traits for thulp
Documentation
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
//! Parameter types for tool definitions.

use serde::{Deserialize, Serialize};

/// The type of a parameter.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "lowercase")]
pub enum ParameterType {
    /// String parameter.
    #[default]
    String,
    /// Integer parameter.
    Integer,
    /// Number (float) parameter.
    Number,
    /// Boolean parameter.
    Boolean,
    /// Array parameter.
    Array,
    /// Object parameter.
    Object,
}

impl ParameterType {
    /// Returns the type name as a string.
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::String => "string",
            Self::Integer => "integer",
            Self::Number => "number",
            Self::Boolean => "boolean",
            Self::Array => "array",
            Self::Object => "object",
        }
    }

    /// Checks if a JSON value matches this parameter type.
    pub fn matches(&self, value: &serde_json::Value) -> bool {
        match (self, value) {
            (Self::String, serde_json::Value::String(_)) => true,
            (Self::Integer, serde_json::Value::Number(n)) => n.is_i64() || n.is_u64(),
            (Self::Number, serde_json::Value::Number(_)) => true,
            (Self::Boolean, serde_json::Value::Bool(_)) => true,
            (Self::Array, serde_json::Value::Array(_)) => true,
            (Self::Object, serde_json::Value::Object(_)) => true,
            _ => false,
        }
    }
}

/// A parameter definition for a tool.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Parameter {
    /// The parameter name.
    pub name: String,

    /// The parameter type.
    #[serde(rename = "type", default)]
    pub param_type: ParameterType,

    /// Whether this parameter is required.
    #[serde(default)]
    pub required: bool,

    /// Description of the parameter.
    #[serde(default)]
    pub description: String,

    /// Default value for the parameter.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub default: Option<serde_json::Value>,

    /// Enum of allowed values.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub enum_values: Vec<serde_json::Value>,
}

impl Parameter {
    /// Create a new parameter with the given name.
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            param_type: ParameterType::default(),
            required: false,
            description: String::new(),
            default: None,
            enum_values: Vec::new(),
        }
    }

    /// Create a builder for a parameter.
    pub fn builder(name: impl Into<String>) -> ParameterBuilder {
        ParameterBuilder::new(name)
    }

    /// Create a required string parameter.
    pub fn required_string(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            param_type: ParameterType::String,
            required: true,
            description: String::new(),
            default: None,
            enum_values: Vec::new(),
        }
    }

    /// Create an optional string parameter.
    pub fn optional_string(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            param_type: ParameterType::String,
            required: false,
            description: String::new(),
            default: None,
            enum_values: Vec::new(),
        }
    }
}

/// Builder for [`Parameter`].
#[derive(Debug, Default)]
pub struct ParameterBuilder {
    name: String,
    param_type: ParameterType,
    required: bool,
    description: String,
    default: Option<serde_json::Value>,
    enum_values: Vec<serde_json::Value>,
}

impl ParameterBuilder {
    /// Create a new parameter builder.
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            ..Default::default()
        }
    }

    /// Set the parameter type.
    pub fn param_type(mut self, param_type: ParameterType) -> Self {
        self.param_type = param_type;
        self
    }

    /// Set whether the parameter is required.
    pub fn required(mut self, required: bool) -> Self {
        self.required = required;
        self
    }

    /// Set the parameter description.
    pub fn description(mut self, description: impl Into<String>) -> Self {
        self.description = description.into();
        self
    }

    /// Set the default value.
    pub fn default(mut self, default: serde_json::Value) -> Self {
        self.default = Some(default);
        self
    }

    /// Add an enum value.
    pub fn enum_value(mut self, value: serde_json::Value) -> Self {
        self.enum_values.push(value);
        self
    }

    /// Build the parameter.
    pub fn build(self) -> Parameter {
        Parameter {
            name: self.name,
            param_type: self.param_type,
            required: self.required,
            description: self.description,
            default: self.default,
            enum_values: self.enum_values,
        }
    }
}

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

    #[test]
    fn parameter_type_as_str() {
        assert_eq!(ParameterType::String.as_str(), "string");
        assert_eq!(ParameterType::Integer.as_str(), "integer");
        assert_eq!(ParameterType::Number.as_str(), "number");
        assert_eq!(ParameterType::Boolean.as_str(), "boolean");
        assert_eq!(ParameterType::Array.as_str(), "array");
        assert_eq!(ParameterType::Object.as_str(), "object");
    }

    #[test]
    fn parameter_type_matches_string() {
        assert!(ParameterType::String.matches(&json!("hello")));
        assert!(!ParameterType::String.matches(&json!(123)));
        assert!(!ParameterType::String.matches(&json!(true)));
    }

    #[test]
    fn parameter_type_matches_integer() {
        assert!(ParameterType::Integer.matches(&json!(123)));
        assert!(ParameterType::Integer.matches(&json!(-456)));
        assert!(!ParameterType::Integer.matches(&json!(1.5)));
        assert!(!ParameterType::Integer.matches(&json!("123")));
    }

    #[test]
    fn parameter_type_matches_number() {
        assert!(ParameterType::Number.matches(&json!(123)));
        assert!(ParameterType::Number.matches(&json!(1.5)));
        assert!(ParameterType::Number.matches(&json!(-3.14)));
        assert!(!ParameterType::Number.matches(&json!("1.5")));
    }

    #[test]
    fn parameter_type_matches_boolean() {
        assert!(ParameterType::Boolean.matches(&json!(true)));
        assert!(ParameterType::Boolean.matches(&json!(false)));
        assert!(!ParameterType::Boolean.matches(&json!("true")));
        assert!(!ParameterType::Boolean.matches(&json!(1)));
    }

    #[test]
    fn parameter_type_matches_array() {
        assert!(ParameterType::Array.matches(&json!([1, 2, 3])));
        assert!(ParameterType::Array.matches(&json!([])));
        assert!(!ParameterType::Array.matches(&json!({"a": 1})));
    }

    #[test]
    fn parameter_type_matches_object() {
        assert!(ParameterType::Object.matches(&json!({"a": 1})));
        assert!(ParameterType::Object.matches(&json!({})));
        assert!(!ParameterType::Object.matches(&json!([1, 2])));
    }

    #[test]
    fn parameter_new() {
        let param = Parameter::new("test");
        assert_eq!(param.name, "test");
        assert_eq!(param.param_type, ParameterType::String);
        assert!(!param.required);
        assert!(param.description.is_empty());
        assert!(param.default.is_none());
    }

    #[test]
    fn parameter_required_string() {
        let param = Parameter::required_string("username");
        assert_eq!(param.name, "username");
        assert_eq!(param.param_type, ParameterType::String);
        assert!(param.required);
    }

    #[test]
    fn parameter_optional_string() {
        let param = Parameter::optional_string("limit");
        assert_eq!(param.name, "limit");
        assert_eq!(param.param_type, ParameterType::String);
        assert!(!param.required);
    }

    #[test]
    fn parameter_builder() {
        let param = Parameter::builder("count")
            .param_type(ParameterType::Integer)
            .required(true)
            .description("Number of items")
            .default(json!(10))
            .build();

        assert_eq!(param.name, "count");
        assert_eq!(param.param_type, ParameterType::Integer);
        assert!(param.required);
        assert_eq!(param.description, "Number of items");
        assert_eq!(param.default, Some(json!(10)));
    }

    #[test]
    fn parameter_builder_with_enum() {
        let param = Parameter::builder("format")
            .param_type(ParameterType::String)
            .enum_value(json!("json"))
            .enum_value(json!("yaml"))
            .enum_value(json!("toml"))
            .build();

        assert_eq!(param.enum_values.len(), 3);
        assert!(param.enum_values.contains(&json!("json")));
    }

    #[test]
    fn parameter_serialization() {
        let param = Parameter::builder("path")
            .param_type(ParameterType::String)
            .required(true)
            .description("File path")
            .build();

        let json = serde_json::to_string(&param).unwrap();
        let parsed: Parameter = serde_json::from_str(&json).unwrap();

        assert_eq!(param, parsed);
    }

    #[test]
    fn parameter_deserialization_with_defaults() {
        let json = r#"{"name": "test"}"#;
        let param: Parameter = serde_json::from_str(json).unwrap();

        assert_eq!(param.name, "test");
        assert_eq!(param.param_type, ParameterType::String);
        assert!(!param.required);
    }

    #[test]
    fn parameter_type_matches_edge_cases() {
        // Integer should match i64 and u64
        let param_type = ParameterType::Integer;
        assert!(param_type.matches(&json!(42)));
        assert!(param_type.matches(&json!(-42)));
        assert!(param_type.matches(&json!(9223372036854775807i64)));
        assert!(!param_type.matches(&json!(3.14)));
        assert!(!param_type.matches(&json!("42")));

        // Number should match any numeric value
        let param_type = ParameterType::Number;
        assert!(param_type.matches(&json!(42)));
        assert!(param_type.matches(&json!(3.14)));
        assert!(param_type.matches(&json!(-42)));
        assert!(!param_type.matches(&json!("42")));

        // Boolean should only match true/false
        let param_type = ParameterType::Boolean;
        assert!(param_type.matches(&json!(true)));
        assert!(param_type.matches(&json!(false)));
        assert!(!param_type.matches(&json!(1)));
        assert!(!param_type.matches(&json!("true")));

        // Array should only match arrays
        let param_type = ParameterType::Array;
        assert!(param_type.matches(&json!([])));
        assert!(param_type.matches(&json!([1, 2, 3])));
        assert!(!param_type.matches(&json!({})));
        assert!(!param_type.matches(&json!("[]")));

        // Object should only match objects
        let param_type = ParameterType::Object;
        assert!(param_type.matches(&json!({})));
        assert!(param_type.matches(&json!({"key": "value"})));
        assert!(!param_type.matches(&json!([])));
        assert!(!param_type.matches(&json!("{}")));
    }

    #[test]
    fn parameter_enum_validation() {
        let param = Parameter::builder("status")
            .param_type(ParameterType::String)
            .enum_value(json!("active"))
            .enum_value(json!("inactive"))
            .enum_value(json!("pending"))
            .build();

        assert_eq!(param.enum_values.len(), 3);
        assert!(param.enum_values.contains(&json!("active")));
        assert!(param.enum_values.contains(&json!("inactive")));
        assert!(param.enum_values.contains(&json!("pending")));
    }

    #[test]
    fn parameter_builder_complex() {
        let param = Parameter::builder("data")
            .param_type(ParameterType::Object)
            .required(true)
            .description("Complex data structure")
            .default(json!({"nested": {"key": "value"}}))
            .enum_value(json!({"type": "default"}))
            .enum_value(json!({"type": "custom"}))
            .build();

        assert_eq!(param.name, "data");
        assert_eq!(param.param_type, ParameterType::Object);
        assert!(param.required);
        assert_eq!(param.description, "Complex data structure");
        assert_eq!(param.default, Some(json!({"nested": {"key": "value"}})));
        assert_eq!(param.enum_values.len(), 2);
    }

    #[test]
    fn parameter_serialization_roundtrip() {
        let original = Parameter::builder("test_param")
            .param_type(ParameterType::Integer)
            .required(true)
            .description("Test description")
            .default(json!(42))
            .build();

        let serialized = serde_json::to_string(&original).unwrap();
        let deserialized: Parameter = serde_json::from_str(&serialized).unwrap();

        assert_eq!(original.name, deserialized.name);
        assert_eq!(original.param_type, deserialized.param_type);
        assert_eq!(original.required, deserialized.required);
        assert_eq!(original.description, deserialized.description);
        assert_eq!(original.default, deserialized.default);
    }
}