artifactize_tools/scope/
logical.rs1use serde::{Deserialize, Serialize};
3use std::{borrow::Borrow, fmt, ops::Deref, str::FromStr};
4macro_rules! identity {
5 ($name:ident, $valid:expr) => {
6 #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
7 #[serde(transparent)]
8 pub struct $name(String);
9 impl FromStr for $name {
10 type Err = String;
11 fn from_str(value: &str) -> Result<Self, Self::Err> {
12 if ($valid)(value) {
13 Ok(Self(value.to_owned()))
14 } else {
15 Err(format!("Invalid {}.", stringify!($name)))
16 }
17 }
18 }
19 impl<'de> Deserialize<'de> for $name {
20 fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
21 use serde::de::Error;
22 String::deserialize(deserializer)?
23 .parse()
24 .map_err(D::Error::custom)
25 }
26 }
27 impl $name {
28 pub fn as_str(&self) -> &str {
29 &self.0
30 }
31 }
32 impl Deref for $name {
33 type Target = str;
34 fn deref(&self) -> &str {
35 &self.0
36 }
37 }
38 impl AsRef<str> for $name {
39 fn as_ref(&self) -> &str {
40 &self.0
41 }
42 }
43 impl Borrow<str> for $name {
44 fn borrow(&self) -> &str {
45 &self.0
46 }
47 }
48 impl Borrow<String> for $name {
49 fn borrow(&self) -> &String {
50 &self.0
51 }
52 }
53 impl fmt::Display for $name {
54 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55 self.0.fmt(f)
56 }
57 }
58 impl PartialEq<str> for $name {
59 fn eq(&self, other: &str) -> bool {
60 self.0 == other
61 }
62 }
63 impl PartialEq<&str> for $name {
64 fn eq(&self, other: &&str) -> bool {
65 self.0 == *other
66 }
67 }
68 impl PartialEq<String> for $name {
69 fn eq(&self, other: &String) -> bool {
70 &self.0 == other
71 }
72 }
73 impl PartialEq<$name> for String {
74 fn eq(&self, other: &$name) -> bool {
75 *self == other.0
76 }
77 }
78 impl PartialEq<$name> for str {
79 fn eq(&self, other: &$name) -> bool {
80 self == other.0
81 }
82 }
83 impl PartialEq<$name> for &str {
84 fn eq(&self, other: &$name) -> bool {
85 *self == other.0
86 }
87 }
88 };
89}
90identity!(LogicalPath, |value: &str| value == "."
91 || crate::files::declaration_path(value).is_ok());
92impl AsRef<std::path::Path> for LogicalPath {
93 fn as_ref(&self) -> &std::path::Path {
94 std::path::Path::new(self.as_str())
95 }
96}
97impl AsRef<std::ffi::OsStr> for LogicalPath {
98 fn as_ref(&self) -> &std::ffi::OsStr {
99 std::ffi::OsStr::new(self.as_str())
100 }
101}