test: format_allow_comments_in_byte_array

This commit is contained in:
rvcas 2025-03-23 23:14:50 -04:00
parent abcbe48267
commit e1104f4293
No known key found for this signature in database
GPG Key ID: C09B64E263F7D68C
2 changed files with 63 additions and 13 deletions

View File

@ -372,13 +372,24 @@ impl<'comments> Formatter<'comments> {
bytes, bytes,
preferred_format, preferred_format,
.. ..
} => self.bytearray(bytes, None, preferred_format), } => self.bytearray(
&bytes
.iter()
.map(|b| (*b, Span::empty()))
.collect::<Vec<(u8, Span)>>(),
None,
preferred_format,
),
TypedExpr::CurvePoint { TypedExpr::CurvePoint {
point, point,
preferred_format, preferred_format,
.. ..
} => self.bytearray( } => self.bytearray(
&point.compress(), &point
.compress()
.into_iter()
.map(|b| (b, Span::empty()))
.collect::<Vec<(u8, Span)>>(),
Some(point.as_ref().into()), Some(point.as_ref().into()),
preferred_format, preferred_format,
), ),
@ -895,7 +906,7 @@ impl<'comments> Formatter<'comments> {
pub fn bytearray<'a>( pub fn bytearray<'a>(
&mut self, &mut self,
bytes: &[u8], bytes: &[(u8, Span)],
curve: Option<CurveType>, curve: Option<CurveType>,
preferred_format: &ByteArrayFormatPreference, preferred_format: &ByteArrayFormatPreference,
) -> Document<'a> { ) -> Document<'a> {
@ -906,7 +917,9 @@ impl<'comments> Formatter<'comments> {
curve.map(|c| c.to_string()).unwrap_or_default(), curve.map(|c| c.to_string()).unwrap_or_default(),
)) ))
.append("\"") .append("\"")
.append(Document::String(hex::encode(bytes))) .append(Document::String(hex::encode(
bytes.iter().map(|(b, _)| *b).collect::<Vec<u8>>(),
)))
.append("\""), .append("\""),
ByteArrayFormatPreference::ArrayOfBytes(Base::Decimal { .. }) => "#" ByteArrayFormatPreference::ArrayOfBytes(Base::Decimal { .. }) => "#"
.to_doc() .to_doc()
@ -914,8 +927,19 @@ impl<'comments> Formatter<'comments> {
curve.map(|c| c.to_string()).unwrap_or_default(), curve.map(|c| c.to_string()).unwrap_or_default(),
)) ))
.append( .append(
flex_break("[", "[") break_("[", "[")
.append(join(bytes.iter().map(|b| b.to_doc()), break_(",", ", "))) .append(join(
bytes.iter().map(|b| {
let doc = b.0.to_doc();
if b.1 == Span::empty() {
doc
} else {
commented(doc, self.pop_comments(b.1.start))
}
}),
break_(",", ", "),
))
.nest(INDENT) .nest(INDENT)
.append(break_(",", "")) .append(break_(",", ""))
.append("]"), .append("]"),
@ -927,14 +951,20 @@ impl<'comments> Formatter<'comments> {
curve.map(|c| c.to_string()).unwrap_or_default(), curve.map(|c| c.to_string()).unwrap_or_default(),
)) ))
.append( .append(
flex_break("[", "[") break_("[", "[")
.append(join( .append(join(
bytes.iter().map(|b| { bytes.iter().map(|b| {
Document::String(if *b < 16 { let doc = Document::String(if b.0 < 16 {
format!("0x0{b:x}") format!("0x0{:x}", b.0)
} else { } else {
format!("{b:#x}") format!("{:#x}", b.0)
}) });
if b.1 == Span::empty() {
doc
} else {
commented(doc, self.pop_comments(b.1.start))
}
}), }),
break_(",", ", "), break_(",", ", "),
)) ))
@ -946,7 +976,8 @@ impl<'comments> Formatter<'comments> {
ByteArrayFormatPreference::Utf8String => nil() ByteArrayFormatPreference::Utf8String => nil()
.append("\"") .append("\"")
.append(Document::String(escape( .append(Document::String(escape(
core::str::from_utf8(bytes).unwrap(), core::str::from_utf8(&bytes.iter().map(|(b, _)| *b).collect::<Vec<u8>>())
.unwrap(),
))) )))
.append("\""), .append("\""),
} }
@ -1007,7 +1038,11 @@ impl<'comments> Formatter<'comments> {
preferred_format, preferred_format,
.. ..
} => self.bytearray( } => self.bytearray(
&point.compress(), &point
.compress()
.into_iter()
.map(|b| (b, Span::empty()))
.collect::<Vec<(u8, Span)>>(),
Some(point.as_ref().into()), Some(point.as_ref().into()),
preferred_format, preferred_format,
), ),

View File

@ -44,6 +44,21 @@ fn format_nul_byte() {
); );
} }
#[test]
fn format_allow_comments_in_byte_array() {
assert_format!(
r#"
pub const thing =
#[
// thing
0x12,
// wow
0x10,
]
"#
);
}
#[test] #[test]
fn format_g1_element_constant() { fn format_g1_element_constant() {
assert_format!( assert_format!(