odbc_api/
narrow.rs

1use crate::{
2    parameter::{VarCharBox, VarCharSlice},
3    IntoParameter,
4};
5
6/// Newtype wrapper intend to be used around `String`s or `str` slices to bind them always as narrow
7/// text independent of wether the `narrow` feature is set or not.
8pub struct Narrow<T>(pub T);
9
10impl<'a> IntoParameter for Narrow<&'a str> {
11    type Parameter = VarCharSlice<'a>;
12
13    fn into_parameter(self) -> Self::Parameter {
14        VarCharSlice::new(self.0.as_bytes())
15    }
16}
17
18impl<'a> IntoParameter for Narrow<Option<&'a str>> {
19    type Parameter = VarCharSlice<'a>;
20
21    fn into_parameter(self) -> Self::Parameter {
22        match self.0 {
23            Some(str) => Narrow(str).into_parameter(),
24            None => VarCharSlice::NULL,
25        }
26    }
27}
28
29impl<'a> IntoParameter for Option<Narrow<&'a str>> {
30    type Parameter = VarCharSlice<'a>;
31
32    fn into_parameter(self) -> Self::Parameter {
33        match self {
34            Some(str) => Narrow(str.0).into_parameter(),
35            None => VarCharSlice::NULL,
36        }
37    }
38}
39
40impl IntoParameter for Narrow<String> {
41    type Parameter = VarCharBox;
42
43    fn into_parameter(self) -> Self::Parameter {
44        VarCharBox::from_string(self.0)
45    }
46}
47
48impl IntoParameter for Narrow<Option<String>> {
49    type Parameter = VarCharBox;
50
51    fn into_parameter(self) -> Self::Parameter {
52        match self.0 {
53            Some(str) => Narrow(str).into_parameter(),
54            None => VarCharBox::null(),
55        }
56    }
57}
58
59impl IntoParameter for Option<Narrow<String>> {
60    type Parameter = VarCharBox;
61
62    fn into_parameter(self) -> Self::Parameter {
63        match self {
64            Some(str) => Narrow(str.0).into_parameter(),
65            None => VarCharBox::null(),
66        }
67    }
68}