(
ops: &[FileOps],
content: &[u8],
policy: SemanticEncoding,
)
| 62 | } |
| 63 | |
| 64 | pub(crate) fn select( |
| 65 | ops: &[FileOps], |
| 66 | content: &[u8], |
| 67 | policy: SemanticEncoding, |
| 68 | ) -> FormatResult<SelectedSemantic> { |
| 69 | if policy == SemanticEncoding::PredictiveV2 { |
| 70 | return compressed_predictive(ops, content); |
| 71 | } |
| 72 | if matches!( |
| 73 | policy, |
| 74 | SemanticEncoding::Compact | SemanticEncoding::CompactMixed |
| 75 | ) { |
| 76 | return compressed_compact(ops, content, policy != SemanticEncoding::CompactMixed); |
| 77 | } |
| 78 | let raw = postcard::to_allocvec(ops)?; |
| 79 | let compressed = |
| 80 | zstd::encode_all(raw.as_slice(), 3).map_err(|e| FormatError::Compress(e.to_string()))?; |
| 81 | let baseline = SelectedSemantic { |
| 82 | compact: false, |
| 83 | predictive: false, |
| 84 | compressed, |
| 85 | uncompressed_len: raw.len(), |
| 86 | fallback: None, |
| 87 | }; |
| 88 | // Do not impose compact-format limits on historical raw objects. Oversized |
| 89 | // new objects likewise keep the raw representation rather than losing data. |
| 90 | if policy == SemanticEncoding::Raw |
| 91 | || (policy == SemanticEncoding::Auto |
| 92 | && (raw.len() > wire::MAX_PAYLOAD || content.len() > wire::MAX_PAYLOAD)) |
| 93 | { |
| 94 | return Ok(baseline); |
| 95 | } |
| 96 | drop(raw); |
| 97 | let compact = match compressed_compact(ops, content, true) { |
| 98 | Ok(payload) => payload, |
| 99 | Err(FormatError::SemanticLimitExceeded) if policy == SemanticEncoding::Auto => { |
| 100 | return Ok(baseline) |
| 101 | } |
| 102 | Err(error) => return Err(error), |
| 103 | }; |
| 104 | let baseline = if compact.compressed.len() < baseline.compressed.len() { |
| 105 | compact |
| 106 | } else { |
| 107 | baseline |
| 108 | }; |
| 109 | match compressed_predictive(ops, content) { |
| 110 | Ok(mut p) if p.compressed.len() < baseline.compressed.len() => { |
| 111 | p.fallback = Some(Box::new(baseline)); |
| 112 | Ok(p) |
| 113 | } |
| 114 | Ok(_) | Err(FormatError::SemanticLimitExceeded) => Ok(baseline), |
| 115 | Err(e) => Err(e), |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | fn compressed_predictive(ops: &[FileOps], content: &[u8]) -> FormatResult<SelectedSemantic> { |
| 120 | let payload = predictive::encode(ops, content)?; |
no test coverage detected