langdb_clust 0.9.10

An unofficial Rust client for the Anthropic/Claude API.
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/// Implements [`serde::Serialize`] and [`serde::Deserialize`] for an enum with corresponding string variants.
///
/// ## Arguments
/// - `$enum_name`: The name of the enum.
/// - `$($variant:ident => $str:expr),*`: The variants of the enum and their corresponding string representations.
macro_rules! impl_enum_string_serialization {
    ($enum_name:ident, $($variant:ident => $str:expr),*) => {
        impl serde::Serialize for $enum_name {
            fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
            where
                S: serde::Serializer,
            {
                match self {
                    $(
                        $enum_name::$variant => serializer.serialize_str($str),
                    )*
                }
            }
        }

        impl<'de> serde::Deserialize<'de> for $enum_name {
            fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
            where
                D: serde::Deserializer<'de>,
            {
                struct EnumVisitor;

                impl<'de> serde::de::Visitor<'de> for EnumVisitor {
                    type Value = $enum_name;

                    fn expecting(
                        &self,
                        formatter: &mut std::fmt::Formatter,
                    ) -> std::fmt::Result {
                        formatter.write_str(concat!("a string representing a ", stringify!($enum_name)))
                    }

                    fn visit_str<E>(self, value: &str) -> Result<$enum_name, E>
                    where
                        E: serde::de::Error,
                    {
                        match value {
                            $(
                                $str => Ok($enum_name::$variant),
                            )*
                            _ => Err(serde::de::Error::custom("invalid value for enum")),
                        }
                    }
                }

                deserializer.deserialize_str(EnumVisitor)
            }
        }
    };
}

pub(crate) use impl_enum_string_serialization;

/// Implements [`serde::Serialize`], [`serde::Deserialize`] and [`From`]
/// for an enum with corresponding struct variants by indicating the tag field.
///
/// ## Arguments
/// - `$enum_name`: The name of the enum.
/// - `$tag_field`: The name of the [`String`] field that contains the tag that indicates the variant.
/// - `$( $variant:ident($struct:ident, $tag:expr) ),*`: The variants of the enum and their corresponding structs and tags.
macro_rules! impl_enum_struct_serialization {
    ($enum_name:ident, $tag_field:ident, $( $variant:ident($struct:ident, $tag:expr) ),* ) => {
        impl serde::Serialize for $enum_name {
            fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
            where
                S: serde::Serializer,
            {
                match self {
                    $(
                        $enum_name::$variant(ref inner) => inner.serialize(serializer),
                    )*
                }
            }
        }

        impl<'de> serde::Deserialize<'de> for $enum_name {
            fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
            where
                D: serde::Deserializer<'de>,
            {
                let value = serde_json::Value::deserialize(deserializer)?;

                let tag = value.get(stringify!($tag_field)).and_then(serde_json::Value::as_str)
                    .ok_or_else(|| serde::de::Error::missing_field(stringify!($tag_field)))?;

                match tag {
                    $(
                        $tag => serde_json::from_value(value.clone())
                            .map($enum_name::$variant)
                            .map_err(serde::de::Error::custom),
                    )*
                    _ => Err(serde::de::Error::custom(format!("unknown tag: {}", tag))),
                }
            }
        }

        $(
            impl From<$struct> for $enum_name {
                fn from(item: $struct) -> Self {
                    $enum_name::$variant(item)
                }
            }
        )*
    };
}

pub(crate) use impl_enum_struct_serialization;

/// Implements [`serde::Serialize`] and [`serde::Deserialize`] for an enum with corresponding boolean variants.
///
/// ## Arguments
/// - `$enum_name`: The name of the enum.
/// - `$true_variant`: The name of the variant that corresponds to `true`.
/// - `$false_variant`: The name of the variant that corresponds to `false`.
macro_rules! impl_enum_bool_serialization {
    ($enum_name:ident, $true_variant:ident, $false_variant:ident) => {
        impl serde::Serialize for $enum_name {
            fn serialize<S>(
                &self,
                serializer: S,
            ) -> Result<S::Ok, S::Error>
            where
                S: serde::Serializer,
            {
                match self {
                    | $enum_name::$true_variant => {
                        serializer.serialize_bool(true)
                    },
                    | $enum_name::$false_variant => {
                        serializer.serialize_bool(false)
                    },
                }
            }
        }

        struct BoolVisitor;

        impl<'de> serde::de::Visitor<'de> for BoolVisitor {
            type Value = $enum_name;

            fn expecting(
                &self,
                formatter: &mut std::fmt::Formatter,
            ) -> std::fmt::Result {
                formatter.write_str("a boolean")
            }

            fn visit_bool<E>(
                self,
                value: bool,
            ) -> Result<Self::Value, E>
            where
                E: serde::de::Error,
            {
                Ok(if value {
                    $enum_name::$true_variant
                } else {
                    $enum_name::$false_variant
                })
            }
        }

        impl<'de> serde::Deserialize<'de> for $enum_name {
            fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
            where
                D: serde::Deserializer<'de>,
            {
                deserializer.deserialize_bool(BoolVisitor)
            }
        }
    };
}

pub(crate) use impl_enum_bool_serialization;

/// Implements [`serde::Serialize`], [`serde::Deserialize`] and [`From`] for an enum with corresponding string or array of any struct variants.
///
/// ## Arguments
/// - `$enum_name`: The name of the enum.
/// - `$single_variant`: The name of the variant that corresponds to a single element.
/// - `$single_type`: The type of the single element. This must be a [`String`].
/// - `$array_variant`: The name of the variant that corresponds to an array of elements.
/// - `$array_type`: The type of the array elements.
macro_rules! impl_enum_with_string_or_array_serialization {
    ($enum_name:ident, $single_variant:ident($single_type:ty), $array_variant:ident($array_type:ty)) => {
        impl serde::Serialize for $enum_name {
            fn serialize<S>(
                &self,
                serializer: S,
            ) -> Result<S::Ok, S::Error>
            where
                S: serde::ser::Serializer,
            {
                match self {
                    | $enum_name::$single_variant(ref single) => {
                        serde::Serialize::serialize(single, serializer)
                    },
                    | $enum_name::$array_variant(ref array) => {
                        let mut seq =
                            serializer.serialize_seq(Some(array.len()))?;
                        for element in array {
                            serde::ser::SerializeSeq::serialize_element(
                                &mut seq, element,
                            )?;
                        }
                        serde::ser::SerializeSeq::end(seq)
                    },
                }
            }
        }

        impl<'de> serde::Deserialize<'de> for $enum_name {
            fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
            where
                D: serde::de::Deserializer<'de>,
            {
                struct EnumVisitor;

                impl<'de> serde::de::Visitor<'de> for EnumVisitor {
                    type Value = $enum_name;

                    fn expecting(
                        &self,
                        formatter: &mut std::fmt::Formatter,
                    ) -> std::fmt::Result {
                        formatter.write_str(
                            "a single element or an array of elements",
                        )
                    }

                    fn visit_str<E>(
                        self,
                        value: &str,
                    ) -> Result<Self::Value, E>
                    where
                        E: serde::de::Error,
                    {
                        Ok($enum_name::$single_variant(
                            value.to_string(),
                        ))
                    }

                    fn visit_seq<A>(
                        self,
                        mut seq: A,
                    ) -> Result<Self::Value, A::Error>
                    where
                        A: serde::de::SeqAccess<'de>,
                    {
                        let mut array = Vec::new();
                        while let Some(element) = seq.next_element()? {
                            array.push(element);
                        }
                        Ok($enum_name::$array_variant(array))
                    }
                }

                deserializer.deserialize_any(EnumVisitor)
            }
        }

        impl From<$single_type> for $enum_name {
            fn from(item: $single_type) -> Self {
                $enum_name::$single_variant(item)
            }
        }

        impl From<Vec<$array_type>> for $enum_name {
            fn from(array: Vec<$array_type>) -> Self {
                $enum_name::$array_variant(array)
            }
        }
    };
}

pub(crate) use impl_enum_with_string_or_array_serialization;

/// Implements [`std::fmt::Display`] for a type that can be serialized.
///
/// ## Arguments
/// - `$t`: The type.
macro_rules! impl_display_for_serialize {
    ($t:ty) => {
        impl std::fmt::Display for $t {
            fn fmt(
                &self,
                f: &mut std::fmt::Formatter<'_>,
            ) -> std::fmt::Result {
                let json = serde_json::to_string_pretty(self)
                    .map_err(|_| std::fmt::Error)?;
                write!(f, "{}", json)
            }
        }
    };
}

pub(crate) use impl_display_for_serialize;

#[cfg(test)]
mod tests {
    use serde::{Deserialize, Serialize};

    #[test]
    fn test_impl_enum_string_serialization() {
        #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
        enum TestEnum {
            A, // "a"
            B, // "b"
            C, // "c"
        }

        impl_enum_string_serialization!(TestEnum, A => "a", B => "b", C => "c");

        let test = TestEnum::A;
        let serialized = serde_json::to_string(&test).unwrap();
        assert_eq!(serialized, "\"a\"");

        let deserialized: TestEnum = serde_json::from_str(&serialized).unwrap();
        assert_eq!(deserialized, test);

        let test = TestEnum::B;
        let serialized = serde_json::to_string(&test).unwrap();
        assert_eq!(serialized, "\"b\"");

        let deserialized: TestEnum = serde_json::from_str(&serialized).unwrap();
        assert_eq!(deserialized, test);

        let test = TestEnum::C;
        let serialized = serde_json::to_string(&test).unwrap();
        assert_eq!(serialized, "\"c\"");

        let deserialized: TestEnum = serde_json::from_str(&serialized).unwrap();
        assert_eq!(deserialized, test);
    }

    #[test]
    fn test_impl_enum_struct_serialization() {
        #[derive(Debug, Clone, PartialEq)]
        enum TestEnum {
            A(TestStructA),
            B(TestStructB),
            C(TestStructC),
        }

        #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
        struct TestStructA {
            tag: String,
            value: u32,
        }

        impl TestStructA {
            fn new(value: u32) -> Self {
                Self {
                    tag: "a".to_string(),
                    value,
                }
            }
        }

        #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
        struct TestStructB {
            tag: String,
            value: u32,
        }

        impl TestStructB {
            #[allow(dead_code)]
            fn new(value: u32) -> Self {
                Self {
                    tag: "b".to_string(),
                    value,
                }
            }
        }

        #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
        struct TestStructC {
            tag: String,
            value: u32,
            other_value: u32,
        }

        impl TestStructC {
            #[allow(dead_code)]
            fn new(
                value: u32,
                other_value: u32,
            ) -> Self {
                Self {
                    tag: "c".to_string(),
                    value,
                    other_value,
                }
            }
        }

        impl_enum_struct_serialization!(
            TestEnum,
            tag,
            A(TestStructA, "a"),
            B(TestStructB, "b"),
            C(TestStructC, "c")
        );

        let test = TestEnum::A(TestStructA::new(42));
        let serialized = serde_json::to_string(&test).unwrap();
        assert_eq!(
            serialized,
            "{\"tag\":\"a\",\"value\":42}"
        );

        let deserialized: TestEnum = serde_json::from_str(&serialized).unwrap();
        assert_eq!(deserialized, test);
    }

    #[test]
    fn test_impl_enum_bool_serialization() {
        #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
        enum TestEnum {
            A, // true
            B, // false
        }

        impl_enum_bool_serialization!(TestEnum, A, B);

        let test = TestEnum::A;
        let serialized = serde_json::to_string(&test).unwrap();
        assert_eq!(serialized, "true");

        let deserialized: TestEnum = serde_json::from_str(&serialized).unwrap();
        assert_eq!(deserialized, test);

        let test = TestEnum::B;
        let serialized = serde_json::to_string(&test).unwrap();
        assert_eq!(serialized, "false");

        let deserialized: TestEnum = serde_json::from_str(&serialized).unwrap();
        assert_eq!(deserialized, test);
    }

    #[test]
    fn test_impl_enum_with_string_or_array_serialization() {
        #[derive(Debug, Clone, PartialEq)]
        enum TestEnum {
            Single(String),
            Array(Vec<TestStruct>),
        }

        #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
        struct TestStruct {
            value: u32,
        }

        impl_enum_with_string_or_array_serialization!(
            TestEnum,
            Single(String),
            Array(TestStruct)
        );

        let test = TestEnum::Single("42".to_string());
        let serialized = serde_json::to_string(&test).unwrap();
        assert_eq!(serialized, "\"42\"");

        let deserialized: TestEnum = serde_json::from_str(&serialized).unwrap();
        assert_eq!(deserialized, test);

        let test = TestEnum::Array(vec![TestStruct {
            value: 42,
        }]);
        let serialized = serde_json::to_string(&test).unwrap();
        assert_eq!(serialized, "[{\"value\":42}]");

        let deserialized: TestEnum = serde_json::from_str(&serialized).unwrap();
        assert_eq!(deserialized, test);
    }

    #[test]
    fn test_impl_display_for_serialize() {
        #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
        struct TestStruct {
            value: u32,
        }

        impl_display_for_serialize!(TestStruct);

        let test = TestStruct {
            value: 42,
        };

        assert_eq!(
            test.to_string(),
            "{\n  \"value\": 42\n}"
        );
    }
}