vector_lookup/lookup_v2/
optional_path.rs

1use vector_config::configurable_component;
2use vrl::owned_value_path;
3use vrl::path::PathPrefix;
4
5use crate::lookup_v2::PathParseError;
6use crate::{OwnedTargetPath, OwnedValuePath};
7
8#[configurable_component]
9#[derive(Debug, Clone, PartialEq, Eq, Default, Hash, PartialOrd, Ord)]
10#[cfg_attr(feature = "proptest", derive(proptest_derive::Arbitrary))]
11#[serde(try_from = "String", into = "String")]
12/// An optional path that deserializes an empty string to `None`.
13pub struct OptionalTargetPath {
14    pub path: Option<OwnedTargetPath>,
15}
16
17impl OptionalTargetPath {
18    pub fn none() -> Self {
19        Self { path: None }
20    }
21
22    pub fn event(path: &str) -> Self {
23        Self {
24            path: Some(OwnedTargetPath {
25                prefix: PathPrefix::Event,
26                path: owned_value_path!(path),
27            }),
28        }
29    }
30
31    pub fn from(prefix: PathPrefix, path: Option<OwnedValuePath>) -> Self {
32        Self {
33            path: path.map(|path| OwnedTargetPath { prefix, path }),
34        }
35    }
36
37    pub fn as_ref(&self) -> Option<&OwnedTargetPath> {
38        self.path.as_ref()
39    }
40}
41
42impl TryFrom<String> for OptionalTargetPath {
43    type Error = PathParseError;
44
45    fn try_from(src: String) -> Result<Self, Self::Error> {
46        if src.is_empty() {
47            Ok(Self { path: None })
48        } else {
49            OwnedTargetPath::try_from(src).map(|path| Self { path: Some(path) })
50        }
51    }
52}
53
54impl From<OptionalTargetPath> for String {
55    fn from(optional_path: OptionalTargetPath) -> Self {
56        match optional_path.path {
57            Some(path) => String::from(path),
58            None => String::new(),
59        }
60    }
61}
62
63impl From<OwnedTargetPath> for OptionalTargetPath {
64    fn from(path: OwnedTargetPath) -> Self {
65        Self { path: Some(path) }
66    }
67}
68
69#[configurable_component]
70#[derive(Debug, Clone, PartialEq, Eq, Default, Hash, PartialOrd, Ord)]
71#[cfg_attr(feature = "proptest", derive(proptest_derive::Arbitrary))]
72#[serde(try_from = "String", into = "String")]
73/// An optional path that deserializes an empty string to `None`.
74pub struct OptionalValuePath {
75    pub path: Option<OwnedValuePath>,
76}
77
78impl OptionalValuePath {
79    pub fn none() -> Self {
80        Self { path: None }
81    }
82
83    pub fn new(path: &str) -> Self {
84        Self {
85            path: Some(owned_value_path!(path)),
86        }
87    }
88}
89
90impl TryFrom<String> for OptionalValuePath {
91    type Error = PathParseError;
92
93    fn try_from(src: String) -> Result<Self, Self::Error> {
94        if src.is_empty() {
95            Ok(Self { path: None })
96        } else {
97            OwnedValuePath::try_from(src).map(|path| Self { path: Some(path) })
98        }
99    }
100}
101
102impl From<OptionalValuePath> for String {
103    fn from(optional_path: OptionalValuePath) -> Self {
104        match optional_path.path {
105            Some(path) => String::from(path),
106            None => String::new(),
107        }
108    }
109}
110
111impl From<OwnedValuePath> for OptionalValuePath {
112    fn from(path: OwnedValuePath) -> Self {
113        Self { path: Some(path) }
114    }
115}
116
117impl From<Option<OwnedValuePath>> for OptionalValuePath {
118    fn from(value: Option<OwnedValuePath>) -> Self {
119        value.map_or(OptionalValuePath::none(), |path| {
120            OptionalValuePath::from(path)
121        })
122    }
123}