| 228 | } |
| 229 | |
| 230 | pub(super) fn columns(ops: &[FileOps], content: &[u8], split_ids: bool) -> FormatResult<Columns> { |
| 231 | if content.len() > MAX_PAYLOAD { |
| 232 | return Err(super::super::FormatError::SemanticLimitExceeded); |
| 233 | } |
| 234 | let mut lines = HashMap::new(); |
| 235 | let mut offset = 0; |
| 236 | for line in content.split_inclusive(|b| *b == b'\n') { |
| 237 | lines.entry(line).or_insert(offset); |
| 238 | lines |
| 239 | .entry(line.strip_suffix(b"\n").unwrap_or(line)) |
| 240 | .or_insert(offset); |
| 241 | offset += line.len(); |
| 242 | } |
| 243 | let mut e = Encoder { |
| 244 | c: Columns { |
| 245 | split_ids, |
| 246 | content_hash: *blake3::hash(content).as_bytes(), |
| 247 | ..Columns::default() |
| 248 | }, |
| 249 | s: State::default(), |
| 250 | content, |
| 251 | lines, |
| 252 | literals: HashMap::new(), |
| 253 | remaining: MAX_EXPANDED, |
| 254 | }; |
| 255 | charge(&mut e.remaining, ops.len(), FileOps::COST)?; |
| 256 | put(&mut e.c.control, ops.len() as u64); |
| 257 | for file in ops { |
| 258 | let metadata = postcard::to_allocvec(&(file.trunk_id(), file.path(), file.trunk_op()))?; |
| 259 | charge(&mut e.remaining, metadata.len(), 1)?; |
| 260 | e.c.metadata.extend(metadata); |
| 261 | charge(&mut e.remaining, file.line_count(), LineOps::COST)?; |
| 262 | put(&mut e.c.control, file.line_count() as u64); |
| 263 | for line in file.line_ops() { |
| 264 | e.line(line)?; |
| 265 | } |
| 266 | } |
| 267 | Ok(e.c) |
| 268 | } |