jsonb_rs/
json_path.rs

1// Copyright 2023 Datafuse Labs.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::borrow::Cow;
16use std::fmt::Debug;
17use std::string::ToString;
18
19#[derive(serde::Serialize, serde::Deserialize, Clone, Debug, PartialEq, Eq, Hash)]
20pub enum JsonPath {
21    String(String),
22    UInt64(u64),
23}
24
25impl ToString for JsonPath {
26    fn to_string(&self) -> String {
27        match self {
28            JsonPath::String(s) => format!("['{}']", s),
29            JsonPath::UInt64(n) => format!("[{}]", n),
30        }
31    }
32}
33
34impl<'a> JsonPath {
35    pub fn as_ref(&'a self) -> JsonPathRef<'a> {
36        match self {
37            JsonPath::String(v) => JsonPathRef::String(Cow::from(v)),
38            JsonPath::UInt64(v) => JsonPathRef::UInt64(*v),
39        }
40    }
41}
42
43#[derive(Clone, Debug, PartialEq, Eq)]
44pub enum JsonPathRef<'a> {
45    String(Cow<'a, str>),
46    UInt64(u64),
47}