From 8829cc6cd019036bef5a1309b12e702d4fe0b4cf Mon Sep 17 00:00:00 2001 From: Sebastian Imlay Date: Sun, 6 Apr 2025 11:37:53 -0400 Subject: [PATCH 1/3] Re-add Display trait on descriptors --- src/descriptors.rs | 58 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/src/descriptors.rs b/src/descriptors.rs index 96f543b..8f58f15 100644 --- a/src/descriptors.rs +++ b/src/descriptors.rs @@ -1,4 +1,8 @@ -use std::borrow::Cow; +use std::{ + borrow::Cow, + fmt::{self, Write}, +}; + use crate::ParseError; @@ -49,6 +53,12 @@ impl<'a> ClassName<'a> { .fold(0, |sum, segment| sum + segment.name.len() + 1) } } +impl<'a> fmt::Display for ClassName<'a> { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + let segments : Vec> = self.segments.iter().map(|s| s.name.clone()).collect(); + write!(f, "{}", segments.join("/")) + } +} // Returns the classname descriptor at the start of the given data, and ignores anything following. // Returns an error if there was no such classname. @@ -96,6 +106,22 @@ impl<'a> FieldType<'a> { } } +impl<'a> fmt::Display for FieldType<'a> { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + match self { + Self::Byte => write!(f, "B"), + Self::Char => write!(f, "C"), + Self::Double => write!(f, "D"), + Self::Float => write!(f, "F"), + Self::Integer => write!(f, "I"), + Self::Long => write!(f, "J"), + Self::Short => write!(f, "S"), + Self::Boolean => write!(f, "Z"), + Self::Object(obj) => write!(f, "L{};", obj), + } + } +} + #[derive(Clone, Debug, Hash, PartialEq, Eq)] pub struct FieldDescriptor<'a> { pub dimensions: u8, @@ -108,6 +134,16 @@ impl<'a> FieldDescriptor<'a> { } } +impl<'a> fmt::Display for FieldDescriptor<'a> { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + if self.dimensions > 0 { + write!(f, "{}{}", "[".repeat(self.dimensions as usize), self.field_type) + } else { + write!(f, "{}", self.field_type) + } + } +} + // Parse the field descriptor at the start of the given data, and ignores anything // following. Returns an error if the data don't start with a field descriptor. pub(crate) fn parse_field_descriptor<'a>( @@ -148,6 +184,14 @@ pub enum ReturnDescriptor<'a> { Return(FieldDescriptor<'a>), Void, } +impl<'a> fmt::Display for ReturnDescriptor<'a> { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + match self { + Self::Void => f.write_char('V'), + Self::Return(field) => write!(f, "{}", field), + } + } +} impl<'a> ReturnDescriptor<'a> { fn byte_len(&self) -> usize { @@ -177,6 +221,18 @@ pub struct MethodDescriptor<'a> { pub return_type: ReturnDescriptor<'a>, } +impl<'a> fmt::Display for MethodDescriptor<'a> { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + f.write_char('(')?; + + for param in &self.parameters { + write!(f, "{}", param)?; + } + + write!(f, "){}", self.return_type) + } +} + impl<'a> MethodDescriptor<'a> { fn byte_len(&self) -> usize { 1 + self From 9f84a9d2e3c1f3661d947c18ba4bd6dea0297963 Mon Sep 17 00:00:00 2001 From: Sebastian Imlay Date: Sun, 6 Apr 2025 12:03:47 -0400 Subject: [PATCH 2/3] cargo fmt and some cilppy --- src/descriptors.rs | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/src/descriptors.rs b/src/descriptors.rs index 8f58f15..2dad6b2 100644 --- a/src/descriptors.rs +++ b/src/descriptors.rs @@ -3,7 +3,6 @@ use std::{ fmt::{self, Write}, }; - use crate::ParseError; #[derive(Clone, Debug, Hash, PartialEq, Eq)] @@ -55,7 +54,7 @@ impl<'a> ClassName<'a> { } impl<'a> fmt::Display for ClassName<'a> { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { - let segments : Vec> = self.segments.iter().map(|s| s.name.clone()).collect(); + let segments: Vec> = self.segments.iter().map(|s| s.name.clone()).collect(); write!(f, "{}", segments.join("/")) } } @@ -106,17 +105,17 @@ impl<'a> FieldType<'a> { } } -impl<'a> fmt::Display for FieldType<'a> { +impl fmt::Display for FieldType<'_> { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { match self { - Self::Byte => write!(f, "B"), - Self::Char => write!(f, "C"), - Self::Double => write!(f, "D"), - Self::Float => write!(f, "F"), - Self::Integer => write!(f, "I"), - Self::Long => write!(f, "J"), - Self::Short => write!(f, "S"), - Self::Boolean => write!(f, "Z"), + Self::Byte => write!(f, "B"), + Self::Char => write!(f, "C"), + Self::Double => write!(f, "D"), + Self::Float => write!(f, "F"), + Self::Integer => write!(f, "I"), + Self::Long => write!(f, "J"), + Self::Short => write!(f, "S"), + Self::Boolean => write!(f, "Z"), Self::Object(obj) => write!(f, "L{};", obj), } } @@ -134,10 +133,15 @@ impl<'a> FieldDescriptor<'a> { } } -impl<'a> fmt::Display for FieldDescriptor<'a> { +impl fmt::Display for FieldDescriptor<'_> { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { if self.dimensions > 0 { - write!(f, "{}{}", "[".repeat(self.dimensions as usize), self.field_type) + write!( + f, + "{}{}", + "[".repeat(self.dimensions as usize), + self.field_type + ) } else { write!(f, "{}", self.field_type) } @@ -184,7 +188,7 @@ pub enum ReturnDescriptor<'a> { Return(FieldDescriptor<'a>), Void, } -impl<'a> fmt::Display for ReturnDescriptor<'a> { +impl fmt::Display for ReturnDescriptor<'_> { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { match self { Self::Void => f.write_char('V'), @@ -221,7 +225,7 @@ pub struct MethodDescriptor<'a> { pub return_type: ReturnDescriptor<'a>, } -impl<'a> fmt::Display for MethodDescriptor<'a> { +impl fmt::Display for MethodDescriptor<'_> { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { f.write_char('(')?; From 7c91458ec222acf020c5aa872ca2ad39f309803b Mon Sep 17 00:00:00 2001 From: Sebastian Imlay Date: Sun, 6 Apr 2025 14:50:21 -0400 Subject: [PATCH 3/3] Updates from comments --- src/descriptors.rs | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/src/descriptors.rs b/src/descriptors.rs index 2dad6b2..d52c87b 100644 --- a/src/descriptors.rs +++ b/src/descriptors.rs @@ -55,7 +55,7 @@ impl<'a> ClassName<'a> { impl<'a> fmt::Display for ClassName<'a> { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { let segments: Vec> = self.segments.iter().map(|s| s.name.clone()).collect(); - write!(f, "{}", segments.join("/")) + write!(f, "{};", segments.join("/")) } } @@ -135,16 +135,12 @@ impl<'a> FieldDescriptor<'a> { impl fmt::Display for FieldDescriptor<'_> { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { - if self.dimensions > 0 { - write!( - f, - "{}{}", - "[".repeat(self.dimensions as usize), - self.field_type - ) - } else { - write!(f, "{}", self.field_type) - } + write!( + f, + "{}{}", + "[".repeat(self.dimensions as usize), + self.field_type + ) } }