noak/writer/attributes/
enclosing_method.rs

1use std::fmt;
2use std::marker::PhantomData;
3
4use crate::error::*;
5use crate::writer::{
6    attributes::{AttributeWriter, AttributeWriterState},
7    cpool,
8    encoding::*,
9};
10
11impl<Ctx: EncoderContext> AttributeWriter<Ctx, AttributeWriterState::Start> {
12    pub fn enclosing_method<F>(mut self, f: F) -> Result<AttributeWriter<Ctx, AttributeWriterState::End>, EncodeError>
13    where
14        F: FnOnce(
15            &mut ManyWriter<EnclosingMethodWriter<Ctx, EnclosingMethodWriterState::Class>, u16>,
16        ) -> Result<(), EncodeError>,
17    {
18        let length_writer = self.attribute_writer("EnclosingMethod")?;
19        let mut builder = ManyWriter::new(self.context)?;
20        f(&mut builder)?;
21        self.context = builder.finish()?;
22        length_writer.finish(&mut self.context)?;
23
24        Ok(AttributeWriter {
25            context: self.context,
26            _marker: PhantomData,
27        })
28    }
29}
30
31pub struct EnclosingMethodWriter<Ctx, State: EnclosingMethodWriterState::State> {
32    context: Ctx,
33    _marker: PhantomData<State>,
34}
35
36impl<Ctx: EncoderContext> EnclosingMethodWriter<Ctx, EnclosingMethodWriterState::Class> {
37    pub fn class<I>(
38        mut self,
39        class: I,
40    ) -> Result<EnclosingMethodWriter<Ctx, EnclosingMethodWriterState::Method>, EncodeError>
41    where
42        I: cpool::Insertable<cpool::Class>,
43    {
44        let index = class.insert(&mut self.context)?;
45        self.context.encoder().write(index)?;
46
47        Ok(EnclosingMethodWriter {
48            context: self.context,
49            _marker: PhantomData,
50        })
51    }
52}
53
54impl<Ctx: EncoderContext> EnclosingMethodWriter<Ctx, EnclosingMethodWriterState::Method> {
55    pub fn method<I>(
56        mut self,
57        class: Option<I>,
58    ) -> Result<EnclosingMethodWriter<Ctx, EnclosingMethodWriterState::End>, EncodeError>
59    where
60        I: cpool::Insertable<cpool::NameAndType>,
61    {
62        let index = class
63            .map(|class| Ok(Some(class.insert(&mut self.context)?)))
64            .unwrap_or(Ok(None))?;
65        self.context.encoder().write(index)?;
66
67        Ok(EnclosingMethodWriter {
68            context: self.context,
69            _marker: PhantomData,
70        })
71    }
72}
73
74impl<Ctx: EncoderContext> WriteAssembler for EnclosingMethodWriter<Ctx, EnclosingMethodWriterState::Class> {
75    type Context = Ctx;
76
77    fn new(context: Self::Context) -> Result<Self, EncodeError> {
78        Ok(EnclosingMethodWriter {
79            context,
80            _marker: PhantomData,
81        })
82    }
83}
84
85impl<Ctx: EncoderContext> WriteDisassembler for EnclosingMethodWriter<Ctx, EnclosingMethodWriterState::End> {
86    type Context = Ctx;
87
88    fn finish(self) -> Result<Self::Context, EncodeError> {
89        Ok(self.context)
90    }
91}
92
93impl<Ctx, State: EnclosingMethodWriterState::State> fmt::Debug for EnclosingMethodWriter<Ctx, State> {
94    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
95        f.debug_struct("EnclosingMethodWriter").finish()
96    }
97}
98
99enc_state!(pub mod EnclosingMethodWriterState: Class, Method, End);