This repository was archived by the owner on Jun 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwrappers.rs
164 lines (136 loc) · 4.19 KB
/
wrappers.rs
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use serde::de::{self, Deserialize, Deserializer};
use std::borrow::Cow;
use std::time::Duration;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OnceDuration {
Once,
Duration(Seconds),
}
impl From<u64> for OnceDuration {
fn from(v: u64) -> Self {
Self::Duration(v.into())
}
}
impl<'de> Deserialize<'de> for OnceDuration {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct OnceDurationVisitor;
impl<'de> de::Visitor<'de> for OnceDurationVisitor {
type Value = OnceDuration;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("\"once\", i64 or f64")
}
fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(OnceDuration::Duration(Seconds(Duration::from_secs(
v as u64,
))))
}
fn visit_f64<E>(self, v: f64) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(OnceDuration::Duration(Seconds(Duration::from_secs_f64(v))))
}
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: de::Error,
{
if v == "once" {
Ok(OnceDuration::Once)
} else {
Err(E::custom(format!("'{}' is not a valid interval", v)))
}
}
}
deserializer.deserialize_any(OnceDurationVisitor)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Seconds(pub Duration);
impl From<u64> for Seconds {
fn from(v: u64) -> Self {
Self::new(v)
}
}
impl Seconds {
pub fn new(value: u64) -> Self {
Self(Duration::from_secs(value))
}
pub fn timer(self) -> tokio::time::Interval {
let mut timer = tokio::time::interval_at(tokio::time::Instant::now() + self.0, self.0);
timer.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
timer
}
}
impl<'de> Deserialize<'de> for Seconds {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct SecondsVisitor;
impl<'de> de::Visitor<'de> for SecondsVisitor {
type Value = Seconds;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("i64 or f64")
}
fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(Seconds(Duration::from_secs(v as u64)))
}
fn visit_f64<E>(self, v: f64) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(Seconds(Duration::from_secs_f64(v)))
}
}
deserializer.deserialize_any(SecondsVisitor)
}
}
#[derive(Debug, Clone)]
pub struct ShellString(pub Cow<'static, str>);
impl<T> From<T> for ShellString
where
T: Into<Cow<'static, str>>,
{
fn from(v: T) -> Self {
Self(v.into())
}
}
impl<'de> Deserialize<'de> for ShellString {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct Visitor;
impl<'de> de::Visitor<'de> for Visitor {
type Value = ShellString;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("text")
}
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(ShellString(v.to_string().into()))
}
}
deserializer.deserialize_any(Visitor)
}
}
impl ShellString {
pub fn new<T: Into<Cow<'static, str>>>(value: T) -> Self {
Self(value.into())
}
pub fn expand(&self) -> crate::errors::Result<Cow<str>> {
use crate::errors::ResultExt;
shellexpand::full(&self.0).error("Failed to expand string")
}
}