-
Notifications
You must be signed in to change notification settings - Fork 238
/
Copy pathlib.rs
253 lines (216 loc) · 7.02 KB
/
lib.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under both the MIT license found in the
* LICENSE-MIT file in the root directory of this source tree and the Apache
* License, Version 2.0 found in the LICENSE-APACHE file in the root directory
* of this source tree.
*/
use std::borrow::Cow;
use std::fmt;
pub mod serialize_duration_as_micros {
use serde::Deserialize;
use serde::Deserializer;
use serde::Serialize;
use serde::Serializer;
pub fn serialize<S>(
value: &Option<::prost_types::Duration>,
serializer: S,
) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
value.as_ref().map(to_micros).serialize(serializer)
}
pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<::prost_types::Duration>, D::Error>
where
D: Deserializer<'de>,
{
let value = Option::<i64>::deserialize(deserializer)?;
Ok(value.map(from_micros))
}
const MICROS_IN_SECOND: i64 = 1000000;
const NANOS_IN_MICRO: i32 = 1000;
/// We saturate here since technically not all ::prost_types::Duration would fit (but all those
/// we care about would).
fn to_micros(duration: &::prost_types::Duration) -> i64 {
let mut micros: i64 = 0;
micros = micros.saturating_add(duration.seconds.saturating_mul(MICROS_IN_SECOND));
micros = micros.saturating_add(duration.nanos.saturating_div(NANOS_IN_MICRO).into());
micros
}
fn from_micros(duration: i64) -> ::prost_types::Duration {
let seconds = duration / MICROS_IN_SECOND;
let nanos = ((duration % MICROS_IN_SECOND) as i32) * NANOS_IN_MICRO;
prost_types::Duration {
seconds,
nanos: nanos as _,
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_value() {
assert_eq!(
to_micros(&::prost_types::Duration {
seconds: 1,
nanos: 1000
}),
1000001
);
}
#[test]
fn test_roundtrip() {
for v in [-1, 10, i64::MAX, i64::MIN, 0] {
assert_eq!(to_micros(&from_micros(v)), v);
}
}
}
}
mod serialize_timestamp {
use serde::Deserialize;
use serde::Deserializer;
use serde::Serialize;
use serde::Serializer;
pub fn serialize<S>(
value: &Option<::prost_types::Timestamp>,
serializer: S,
) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let d = value.as_ref().map(|v| (v.seconds, v.nanos));
d.serialize(serializer)
}
pub fn deserialize<'de, D>(
deserializer: D,
) -> Result<Option<::prost_types::Timestamp>, D::Error>
where
D: Deserializer<'de>,
{
let d = Option::<(i64, i32)>::deserialize(deserializer)?;
let d = d.map(|(seconds, nanos)| ::prost_types::Timestamp { seconds, nanos });
Ok(d)
}
}
mod serialize_bytes {
use serde::Deserialize;
use serde::Deserializer;
use serde::Serialize;
use serde::Serializer;
pub fn serialize<S>(value: &[u8], serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let d = hex::encode(value);
d.serialize(serializer)
}
pub fn deserialize<'de, D>(deserializer: D) -> Result<Vec<u8>, D::Error>
where
D: Deserializer<'de>,
{
let d = String::deserialize(deserializer)?;
let d = hex::decode(d).map_err(serde::de::Error::custom)?;
Ok(d)
}
}
mod serialize_action_kind {
use serde::Deserialize;
use serde::Deserializer;
use serde::Serialize;
use serde::Serializer;
#[allow(clippy::trivially_copy_pass_by_ref)]
pub fn serialize<S>(value: &i32, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let k = crate::ActionKind::from_i32(*value).ok_or_else(|| {
serde::ser::Error::custom(format!("Invalid ActionKind enum value: {}", value))
})?;
k.serialize(serializer)
}
pub fn deserialize<'de, D>(deserializer: D) -> Result<i32, D::Error>
where
D: Deserializer<'de>,
{
let d = crate::ActionKind::deserialize(deserializer)?;
Ok(d as i32)
}
}
tonic::include_proto!("buck.data");
/// Trait for things that can be converted into protobuf messages, for ease of emitting events. There are many core Buck
/// types that are represented in the Daemon API that use this trait to ease conversion.
pub trait ToProtoMessage {
type Message: prost::Message;
fn as_proto(&self) -> Self::Message;
}
impl ToProtoMessage for buck2_core::target::label::TargetLabel {
type Message = crate::TargetLabel;
fn as_proto(&self) -> Self::Message {
crate::TargetLabel {
package: self.pkg().to_string(),
name: self.name().to_string(),
}
}
}
impl ToProtoMessage for buck2_core::target::label::ConfiguredTargetLabel {
type Message = crate::ConfiguredTargetLabel;
fn as_proto(&self) -> Self::Message {
crate::ConfiguredTargetLabel {
label: Some(self.unconfigured().as_proto()),
configuration: Some(self.cfg().as_proto()),
execution_configuration: self.exec_cfg().map(ToProtoMessage::as_proto),
}
}
}
impl ToProtoMessage for buck2_core::configuration::data::ConfigurationData {
type Message = crate::Configuration;
fn as_proto(&self) -> Self::Message {
crate::Configuration {
full_name: self.full_name().to_owned(),
}
}
}
/// Write out a human-readable description of the error tags
/// that is printed out in the context stack when program fails.
impl fmt::Display for ErrorCategory {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let msg = match &self {
ErrorCategory::Infra => "This error is an internal Buck2 error",
ErrorCategory::User => "This error was caused by the end user",
};
write!(f, "{}", msg)
}
}
impl fmt::Display for ErrorCause {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let msg = match &self {
ErrorCause::InvalidPackage => "The package is invalid",
ErrorCause::DaemonIsBusy => "Buck daemon is busy processing another command",
};
write!(f, "{}", msg)
}
}
impl fmt::Display for DaemonShutdown {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}, caller:", self.reason)?;
for caller in self.callers.iter() {
let max_len = 70;
let short_caller = if caller.len() > max_len {
Cow::Owned(
caller
.chars()
.take(max_len)
.chain(std::iter::repeat('.').take(3))
.collect(),
)
} else {
Cow::Borrowed(caller)
};
writeln!(f)?;
write!(f, " * {}", short_caller)?;
}
Ok(())
}
}