| 147 | Ok(out) |
| 148 | } |
| 149 | fn leaves(&mut self) -> FormatResult<Vec<LeafOp>> { |
| 150 | let count = self.count::<LeafOp>(roles::TOKENS)?; |
| 151 | let mut ops = Vec::new(); |
| 152 | ops.try_reserve_exact(count) |
| 153 | .map_err(|_| invalid("leaf allocation failed"))?; |
| 154 | for _ in 0..count { |
| 155 | ops.push(match self.value(roles::LEAF)? { |
| 156 | 0 => { |
| 157 | let after = if self.flag(roles::LEAF_AFTER)? { |
| 158 | Some(self.leaf()?) |
| 159 | } else { |
| 160 | None |
| 161 | }; |
| 162 | if let Some(p) = &self.predictive { |
| 163 | p.require_kind()?; |
| 164 | } |
| 165 | if self.predictive.is_some() { |
| 166 | let content = self.bytes()?; |
| 167 | let kind = self.predictive.as_mut().unwrap().kind(&content)?; |
| 168 | LeafOp::Insert { |
| 169 | after, |
| 170 | kind, |
| 171 | content, |
| 172 | } |
| 173 | } else { |
| 174 | let (kind, rest): (TokenKind, _) = postcard::take_from_bytes(self.c.kinds)?; |
| 175 | self.c.kinds = rest; |
| 176 | LeafOp::Insert { |
| 177 | after, |
| 178 | kind, |
| 179 | content: self.bytes()?, |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | 1 => LeafOp::Delete { leaf: self.leaf()? }, |
| 184 | 2 => LeafOp::Replace { |
| 185 | leaf: self.leaf()?, |
| 186 | new_content: self.bytes()?, |
| 187 | }, |
| 188 | 3 => LeafOp::Restore { leaf: self.leaf()? }, |
| 189 | _ => return Err(invalid("unknown leaf operation")), |
| 190 | }); |
| 191 | } |
| 192 | Ok(ops) |
| 193 | } |
| 194 | fn line(&mut self) -> FormatResult<LineOps> { |
| 195 | let id = self.branch()?; |
| 196 | let old = if self.flag(roles::OLD)? { |