1 use std::fmt;
2 
3 use crate::protobuf_abs_path::ProtobufAbsPath;
4 use crate::protobuf_rel_path::ProtobufRelPath;
5 
6 /// Protobuf identifier can be absolute or relative.
7 #[derive(Debug, Eq, PartialEq, Clone, Hash)]
8 pub(crate) enum ProtobufPath {
9     Abs(ProtobufAbsPath),
10     Rel(ProtobufRelPath),
11 }
12 
13 impl ProtobufPath {
new<S: Into<String>>(path: S) -> ProtobufPath14     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 
_resolve(&self, package: &ProtobufAbsPath) -> ProtobufAbsPath23     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 
35 impl fmt::Display for ProtobufPath {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result36     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 }
43