lvm_class/attribute/
field.rs1use crate::attribute::{AttributeKind, SharedAttribute};
2use crate::constant_pool::ConstantPool;
3use crate::error::ClassFormatErr;
4use lvm_common::utils::cursor::ByteCursor;
5
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub enum FieldAttribute {
8 Shared(SharedAttribute),
9 ConstantValue { attr_name_idx: u16, value_idx: u16 },
10}
11
12impl FieldAttribute {
13 pub fn kind(&self) -> AttributeKind {
14 match self {
15 Self::Shared(attr) => attr.kind(),
16 Self::ConstantValue { .. } => AttributeKind::ConstantValue,
17 }
18 }
19}
20
21impl<'a> FieldAttribute {
22 pub(crate) fn read(
23 pool: &ConstantPool,
24 cursor: &mut ByteCursor<'a>,
25 ) -> Result<Self, ClassFormatErr> {
26 let attr_name_idx = cursor.u16()?;
27 let _attribute_length = cursor.u32()? as usize;
28
29 let attribute_kind = AttributeKind::try_from(pool.get_utf8(&attr_name_idx)?)?;
30 match attribute_kind {
31 AttributeKind::ConstantValue => Ok(FieldAttribute::ConstantValue {
32 attr_name_idx,
33 value_idx: cursor.u16()?,
34 }),
35 AttributeKind::RuntimeVisibleAnnotations
36 | AttributeKind::Synthetic
37 | AttributeKind::Deprecated
38 | AttributeKind::Signature => Ok(FieldAttribute::Shared(SharedAttribute::read(
39 attr_name_idx,
40 attribute_kind,
41 cursor,
42 )?)),
43 _ => unimplemented!(),
44 }
45 }
46}