#![allow(dead_code)]
use core::fmt;
use prost::Message;
pub fn encode_decode_round_trip<F>(s: &'static str)
where
F: Message + Default + TryFrom<&'static str> + PartialEq + fmt::Debug,
<F as TryFrom<&'static str>>::Error: fmt::Debug,
{
let original: F = s.try_into().unwrap();
let encoded = original.encode_to_vec();
let decoded = F::decode(&encoded[..]).unwrap();
assert_eq!(original, decoded);
}
pub fn encode_length_delimited_round_trip<F>(s: &'static str)
where
F: Message + Default + TryFrom<&'static str> + PartialEq + fmt::Debug,
<F as TryFrom<&'static str>>::Error: fmt::Debug,
{
let original: F = s.try_into().unwrap();
let encoded = original.encode_length_delimited_to_vec();
let decoded = F::decode_length_delimited(&encoded[..]).unwrap();
assert_eq!(original, decoded);
}
pub fn verify_wire_format<F>(s: &'static str)
where
F: Message + Default + TryFrom<&'static str> + fmt::Debug,
<F as TryFrom<&'static str>>::Error: fmt::Debug,
{
let original: F = s.try_into().unwrap();
let encoded = original.encode_to_vec();
assert_eq!(encoded, s.as_bytes(), "encoded bytes should be raw UTF-8");
assert_eq!(
original.encoded_len(),
s.len(),
"encoded_len should match byte length"
);
}
pub fn decode_empty<F>()
where
F: Message + Default + AsRef<str> + fmt::Debug,
{
let decoded = F::decode(&[][..]).unwrap();
assert_eq!(
decoded.as_ref(),
"",
"decoding empty buffer should give empty string"
);
}
pub fn clear_test<F>(s: &'static str)
where
F: Message + Default + TryFrom<&'static str> + AsRef<str> + fmt::Debug,
<F as TryFrom<&'static str>>::Error: fmt::Debug,
{
let mut value: F = s.try_into().unwrap();
value.clear();
assert_eq!(value.as_ref(), "", "clear should reset to empty string");
}
pub fn cross_type_wire_compat<F>(s: &'static str)
where
F: Message + Default + TryFrom<&'static str> + fmt::Debug,
<F as TryFrom<&'static str>>::Error: fmt::Debug,
{
let flex_val: F = s.try_into().unwrap();
let string_val = s.to_string();
let tag = 1u32;
let mut flex_buf = Vec::new();
prost::encoding::message::encode(tag, &flex_val, &mut flex_buf);
let mut string_buf = Vec::new();
prost::encoding::string::encode(tag, &string_val, &mut string_buf);
assert_eq!(
flex_buf, string_buf,
"message::encode(flex) and string::encode(String) should produce identical bytes"
);
let flex_len = prost::encoding::message::encoded_len(tag, &flex_val);
let string_len = prost::encoding::string::encoded_len(tag, &string_val);
assert_eq!(flex_len, string_len, "encoded_len should match");
}
pub fn encode_flex_decode_string<F>(s: &'static str)
where
F: Message + Default + TryFrom<&'static str> + fmt::Debug,
<F as TryFrom<&'static str>>::Error: fmt::Debug,
{
let flex_val: F = s.try_into().unwrap();
let tag = 1u32;
let mut buf = Vec::new();
prost::encoding::message::encode(tag, &flex_val, &mut buf);
let mut cursor = &buf[..];
let (decoded_tag, wire_type) = prost::encoding::decode_key(&mut cursor).unwrap();
assert_eq!(decoded_tag, tag);
let mut decoded = String::new();
prost::encoding::string::merge(
wire_type,
&mut decoded,
&mut cursor,
prost::encoding::DecodeContext::default(),
)
.unwrap();
assert_eq!(
decoded, s,
"String decoded from flex-encoded field should match"
);
}
pub fn encode_string_decode_flex<F>(s: &'static str)
where
F: Message + Default + AsRef<str> + fmt::Debug,
{
let string_val = s.to_string();
let tag = 1u32;
let mut buf = Vec::new();
prost::encoding::string::encode(tag, &string_val, &mut buf);
let mut cursor = &buf[..];
let (decoded_tag, wire_type) = prost::encoding::decode_key(&mut cursor).unwrap();
assert_eq!(decoded_tag, tag);
assert_eq!(wire_type, prost::encoding::WireType::LengthDelimited);
let mut decoded = F::default();
decoded.merge_length_delimited(cursor).unwrap();
assert_eq!(
decoded.as_ref(),
s,
"flex type decoded from String-encoded field should match"
);
}