vector_lookup/lookup_v2/
mod.rs

1mod optional_path;
2
3pub use optional_path::{OptionalTargetPath, OptionalValuePath};
4use std::fmt;
5use vector_config_macros::configurable_component;
6
7pub use vrl::path::{
8    parse_target_path, parse_value_path, BorrowedSegment, OwnedSegment, OwnedTargetPath,
9    OwnedValuePath, PathConcat, PathParseError, PathPrefix, TargetPath, ValuePath,
10};
11use vrl::value::KeyString;
12
13/// A wrapper around `OwnedValuePath` that allows it to be used in Vector config.
14/// This requires a valid path to be used. If you want to allow optional paths,
15/// use [optional_path::OptionalValuePath].
16#[configurable_component]
17#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
18#[cfg_attr(feature = "proptest", derive(proptest_derive::Arbitrary))]
19#[serde(try_from = "String", into = "String")]
20pub struct ConfigValuePath(pub OwnedValuePath);
21
22impl TryFrom<String> for ConfigValuePath {
23    type Error = PathParseError;
24
25    fn try_from(src: String) -> Result<Self, Self::Error> {
26        OwnedValuePath::try_from(src).map(ConfigValuePath)
27    }
28}
29
30impl TryFrom<KeyString> for ConfigValuePath {
31    type Error = PathParseError;
32
33    fn try_from(src: KeyString) -> Result<Self, Self::Error> {
34        OwnedValuePath::try_from(String::from(src)).map(ConfigValuePath)
35    }
36}
37
38impl From<ConfigValuePath> for String {
39    fn from(owned: ConfigValuePath) -> Self {
40        String::from(owned.0)
41    }
42}
43
44impl<'a> ValuePath<'a> for &'a ConfigValuePath {
45    type Iter = <&'a OwnedValuePath as ValuePath<'a>>::Iter;
46
47    fn segment_iter(&self) -> Self::Iter {
48        (&self.0).segment_iter()
49    }
50}
51
52#[cfg(any(test, feature = "test"))]
53impl From<&str> for ConfigValuePath {
54    fn from(path: &str) -> Self {
55        ConfigValuePath::try_from(path.to_string()).unwrap()
56    }
57}
58
59/// A wrapper around `OwnedTargetPath` that allows it to be used in Vector config
60/// with prefix default to `PathPrefix::Event`
61#[configurable_component]
62#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
63#[cfg_attr(feature = "proptest", derive(proptest_derive::Arbitrary))]
64#[serde(try_from = "String", into = "String")]
65pub struct ConfigTargetPath(pub OwnedTargetPath);
66
67impl fmt::Display for ConfigTargetPath {
68    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
69        write!(f, "{}", self.0)
70    }
71}
72
73impl TryFrom<String> for ConfigTargetPath {
74    type Error = PathParseError;
75
76    fn try_from(src: String) -> Result<Self, Self::Error> {
77        OwnedTargetPath::try_from(src).map(ConfigTargetPath)
78    }
79}
80
81impl TryFrom<KeyString> for ConfigTargetPath {
82    type Error = PathParseError;
83
84    fn try_from(src: KeyString) -> Result<Self, Self::Error> {
85        OwnedTargetPath::try_from(src).map(ConfigTargetPath)
86    }
87}
88
89impl From<ConfigTargetPath> for String {
90    fn from(owned: ConfigTargetPath) -> Self {
91        String::from(owned.0)
92    }
93}
94
95impl<'a> TargetPath<'a> for &'a ConfigTargetPath {
96    type ValuePath = &'a OwnedValuePath;
97
98    fn prefix(&self) -> PathPrefix {
99        self.0.prefix
100    }
101
102    fn value_path(&self) -> Self::ValuePath {
103        &self.0.path
104    }
105}
106
107#[cfg(any(test, feature = "test"))]
108impl From<&str> for ConfigTargetPath {
109    fn from(path: &str) -> Self {
110        ConfigTargetPath::try_from(path.to_string()).unwrap()
111    }
112}