Skip to main content

noak/reader/cpool/
value.rs

1use crate::error::*;
2use crate::mutf8::MStr;
3use crate::reader::cpool::{self, ConstantPool, Index};
4
5pub trait ToValue<'input> {
6    type Target;
7
8    fn retrieve_from(self, pool: &ConstantPool<'input>) -> Result<Self::Target, DecodeError>;
9}
10
11#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
12pub struct Class<'input> {
13    pub name: &'input MStr,
14}
15
16impl<'input> ToValue<'input> for Index<cpool::Class<'input>> {
17    type Target = Class<'input>;
18
19    fn retrieve_from(self, pool: &ConstantPool<'input>) -> Result<Self::Target, DecodeError> {
20        let this = pool.get(self)?;
21        Ok(Class {
22            name: pool.retrieve(this.name)?,
23        })
24    }
25}
26
27#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
28pub struct FieldRef<'input> {
29    pub class: Class<'input>,
30    pub name_and_type: NameAndType<'input>,
31}
32
33impl<'input> ToValue<'input> for Index<cpool::FieldRef<'input>> {
34    type Target = FieldRef<'input>;
35
36    fn retrieve_from(self, pool: &ConstantPool<'input>) -> Result<Self::Target, DecodeError> {
37        let this = pool.get(self)?;
38        Ok(FieldRef {
39            class: pool.retrieve(this.class)?,
40            name_and_type: pool.retrieve(this.name_and_type)?,
41        })
42    }
43}
44
45#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
46pub struct MethodRef<'input> {
47    pub class: Class<'input>,
48    pub name_and_type: NameAndType<'input>,
49}
50
51impl<'input> ToValue<'input> for Index<cpool::MethodRef<'input>> {
52    type Target = MethodRef<'input>;
53
54    fn retrieve_from(self, pool: &ConstantPool<'input>) -> Result<Self::Target, DecodeError> {
55        let this = pool.get(self)?;
56        Ok(MethodRef {
57            class: pool.retrieve(this.class)?,
58            name_and_type: pool.retrieve(this.name_and_type)?,
59        })
60    }
61}
62
63#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
64pub struct InterfaceMethodRef<'input> {
65    pub class: Class<'input>,
66    pub name_and_type: NameAndType<'input>,
67}
68
69impl<'input> ToValue<'input> for Index<cpool::InterfaceMethodRef<'input>> {
70    type Target = InterfaceMethodRef<'input>;
71
72    fn retrieve_from(self, pool: &ConstantPool<'input>) -> Result<Self::Target, DecodeError> {
73        let this = pool.get(self)?;
74        Ok(InterfaceMethodRef {
75            class: pool.retrieve(this.class)?,
76            name_and_type: pool.retrieve(this.name_and_type)?,
77        })
78    }
79}
80
81#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
82pub struct String<'input> {
83    pub string: &'input MStr,
84}
85
86impl<'input> ToValue<'input> for Index<cpool::String<'input>> {
87    type Target = String<'input>;
88
89    fn retrieve_from(self, pool: &ConstantPool<'input>) -> Result<Self::Target, DecodeError> {
90        let this = pool.get(self)?;
91        Ok(String {
92            string: pool.retrieve(this.string)?,
93        })
94    }
95}
96
97#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
98pub struct Integer {
99    pub value: i32,
100}
101
102impl<'input> ToValue<'input> for Index<cpool::Integer> {
103    type Target = Integer;
104
105    fn retrieve_from(self, pool: &ConstantPool<'input>) -> Result<Self::Target, DecodeError> {
106        let this = pool.get(self)?;
107        Ok(Integer { value: this.value })
108    }
109}
110
111#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
112pub struct Long {
113    pub value: i64,
114}
115
116impl<'input> ToValue<'input> for Index<cpool::Long> {
117    type Target = Long;
118
119    fn retrieve_from(self, pool: &ConstantPool<'input>) -> Result<Self::Target, DecodeError> {
120        let this = pool.get(self)?;
121        Ok(Long { value: this.value })
122    }
123}
124
125#[derive(Debug, Clone, PartialEq, PartialOrd)]
126pub struct Float {
127    pub value: f32,
128}
129
130impl<'input> ToValue<'input> for Index<cpool::Float> {
131    type Target = Float;
132
133    fn retrieve_from(self, pool: &ConstantPool<'input>) -> Result<Self::Target, DecodeError> {
134        let this = pool.get(self)?;
135        Ok(Float { value: this.value })
136    }
137}
138
139#[derive(Debug, Clone, PartialEq, PartialOrd)]
140pub struct Double {
141    pub value: f64,
142}
143
144impl<'input> ToValue<'input> for Index<cpool::Double> {
145    type Target = Double;
146
147    fn retrieve_from(self, pool: &ConstantPool<'input>) -> Result<Self::Target, DecodeError> {
148        let this = pool.get(self)?;
149        Ok(Double { value: this.value })
150    }
151}
152
153#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
154pub struct NameAndType<'input> {
155    pub name: &'input MStr,
156    pub descriptor: &'input MStr,
157}
158
159impl<'input> ToValue<'input> for Index<cpool::NameAndType<'input>> {
160    type Target = NameAndType<'input>;
161
162    fn retrieve_from(self, pool: &ConstantPool<'input>) -> Result<Self::Target, DecodeError> {
163        let this = pool.get(self)?;
164        Ok(NameAndType {
165            name: pool.retrieve(this.name)?,
166            descriptor: pool.retrieve(this.descriptor)?,
167        })
168    }
169}
170
171impl<'input> ToValue<'input> for Index<cpool::Utf8<'input>> {
172    type Target = &'input MStr;
173
174    fn retrieve_from(self, pool: &ConstantPool<'input>) -> Result<Self::Target, DecodeError> {
175        let this = pool.get(self)?;
176        Ok(this.content)
177    }
178}
179
180#[derive(Debug, Clone, PartialEq, PartialOrd)]
181pub struct MethodHandle<'input> {
182    pub kind: cpool::MethodKind,
183    pub reference: cpool::Item<'input>,
184}
185
186impl<'input> ToValue<'input> for Index<cpool::MethodHandle<'input>> {
187    type Target = MethodHandle<'input>;
188
189    fn retrieve_from(self, pool: &ConstantPool<'input>) -> Result<Self::Target, DecodeError> {
190        let this = pool.get(self)?;
191        Ok(MethodHandle {
192            kind: this.kind,
193            reference: pool.get(this.reference)?.clone(),
194        })
195    }
196}
197
198#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
199pub struct MethodType<'input> {
200    pub descriptor: &'input MStr,
201}
202
203impl<'input> ToValue<'input> for Index<cpool::MethodType<'input>> {
204    type Target = MethodType<'input>;
205
206    fn retrieve_from(self, pool: &ConstantPool<'input>) -> Result<Self::Target, DecodeError> {
207        let this = pool.get(self)?;
208        Ok(MethodType {
209            descriptor: pool.retrieve(this.descriptor)?,
210        })
211    }
212}
213
214#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
215pub struct Dynamic<'input> {
216    // actually an index into the bootstrap method table
217    pub bootstrap_method_attr: u16,
218    pub name_and_type: NameAndType<'input>,
219}
220
221impl<'input> ToValue<'input> for Index<cpool::Dynamic<'input>> {
222    type Target = Dynamic<'input>;
223
224    fn retrieve_from(self, pool: &ConstantPool<'input>) -> Result<Self::Target, DecodeError> {
225        let this = pool.get(self)?;
226        Ok(Dynamic {
227            bootstrap_method_attr: this.bootstrap_method_attr,
228            name_and_type: pool.retrieve(this.name_and_type)?,
229        })
230    }
231}
232
233#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
234pub struct InvokeDynamic<'input> {
235    // actually an index into the bootstrap method table
236    pub bootstrap_method_attr: u16,
237    pub name_and_type: NameAndType<'input>,
238}
239
240impl<'input> ToValue<'input> for Index<cpool::InvokeDynamic<'input>> {
241    type Target = InvokeDynamic<'input>;
242
243    fn retrieve_from(self, pool: &ConstantPool<'input>) -> Result<Self::Target, DecodeError> {
244        let this = pool.get(self)?;
245        Ok(InvokeDynamic {
246            bootstrap_method_attr: this.bootstrap_method_attr,
247            name_and_type: pool.retrieve(this.name_and_type)?,
248        })
249    }
250}
251
252#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
253pub struct Module<'input> {
254    pub name: &'input MStr,
255}
256
257impl<'input> ToValue<'input> for Index<cpool::Module<'input>> {
258    type Target = Module<'input>;
259
260    fn retrieve_from(self, pool: &ConstantPool<'input>) -> Result<Self::Target, DecodeError> {
261        let this = pool.get(self)?;
262        Ok(Module {
263            name: pool.retrieve(this.name)?,
264        })
265    }
266}
267
268#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
269pub struct Package<'input> {
270    pub name: &'input MStr,
271}
272
273impl<'input> ToValue<'input> for Index<cpool::Package<'input>> {
274    type Target = Package<'input>;
275
276    fn retrieve_from(self, pool: &ConstantPool<'input>) -> Result<Self::Target, DecodeError> {
277        let this = pool.get(self)?;
278        Ok(Package {
279            name: pool.retrieve(this.name)?,
280        })
281    }
282}