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