protobuf_parse/
protobuf_path.rs

1use std::fmt;
2
3use crate::protobuf_abs_path::ProtobufAbsPath;
4use crate::protobuf_rel_path::ProtobufRelPath;
5
6/// Protobuf identifier can be absolute or relative.
7#[derive(Debug, Eq, PartialEq, Clone, Hash)]
8pub(crate) enum ProtobufPath {
9    Abs(ProtobufAbsPath),
10    Rel(ProtobufRelPath),
11}
12
13impl ProtobufPath {
14    pub fn new<S: Into<String>>(path: S) -> ProtobufPath {
15        let path = path.into();
16        if path.starts_with('.') {
17            ProtobufPath::Abs(ProtobufAbsPath::new(path))
18        } else {
19            ProtobufPath::Rel(ProtobufRelPath::new(path))
20        }
21    }
22
23    pub fn _resolve(&self, package: &ProtobufAbsPath) -> ProtobufAbsPath {
24        match self {
25            ProtobufPath::Abs(p) => p.clone(),
26            ProtobufPath::Rel(p) => {
27                let mut package = package.clone();
28                package.push_relative(p);
29                package
30            }
31        }
32    }
33}
34
35impl fmt::Display for ProtobufPath {
36    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37        match self {
38            ProtobufPath::Abs(p) => write!(f, "{}", p),
39            ProtobufPath::Rel(p) => write!(f, "{}", p),
40        }
41    }
42}