67 | 67 |
pub bytes: vec::IntoIter<u8>,
|
68 | 68 |
}
|
69 | 69 |
|
| 70 |
#[test]
|
| 71 |
fn reader_tests() {
|
| 72 |
assert!(Reader::from_slice(&[0x00]).read_twip() == Ok(-32.0));
|
| 73 |
assert!(Reader::from_slice(&[0x00]).read_prefix_int() == Ok(0x00));
|
| 74 |
assert!(Reader::from_slice(&[0x7f]).read_prefix_int() == Ok(0x7f));
|
| 75 |
assert!(Reader::from_slice(&[0x80,0xff]).read_prefix_int() == Ok(0xff));
|
| 76 |
}
|
| 77 |
|
70 | 78 |
impl Reader {
|
| 79 |
fn from_slice(lst: &[u8]) -> Self {
|
| 80 |
Reader { bytes: lst.to_vec().into_iter() }
|
| 81 |
}
|
71 | 82 |
/// This gets the next byte, or fails if it's out of input.
|
72 | 83 |
fn next(&mut self) -> Result<u8, String> {
|
73 | 84 |
Ok(try!(self.bytes.next().ok_or("out of input")))
|
|
116 | 127 |
fn continue_prefix_int(&mut self, mut left: u8, upper: u8) -> Result<u64, String> {
|
117 | 128 |
let mut ret = upper as u64;
|
118 | 129 |
while left > 0 {
|
119 | |
left -= 0;
|
| 130 |
left -= 1;
|
120 | 131 |
ret = (ret << 8) | try!(self.next()) as u64;
|
121 | 132 |
}
|
122 | 133 |
Ok(ret)
|
|
125 | 136 |
/// This reads a single V3D: three twips for the x/y/z, three twips
|
126 | 137 |
/// for the normal, and two ratios for the uv coords.
|
127 | 138 |
fn read_v3d(&mut self) -> Result<V3D, String> {
|
128 | |
Ok(V3D { pos: [ try!(self.read_twip()),
|
129 | |
try!(self.read_twip()),
|
130 | |
try!(self.read_twip()) ],
|
| 139 |
Ok(V3D { pos: [ try!(self.read_twip()) * 0.1,
|
| 140 |
try!(self.read_twip()) * 0.1,
|
| 141 |
try!(self.read_twip()) * 0.1],
|
131 | 142 |
nrm: [ try!(self.read_twip()),
|
132 | 143 |
try!(self.read_twip()),
|
133 | 144 |
try!(self.read_twip()) ],
|
|
143 | 154 |
{
|
144 | 155 |
let ct = try!(self.read_prefix_int());
|
145 | 156 |
let mut ret = Vec::with_capacity(ct as usize);
|
146 | |
for i in 0..ct {
|
147 | |
ret[i as usize] = try!(reader(self))
|
| 157 |
for _ in 0..ct {
|
| 158 |
ret.push(try!(reader(self)))
|
148 | 159 |
}
|
149 | 160 |
Ok(ret)
|
150 | 161 |
}
|