cfsp/node/attribute/
module.rs

1use serde::{Deserialize, Serialize};
2
3use crate::node::access_flag::{ExportsAccessFlag, OpensAccessFlag, RequiresAccessFlag};
4use crate::node::constant::{ConstantPool, Module, Package, Utf8};
5
6#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
7pub struct Requires {
8    pub requires_index: u16,
9    pub requires_flags: RequiresAccessFlag,
10    pub requires_version_index: u16,
11}
12
13impl Requires {
14    pub fn requires<'attribute, 'constant_pool: 'attribute>(
15        &'attribute self,
16        constant_pool: &'constant_pool ConstantPool,
17    ) -> Option<&'constant_pool Module> {
18        constant_pool.get_module(self.requires_index)
19    }
20
21    pub fn requires_version<'attribute, 'constant_pool: 'attribute>(
22        &'attribute self,
23        constant_pool: &'constant_pool ConstantPool,
24    ) -> Option<&'constant_pool Utf8> {
25        constant_pool.get_utf8(self.requires_version_index)
26    }
27}
28
29#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
30pub struct Exports {
31    pub exports_index: u16,
32    pub exports_flags: ExportsAccessFlag,
33    pub exports_to_count: u16,
34    pub exports_to_index: Vec<u16>,
35}
36
37impl Exports {
38    pub fn exports<'attribute, 'constant_pool: 'attribute>(
39        &'attribute self,
40        constant_pool: &'constant_pool ConstantPool,
41    ) -> Option<&'constant_pool Package> {
42        constant_pool.get_package(self.exports_index)
43    }
44
45    pub fn exports_to<'attribute, 'constant_pool: 'attribute>(
46        &'attribute self,
47        index: usize,
48        constant_pool: &'constant_pool ConstantPool,
49    ) -> Option<&'constant_pool Module> {
50        self.exports_to_index
51            .get(index)
52            .and_then(|exports_to_index| constant_pool.get_module(*exports_to_index))
53    }
54}
55
56#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
57pub struct Opens {
58    pub opens_index: u16,
59    pub opens_flags: OpensAccessFlag,
60    pub opens_to_count: u16,
61    pub opens_to_index: Vec<u16>,
62}
63
64impl Opens {
65    pub fn opens<'attribute, 'constant_pool: 'attribute>(
66        &'attribute self,
67        constant_pool: &'constant_pool ConstantPool,
68    ) -> Option<&'constant_pool Package> {
69        constant_pool.get_package(self.opens_index)
70    }
71
72    pub fn opens_to<'attribute, 'constant_pool: 'attribute>(
73        &'attribute self,
74        index: usize,
75        constant_pool: &'constant_pool ConstantPool,
76    ) -> Option<&'constant_pool Module> {
77        self.opens_to_index
78            .get(index)
79            .and_then(|opens_to_index| constant_pool.get_module(*opens_to_index))
80    }
81}
82
83#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Serialize, Deserialize)]
84pub struct Provides {
85    pub provides_index: u16,
86    pub provides_with_count: u16,
87    pub provides_with_index: Vec<u16>,
88}
89
90impl Provides {
91    pub fn provides<'attribute, 'constant_pool: 'attribute>(
92        &'attribute self,
93        constant_pool: &'constant_pool ConstantPool,
94    ) -> Option<&'constant_pool Package> {
95        constant_pool.get_package(self.provides_index)
96    }
97
98    pub fn provides_to<'attribute, 'constant_pool: 'attribute>(
99        &'attribute self,
100        index: usize,
101        constant_pool: &'constant_pool ConstantPool,
102    ) -> Option<&'constant_pool Module> {
103        self.provides_with_index
104            .get(index)
105            .and_then(|provides_with_index| constant_pool.get_module(*provides_with_index))
106    }
107}