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
use vector_config::configurable_component;
use vrl::owned_value_path;
use vrl::path::PathPrefix;

use crate::lookup_v2::PathParseError;
use crate::{OwnedTargetPath, OwnedValuePath};

#[configurable_component]
#[derive(Debug, Clone, PartialEq, Eq, Default, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "proptest", derive(proptest_derive::Arbitrary))]
#[serde(try_from = "String", into = "String")]
/// An optional path that deserializes an empty string to `None`.
pub struct OptionalTargetPath {
    pub path: Option<OwnedTargetPath>,
}

impl OptionalTargetPath {
    pub fn none() -> Self {
        Self { path: None }
    }

    pub fn event(path: &str) -> Self {
        Self {
            path: Some(OwnedTargetPath {
                prefix: PathPrefix::Event,
                path: owned_value_path!(path),
            }),
        }
    }

    pub fn from(prefix: PathPrefix, path: Option<OwnedValuePath>) -> Self {
        Self {
            path: path.map(|path| OwnedTargetPath { prefix, path }),
        }
    }

    pub fn as_ref(&self) -> Option<&OwnedTargetPath> {
        self.path.as_ref()
    }
}

impl TryFrom<String> for OptionalTargetPath {
    type Error = PathParseError;

    fn try_from(src: String) -> Result<Self, Self::Error> {
        if src.is_empty() {
            Ok(Self { path: None })
        } else {
            OwnedTargetPath::try_from(src).map(|path| Self { path: Some(path) })
        }
    }
}

impl From<OptionalTargetPath> for String {
    fn from(optional_path: OptionalTargetPath) -> Self {
        match optional_path.path {
            Some(path) => String::from(path),
            None => String::new(),
        }
    }
}

impl From<OwnedTargetPath> for OptionalTargetPath {
    fn from(path: OwnedTargetPath) -> Self {
        Self { path: Some(path) }
    }
}

#[configurable_component]
#[derive(Debug, Clone, PartialEq, Eq, Default, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "proptest", derive(proptest_derive::Arbitrary))]
#[serde(try_from = "String", into = "String")]
/// An optional path that deserializes an empty string to `None`.
pub struct OptionalValuePath {
    pub path: Option<OwnedValuePath>,
}

impl OptionalValuePath {
    pub fn none() -> Self {
        Self { path: None }
    }

    pub fn new(path: &str) -> Self {
        Self {
            path: Some(owned_value_path!(path)),
        }
    }
}

impl TryFrom<String> for OptionalValuePath {
    type Error = PathParseError;

    fn try_from(src: String) -> Result<Self, Self::Error> {
        if src.is_empty() {
            Ok(Self { path: None })
        } else {
            OwnedValuePath::try_from(src).map(|path| Self { path: Some(path) })
        }
    }
}

impl From<OptionalValuePath> for String {
    fn from(optional_path: OptionalValuePath) -> Self {
        match optional_path.path {
            Some(path) => String::from(path),
            None => String::new(),
        }
    }
}

impl From<OwnedValuePath> for OptionalValuePath {
    fn from(path: OwnedValuePath) -> Self {
        Self { path: Some(path) }
    }
}

impl From<Option<OwnedValuePath>> for OptionalValuePath {
    fn from(value: Option<OwnedValuePath>) -> Self {
        value.map_or(OptionalValuePath::none(), |path| {
            OptionalValuePath::from(path)
        })
    }
}