lint: update rustfmt.toml and run cargo +nighlty fmt
This commit is contained in:
parent
d7602c8578
commit
bab1ac45a1
@ -16,7 +16,7 @@
|
||||
*/
|
||||
|
||||
use std::fmt;
|
||||
use std::marker::{PhantomData};
|
||||
use std::marker::PhantomData;
|
||||
|
||||
use base64;
|
||||
use serde::{Deserializer, Serializer};
|
||||
@ -24,7 +24,7 @@ use serde::{Deserializer, Serializer};
|
||||
pub fn decode(encoded: &[u8]) -> Result<Vec<u8>, base64::DecodeError> {
|
||||
let space_regex = regex::bytes::Regex::new(r"[ \t\r\n]").unwrap();
|
||||
let base64_data = space_regex.replace_all(encoded, &b""[..]);
|
||||
let config = base64::Config::new(base64::CharacterSet::Standard, true);
|
||||
let config = base64::Config::new(base64::CharacterSet::Standard, true);
|
||||
base64::decode_config(&base64_data, config)
|
||||
}
|
||||
|
||||
@ -44,19 +44,18 @@ struct Base64Visitor;
|
||||
|
||||
impl<'de> serde::de::Visitor<'de> for Base64Visitor {
|
||||
type Value = Vec<u8>;
|
||||
|
||||
fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||
fmt.write_str("a base64-encoded string")
|
||||
}
|
||||
|
||||
fn visit_bytes<E>(self, base64: &[u8]) -> Result<Self::Value, E>
|
||||
where E: serde::de::Error
|
||||
{
|
||||
where E: serde::de::Error {
|
||||
decode(base64).map_err(|error| E::custom(error.to_string()))
|
||||
}
|
||||
|
||||
fn visit_str<E>(self, base64: &str) -> Result<Self::Value, E>
|
||||
where E: serde::de::Error
|
||||
{
|
||||
where E: serde::de::Error {
|
||||
self.visit_bytes(base64.as_bytes())
|
||||
}
|
||||
}
|
||||
@ -71,21 +70,29 @@ impl<'de, T> serde::de::Visitor<'de> for FixedLengthBase64Visitor<T>
|
||||
where T: AsMut<[u8]> + AsRef<[u8]> + Default
|
||||
{
|
||||
type Value = T;
|
||||
|
||||
fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||
fmt.write_str("a base64-encoded string")
|
||||
}
|
||||
|
||||
fn visit_bytes<E>(self, base64: &[u8]) -> Result<Self::Value, E>
|
||||
where E: serde::de::Error
|
||||
{
|
||||
where E: serde::de::Error {
|
||||
let mut deserialized = T::default();
|
||||
let estimated_length = (base64.len() + 3) / 4 * 3;
|
||||
if estimated_length > deserialized.as_ref().len() + 2 {
|
||||
Err(E::custom(format!("base64 parameter length {} > {}", estimated_length, deserialized.as_ref().len())))
|
||||
Err(E::custom(format!(
|
||||
"base64 parameter length {} > {}",
|
||||
estimated_length,
|
||||
deserialized.as_ref().len()
|
||||
)))
|
||||
} else {
|
||||
let data = decode(base64).map_err(|error| E::custom(error.to_string()))?;
|
||||
if data.len() != deserialized.as_ref().len() {
|
||||
Err(E::custom(format!("base64 parameter length {} != {}", data.len(), deserialized.as_ref().len())))
|
||||
Err(E::custom(format!(
|
||||
"base64 parameter length {} != {}",
|
||||
data.len(),
|
||||
deserialized.as_ref().len()
|
||||
)))
|
||||
} else {
|
||||
deserialized.as_mut().copy_from_slice(&data[..]);
|
||||
Ok(deserialized)
|
||||
@ -94,8 +101,7 @@ where T: AsMut<[u8]> + AsRef<[u8]> + Default
|
||||
}
|
||||
|
||||
fn visit_str<E>(self, base64: &str) -> Result<Self::Value, E>
|
||||
where E: serde::de::Error
|
||||
{
|
||||
where E: serde::de::Error {
|
||||
self.visit_bytes(base64.as_bytes())
|
||||
}
|
||||
}
|
||||
@ -110,5 +116,4 @@ pub trait SerdeFixedLengthBase64: Sized + AsMut<[u8]> + AsRef<[u8]> + Default {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: AsMut<[u8]> + AsRef<[u8]> + Default> SerdeFixedLengthBase64 for T {
|
||||
}
|
||||
impl<T: AsMut<[u8]> + AsRef<[u8]> + Default> SerdeFixedLengthBase64 for T {}
|
||||
|
||||
@ -15,24 +15,25 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use std::time::{Duration};
|
||||
use std::time::Duration;
|
||||
|
||||
use rand::Rng;
|
||||
|
||||
pub const NANOS_PER_SEC: u32 = 1_000_000_000;
|
||||
|
||||
pub fn random(max: Duration) -> Duration {
|
||||
let secs = rand::thread_rng().gen_range(0, max.as_secs().saturating_add(1));
|
||||
let secs = rand::thread_rng().gen_range(0, max.as_secs().saturating_add(1));
|
||||
let nanos = rand::thread_rng().gen_range(0, max.subsec_nanos().saturating_add(1));
|
||||
Duration::new(secs, nanos)
|
||||
}
|
||||
|
||||
pub fn as_ticks(duration: Duration, tick_interval: Duration) -> u32 {
|
||||
let duration_ms = duration.as_millis();
|
||||
let duration_ms = duration.as_millis();
|
||||
let tick_interval_ms = tick_interval.as_millis();
|
||||
let ticks = duration_ms.saturating_add(tick_interval_ms.saturating_sub(1))
|
||||
.checked_div(tick_interval_ms)
|
||||
.unwrap_or(0);
|
||||
let ticks = duration_ms
|
||||
.saturating_add(tick_interval_ms.saturating_sub(1))
|
||||
.checked_div(tick_interval_ms)
|
||||
.unwrap_or(0);
|
||||
ticks as u32
|
||||
}
|
||||
|
||||
@ -42,7 +43,7 @@ pub fn as_secs_f64(duration: Duration) -> f64 {
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use std::time::{Duration};
|
||||
use std::time::Duration;
|
||||
|
||||
use super::*;
|
||||
|
||||
@ -50,18 +51,18 @@ mod test {
|
||||
fn test_as_ticks() {
|
||||
let max_duration = Duration::new(u64::max_value(), NANOS_PER_SEC - 1);
|
||||
|
||||
assert_eq!(as_ticks(Duration::from_secs(10), Duration::from_secs(1)), 10);
|
||||
assert_eq!(as_ticks(Duration::from_secs(10), Duration::from_secs(0)), 0);
|
||||
assert_eq!(as_ticks(Duration::from_millis(100), Duration::from_millis(10)), 10);
|
||||
assert_eq!(as_ticks(Duration::from_millis(100), Duration::from_millis(11)), 10);
|
||||
assert_eq!(as_ticks(Duration::from_millis(100), Duration::from_millis(12)), 9);
|
||||
assert_eq!(as_ticks(Duration::from_millis(100), Duration::from_millis(99)), 2);
|
||||
assert_eq!(as_ticks(Duration::from_millis(100), Duration::from_millis(100)), 1);
|
||||
assert_eq!(as_ticks(Duration::from_secs(10), Duration::from_secs(1)), 10);
|
||||
assert_eq!(as_ticks(Duration::from_secs(10), Duration::from_secs(0)), 0);
|
||||
assert_eq!(as_ticks(Duration::from_millis(100), Duration::from_millis(10)), 10);
|
||||
assert_eq!(as_ticks(Duration::from_millis(100), Duration::from_millis(11)), 10);
|
||||
assert_eq!(as_ticks(Duration::from_millis(100), Duration::from_millis(12)), 9);
|
||||
assert_eq!(as_ticks(Duration::from_millis(100), Duration::from_millis(99)), 2);
|
||||
assert_eq!(as_ticks(Duration::from_millis(100), Duration::from_millis(100)), 1);
|
||||
assert_eq!(as_ticks(Duration::from_millis(100), Duration::from_millis(1000)), 1);
|
||||
|
||||
assert_eq!(as_ticks(max_duration, Duration::from_secs(0)), 0);
|
||||
assert_eq!(as_ticks(max_duration, max_duration), 1);
|
||||
assert_eq!(as_ticks(Duration::from_secs(0), max_duration), 0);
|
||||
assert_eq!(as_ticks(max_duration, Duration::from_secs(0)), 0);
|
||||
assert_eq!(as_ticks(max_duration, max_duration), 1);
|
||||
assert_eq!(as_ticks(Duration::from_secs(0), max_duration), 0);
|
||||
assert_eq!(as_ticks(Duration::from_secs(0), Duration::from_secs(0)), 0);
|
||||
|
||||
assert_eq!(as_ticks(Duration::from_millis(1), max_duration), 1);
|
||||
|
||||
@ -16,19 +16,18 @@
|
||||
*/
|
||||
|
||||
use std::fmt;
|
||||
use std::marker::{PhantomData};
|
||||
use std::marker::PhantomData;
|
||||
|
||||
use serde::{Deserializer, Serializer};
|
||||
|
||||
use super::{ToHex};
|
||||
use super::ToHex;
|
||||
|
||||
pub fn parse(hex: &str) -> Result<Vec<u8>, hex::FromHexError> {
|
||||
hex::decode(hex)
|
||||
}
|
||||
|
||||
pub fn parse_fixed<T>(hex: &str) -> Result<T, hex::FromHexError>
|
||||
where T: Sized + AsMut<[u8]> + AsRef<[u8]> + Default
|
||||
{
|
||||
where T: Sized + AsMut<[u8]> + AsRef<[u8]> + Default {
|
||||
let mut bytes = T::default();
|
||||
let () = hex::decode_to_slice(hex, bytes.as_mut())?;
|
||||
Ok(bytes)
|
||||
@ -50,12 +49,13 @@ struct HexVisitor;
|
||||
|
||||
impl<'de> serde::de::Visitor<'de> for HexVisitor {
|
||||
type Value = Vec<u8>;
|
||||
|
||||
fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||
fmt.write_str("a hexadecimal-encoded string")
|
||||
}
|
||||
|
||||
fn visit_str<E>(self, hex: &str) -> Result<Vec<u8>, E>
|
||||
where E: serde::de::Error
|
||||
{
|
||||
where E: serde::de::Error {
|
||||
parse(hex).map_err(|error| E::custom(format!("{}", error)))
|
||||
}
|
||||
}
|
||||
@ -70,12 +70,13 @@ impl<'de, T> serde::de::Visitor<'de> for FixedLengthHexVisitor<T>
|
||||
where T: AsMut<[u8]> + AsRef<[u8]> + Default
|
||||
{
|
||||
type Value = T;
|
||||
|
||||
fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||
fmt.write_str("a hexadecimal-encoded string")
|
||||
}
|
||||
|
||||
fn visit_str<E>(self, hex: &str) -> Result<Self::Value, E>
|
||||
where E: serde::de::Error
|
||||
{
|
||||
where E: serde::de::Error {
|
||||
parse_fixed(hex).map_err(|error| E::custom(format!("{}", error)))
|
||||
}
|
||||
}
|
||||
@ -90,8 +91,7 @@ pub trait SerdeFixedLengthHex: Sized + AsMut<[u8]> + AsRef<[u8]> + Default {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: AsMut<[u8]> + AsRef<[u8]> + Default> SerdeFixedLengthHex for T {
|
||||
}
|
||||
impl<T: AsMut<[u8]> + AsRef<[u8]> + Default> SerdeFixedLengthHex for T {}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
@ -99,9 +99,9 @@ mod test {
|
||||
|
||||
#[test]
|
||||
fn test_parse() {
|
||||
assert_eq!(&parse("").unwrap(), b"");
|
||||
assert_eq!(&parse("").unwrap(), b"");
|
||||
assert_eq!(&parse("616263").unwrap(), b"abc");
|
||||
assert_eq!(&parse("00fF").unwrap(), b"\x00\xFF");
|
||||
assert_eq!(&parse("00fF").unwrap(), b"\x00\xFF");
|
||||
|
||||
parse("\n").unwrap_err();
|
||||
parse(" ").unwrap_err();
|
||||
@ -121,16 +121,16 @@ mod test {
|
||||
|
||||
#[test]
|
||||
fn test_parse_fixed() {
|
||||
assert_eq!(&parse_fixed::<[u8; 0]>("").unwrap(), b"");
|
||||
assert_eq!(&parse_fixed::<[u8; 0]>("").unwrap(), b"");
|
||||
assert_eq!(&parse_fixed::<[u8; 3]>("616263").unwrap(), b"abc");
|
||||
assert_eq!(&parse_fixed::<[u8; 2]>("00fF").unwrap(), b"\x00\xFF");
|
||||
assert_eq!(&parse_fixed::<[u8; 2]>("00fF").unwrap(), b"\x00\xFF");
|
||||
|
||||
parse_fixed::<[u8; 1]>("").unwrap_err();
|
||||
parse_fixed::<[u8; 0]>("00").unwrap_err();
|
||||
parse_fixed::<[u8; 2]>("00").unwrap_err();
|
||||
|
||||
macro_rules! test_parse_fixed {
|
||||
($n:literal) => ({
|
||||
($n:literal) => {{
|
||||
parse_fixed::<[u8; $n]>("\n").unwrap_err();
|
||||
parse_fixed::<[u8; $n]>(" ").unwrap_err();
|
||||
parse_fixed::<[u8; $n]>(" 00").unwrap_err();
|
||||
@ -145,7 +145,7 @@ mod test {
|
||||
parse_fixed::<[u8; $n]>("\x00\x00").unwrap_err();
|
||||
parse_fixed::<[u8; $n]>("FF\x7F").unwrap_err();
|
||||
parse_fixed::<[u8; $n]>("000").unwrap_err();
|
||||
})
|
||||
}};
|
||||
}
|
||||
test_parse_fixed!(0);
|
||||
test_parse_fixed!(1);
|
||||
|
||||
@ -72,7 +72,7 @@ where T: fmt::Display
|
||||
let OptionDisplay(inner) = self;
|
||||
match inner {
|
||||
Some(inner) => fmt::Display::fmt(inner, fmt),
|
||||
None => write!(fmt, "<none>"),
|
||||
None => write!(fmt, "<none>"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -33,10 +33,9 @@ pub fn decode(pem: &[u8]) -> Vec<Vec<u8>> {
|
||||
}
|
||||
|
||||
pub fn encode<T>(tag: &str, certificates_der: impl IntoIterator<Item = T> + Clone) -> String
|
||||
where T: AsRef<[u8]>
|
||||
{
|
||||
const PEM_BEGIN: &'static str = "-----BEGIN ";
|
||||
const PEM_END: &'static str = "-----END ";
|
||||
where T: AsRef<[u8]> {
|
||||
const PEM_BEGIN: &'static str = "-----BEGIN ";
|
||||
const PEM_END: &'static str = "-----END ";
|
||||
const PEM_TAG_SUFFIX: &'static str = "-----\n";
|
||||
|
||||
let config = base64::Config::new(base64::CharacterSet::Standard, true);
|
||||
@ -55,11 +54,10 @@ where T: AsRef<[u8]>
|
||||
encoded.push_str(tag);
|
||||
encoded.push_str(PEM_TAG_SUFFIX);
|
||||
|
||||
let base64: String = base64::encode_config(certificate_der.as_ref(), config);
|
||||
let base64_bytes: &[u8] = base64.as_ref();
|
||||
let base64: String = base64::encode_config(certificate_der.as_ref(), config);
|
||||
let base64_bytes: &[u8] = base64.as_ref();
|
||||
for base64_line_bytes in base64_bytes.chunks(64) {
|
||||
let base64_line_str = str::from_utf8(base64_line_bytes)
|
||||
.unwrap_or_else(|_| unreachable!("base64 is ascii"));
|
||||
let base64_line_str = str::from_utf8(base64_line_bytes).unwrap_or_else(|_| unreachable!("base64 is ascii"));
|
||||
encoded.push_str(base64_line_str);
|
||||
encoded.push_str("\n");
|
||||
}
|
||||
@ -95,7 +93,10 @@ mod tests {
|
||||
assert_eq!(decode(b"-----BEGIN -----\ndGVzdA==\n-----END -----"), [b"test"]);
|
||||
assert_eq!(decode(b"-----BEGIN -----\t \r\n dGVzdA== \n\n-----END TEST-----"), [b"test"]);
|
||||
assert_eq!(decode(b"-----BEGIN TEST-----\t \r\n dGVzdA== \n\n-----END TEST-----"), [b"test"]);
|
||||
assert_eq!(decode(b"-----BEGIN TEST1 TEST2-----\t \r\n d\tG VzdA== \n\n-----END TEST1-TEST2-----"), [b"test"]);
|
||||
assert_eq!(
|
||||
decode(b"-----BEGIN TEST1 TEST2-----\t \r\n d\tG VzdA== \n\n-----END TEST1-TEST2-----"),
|
||||
[b"test"]
|
||||
);
|
||||
let test_certs = [b"test"];
|
||||
assert_eq!(decode(encode("", &test_certs).as_bytes()), test_certs);
|
||||
let test_certs = [b"test1", b"test2"];
|
||||
@ -108,7 +109,9 @@ mod tests {
|
||||
assert_eq!(encode("TEST", &no_certs), "");
|
||||
assert_eq!(encode("", &[b"test"]), "-----BEGIN -----\ndGVzdA==\n-----END -----\n");
|
||||
assert_eq!(encode("TEST", &[b""]), "-----BEGIN TEST-----\n-----END TEST-----\n");
|
||||
assert_eq!(encode("TEST", &[b"test1", b"test2"]),
|
||||
"-----BEGIN TEST-----\ndGVzdDE=\n-----END TEST-----\n-----BEGIN TEST-----\ndGVzdDI=\n-----END TEST-----\n");
|
||||
assert_eq!(
|
||||
encode("TEST", &[b"test1", b"test2"]),
|
||||
"-----BEGIN TEST-----\ndGVzdDE=\n-----END TEST-----\n-----BEGIN TEST-----\ndGVzdDI=\n-----END TEST-----\n"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@ -15,10 +15,10 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use std::thread;
|
||||
use std::thread::{JoinHandle};
|
||||
use std::time::*;
|
||||
use std::sync::*;
|
||||
use std::thread;
|
||||
use std::thread::JoinHandle;
|
||||
use std::time::*;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct StopJoinHandle<T> {
|
||||
@ -39,17 +39,19 @@ impl<T> StopJoinHandle<T> {
|
||||
join_handle: Arc::new(Mutex::new(Some(join_handle))),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn stop(&self) {
|
||||
let mut stopped_guard = match self.stop_state.stopped.lock() {
|
||||
Ok(guard) => guard,
|
||||
Ok(guard) => guard,
|
||||
Err(poison) => poison.into_inner(),
|
||||
};
|
||||
*stopped_guard = true;
|
||||
self.stop_state.condvar.notify_all();
|
||||
}
|
||||
|
||||
pub fn join(&self) -> Option<thread::Result<T>> {
|
||||
let mut join_handle_guard = match self.join_handle.lock() {
|
||||
Ok(guard) => guard,
|
||||
Ok(guard) => guard,
|
||||
Err(poison) => poison.into_inner(),
|
||||
};
|
||||
if let Some(join_handle) = join_handle_guard.take() {
|
||||
@ -62,7 +64,7 @@ impl<T> StopJoinHandle<T> {
|
||||
impl StopState {
|
||||
pub fn sleep_while_running(&self, duration: Duration) -> bool {
|
||||
let mut stopped_guard = match self.stopped.lock() {
|
||||
Ok(guard) => guard,
|
||||
Ok(guard) => guard,
|
||||
Err(poison) => poison.into_inner(),
|
||||
};
|
||||
let start = Instant::now();
|
||||
@ -72,11 +74,11 @@ impl StopState {
|
||||
}
|
||||
let timeout = match duration.checked_sub(start.elapsed()) {
|
||||
Some(timeout) => timeout,
|
||||
None => break true,
|
||||
None => break true,
|
||||
};
|
||||
stopped_guard = {
|
||||
let (stopped_guard, wait_timeout_result) = match self.condvar.wait_timeout(stopped_guard, timeout) {
|
||||
Ok(result) => result,
|
||||
Ok(result) => result,
|
||||
Err(poison) => poison.into_inner(),
|
||||
};
|
||||
if wait_timeout_result.timed_out() {
|
||||
@ -95,9 +97,9 @@ mod test {
|
||||
|
||||
#[test]
|
||||
fn test_stop_join() {
|
||||
let stop_state = Arc::new(StopState::default());
|
||||
let stop_state = Arc::new(StopState::default());
|
||||
let stop_state_2 = stop_state.clone();
|
||||
let join_handle = std::thread::spawn(move || {
|
||||
let join_handle = std::thread::spawn(move || {
|
||||
assert!(!stop_state.sleep_while_running(Duration::from_secs(60)));
|
||||
});
|
||||
let stop_join_handle = StopJoinHandle::new(stop_state_2, join_handle);
|
||||
@ -108,10 +110,10 @@ mod test {
|
||||
|
||||
#[test]
|
||||
fn test_sleep_while_running() {
|
||||
let stop_state = Arc::new(StopState::default());
|
||||
let stop_state = Arc::new(StopState::default());
|
||||
let stop_state_2 = stop_state.clone();
|
||||
let (tx, rx) = std::sync::mpsc::channel();
|
||||
let join_handle = std::thread::spawn(move || {
|
||||
let (tx, rx) = std::sync::mpsc::channel();
|
||||
let join_handle = std::thread::spawn(move || {
|
||||
assert!(stop_state.sleep_while_running(Duration::from_millis(1)));
|
||||
assert!(stop_state.sleep_while_running(Duration::from_millis(1)));
|
||||
let _ = tx.send(());
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
binop_separator = "Back"
|
||||
enum_discrim_align_threshold = 30
|
||||
license_template_path = "rustfmt.license-template"
|
||||
match_arm_align_threshold = 30
|
||||
max_width = 140
|
||||
overflow_delimited_expr = true
|
||||
reorder_impl_items = true
|
||||
|
||||
@ -15,6 +15,7 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#[rustfmt::skip]
|
||||
#[allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals, improper_ctypes)]
|
||||
mod bindgen_wrapper;
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user