1 use crate::{
2     BinaryReader, BinaryReaderError, ExternalKind, Range, Result, SectionIteratorLimited,
3     SectionReader, SectionWithLimitedItems,
4 };
5 
6 #[derive(Clone)]
7 pub struct AliasSectionReader<'a> {
8     reader: BinaryReader<'a>,
9     count: u32,
10 }
11 
12 #[derive(Clone, Debug)]
13 pub enum Alias<'a> {
14     OuterType {
15         relative_depth: u32,
16         index: u32,
17     },
18     OuterModule {
19         relative_depth: u32,
20         index: u32,
21     },
22     InstanceExport {
23         instance: u32,
24         kind: ExternalKind,
25         export: &'a str,
26     },
27 }
28 
29 impl<'a> AliasSectionReader<'a> {
new(data: &'a [u8], offset: usize) -> Result<AliasSectionReader<'a>>30     pub fn new(data: &'a [u8], offset: usize) -> Result<AliasSectionReader<'a>> {
31         let mut reader = BinaryReader::new_with_offset(data, offset);
32         let count = reader.read_var_u32()?;
33         Ok(AliasSectionReader { reader, count })
34     }
35 
original_position(&self) -> usize36     pub fn original_position(&self) -> usize {
37         self.reader.original_position()
38     }
39 
get_count(&self) -> u3240     pub fn get_count(&self) -> u32 {
41         self.count
42     }
43 
read(&mut self) -> Result<Alias<'a>>44     pub fn read(&mut self) -> Result<Alias<'a>> {
45         Ok(match self.reader.read_u8()? {
46             0x00 => Alias::InstanceExport {
47                 instance: self.reader.read_var_u32()?,
48                 kind: self.reader.read_external_kind()?,
49                 export: self.reader.read_string()?,
50             },
51             0x01 => {
52                 let relative_depth = self.reader.read_var_u32()?;
53                 match self.reader.read_external_kind()? {
54                     ExternalKind::Type => Alias::OuterType {
55                         relative_depth,
56                         index: self.reader.read_var_u32()?,
57                     },
58                     ExternalKind::Module => Alias::OuterModule {
59                         relative_depth,
60                         index: self.reader.read_var_u32()?,
61                     },
62                     _ => {
63                         return Err(BinaryReaderError::new(
64                             "invalid external kind in alias",
65                             self.original_position() - 1,
66                         ))
67                     }
68                 }
69             }
70             _ => {
71                 return Err(BinaryReaderError::new(
72                     "invalid byte in alias",
73                     self.original_position() - 1,
74                 ))
75             }
76         })
77     }
78 }
79 
80 impl<'a> SectionReader for AliasSectionReader<'a> {
81     type Item = Alias<'a>;
82 
read(&mut self) -> Result<Self::Item>83     fn read(&mut self) -> Result<Self::Item> {
84         AliasSectionReader::read(self)
85     }
eof(&self) -> bool86     fn eof(&self) -> bool {
87         self.reader.eof()
88     }
original_position(&self) -> usize89     fn original_position(&self) -> usize {
90         AliasSectionReader::original_position(self)
91     }
range(&self) -> Range92     fn range(&self) -> Range {
93         self.reader.range()
94     }
95 }
96 
97 impl<'a> SectionWithLimitedItems for AliasSectionReader<'a> {
get_count(&self) -> u3298     fn get_count(&self) -> u32 {
99         AliasSectionReader::get_count(self)
100     }
101 }
102 
103 impl<'a> IntoIterator for AliasSectionReader<'a> {
104     type Item = Result<Alias<'a>>;
105     type IntoIter = SectionIteratorLimited<AliasSectionReader<'a>>;
106 
into_iter(self) -> Self::IntoIter107     fn into_iter(self) -> Self::IntoIter {
108         SectionIteratorLimited::new(self)
109     }
110 }
111