(
value: &T,
content: Option<&[u8]>,
sequential: bool,
)
| 476 | encode_context(value, Some(content), true) |
| 477 | } |
| 478 | fn encode_context<T: Serialize>( |
| 479 | value: &T, |
| 480 | content: Option<&[u8]>, |
| 481 | sequential: bool, |
| 482 | ) -> Result<Vec<u8>> { |
| 483 | let mut writer = Writer { |
| 484 | sequential, |
| 485 | ..Writer::default() |
| 486 | }; |
| 487 | if let Some(content) = content { |
| 488 | ensure!(content.len() <= LIMIT, "authored lookup limit"); |
| 489 | writer.c.content_hash = Some(*blake3::hash(content).as_bytes()); |
| 490 | writer.authored = content.to_vec(); |
| 491 | let mut base = 0; |
| 492 | for line in content.split_inclusive(|b| *b == b'\n') { |
| 493 | writer.content_lookup.entry(line.to_vec()).or_insert(base); |
| 494 | for token in atomic_core::diff::token::Tokenizer::new(line) { |
| 495 | let at = base + token.offset(); |
| 496 | let key = token.content(); |
| 497 | if content.get(at..at + key.len()) == Some(key) { |
| 498 | writer.content_lookup.entry(key.to_vec()).or_insert(at); |
| 499 | } |
| 500 | } |
| 501 | base += line.len(); |
| 502 | } |
| 503 | } |
| 504 | value.serialize(&mut writer)?; |
| 505 | ensure!( |
| 506 | writer.c.dictionary.len() <= 1_000_000, |
| 507 | "graph dictionary limit" |
| 508 | ); |
| 509 | let payload = postcard::to_allocvec(&writer.c)?; |
| 510 | ensure!(payload.len() <= LIMIT * 2, "graph frame limit"); |
| 511 | let mut out = MAGIC.to_vec(); |
| 512 | out.extend(zstd::encode_all(payload.as_slice(), 3)?); |
| 513 | Ok(out) |
| 514 | } |
| 515 | pub fn decode(encoded: &[u8]) -> Result<Vec<u8>> { |
| 516 | decode_context(encoded, None) |
| 517 | } |
no test coverage detected