pub mod codec;
mod sections;
mod stage;
pub use sections::{
ContentReadError, ContentSectionsBuilder, ContentSegment, EncodedRecord, KNOWN,
SECTIONS_VERSION, write_folded,
};
pub use stage::{PackedRecord, Stage, StageWriter, StagedUnit, write_staged};
use codec::{Decoder, Encoder};
use crate::ids::{ContentKey, Digest128};
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub struct RecordKey(Digest128);
impl RecordKey {
#[must_use]
pub fn new(content: ContentKey, language: &str, extractor_version: &str) -> Self {
let mut hasher = blake3::Hasher::new_derive_key("aft-store 2026-11 content record key");
hasher.update(content.digest().as_bytes());
for part in [language, extractor_version] {
hasher.update(&(part.len() as u64).to_le_bytes());
hasher.update(part.as_bytes());
}
Self(Digest128::from_hash(&hasher.finalize()))
}
#[must_use]
pub const fn from_digest(digest: Digest128) -> Self {
Self(digest)
}
#[must_use]
pub const fn digest(&self) -> Digest128 {
self.0
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug, thiserror::Error)]
#[error("content record does not decode: {0}")]
pub struct DecodeError(pub &'static str);
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Symbol {
pub is_test: Option<bool>,
pub ordinal: u32,
pub name: String,
pub scoped_name: String,
pub kind: String,
pub exported: bool,
pub is_default_export: bool,
pub start_line: u32,
pub start_col: u32,
pub end_line: u32,
pub end_col: u32,
pub signature: Option<String>,
pub program_start: bool,
pub entry_attribute: Option<String>,
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
pub enum RefKind {
Construction,
CallbackRef,
Call,
ValueRef,
Import,
Module,
Reexport,
ExportAlias,
MacroMention,
}
impl RefKind {
const ALL: [Self; 9] = [
Self::Construction,
Self::CallbackRef,
Self::Call,
Self::ValueRef,
Self::Import,
Self::Module,
Self::Reexport,
Self::ExportAlias,
Self::MacroMention,
];
fn code(self) -> u8 {
Self::ALL
.iter()
.position(|kind| *kind == self)
.and_then(|index| u8::try_from(index).ok())
.expect("every kind is in ALL")
}
fn from_code(code: u8) -> Result<Self, DecodeError> {
Self::ALL
.get(usize::from(code))
.copied()
.ok_or(DecodeError("reference kind"))
}
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Reference {
pub ordinal: u32,
pub kind: RefKind,
pub caller_symbol: Option<String>,
pub short_name: Option<String>,
pub full_ref: Option<String>,
pub module_path: Option<String>,
pub line: u32,
pub byte_start: u64,
pub byte_end: u64,
pub path_override: Option<String>,
pub local_name: Option<String>,
pub requested_name: Option<String>,
pub namespace_alias: Option<String>,
pub wildcard: bool,
pub import_kind: Option<String>,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Import {
pub ordinal: u32,
pub module_path: String,
pub names: Vec<String>,
pub default_import: Option<String>,
pub namespace_import: Option<String>,
pub byte_start: u64,
pub byte_end: u64,
pub raw_text: String,
pub type_only: bool,
pub side_effect: bool,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct PythonBinding {
pub byte_start: u64,
pub byte_end: u64,
pub target: Option<(String, String)>,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct TypeFact {
pub name: String,
pub bases: Vec<String>,
pub interface: bool,
pub closed: bool,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct MethodFact {
pub owner: String,
pub trait_name: Option<String>,
pub name: String,
pub symbol: String,
pub has_body: bool,
pub shape: String,
pub private: bool,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct FieldFact {
pub owner: String,
pub name: String,
pub ty: String,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct ReturnFact {
pub symbol: String,
pub ty: String,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct CallSite {
pub ordinal: u32,
pub caller: Option<String>,
pub line: u32,
pub member: Option<String>,
pub receiver: Option<String>,
pub dynamic: bool,
}
#[derive(Clone, PartialEq, Eq, Debug, Default)]
pub struct Surface {
pub symbols: Vec<Symbol>,
pub default_export_symbol: Option<String>,
pub exported_symbols: Vec<String>,
pub callable_symbols: Vec<String>,
pub entry_point_symbols: Vec<String>,
pub go_package: Option<String>,
pub types: Vec<TypeFact>,
pub methods: Vec<MethodFact>,
pub fields: Vec<FieldFact>,
pub returns: Vec<ReturnFact>,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct OutlineSymbol {
pub name: String,
pub kind: String,
pub start_line: u32,
pub start_col: u32,
pub end_line: u32,
pub end_col: u32,
pub signature: Option<String>,
pub scope_chain: Vec<String>,
pub exported: bool,
pub parent: Option<String>,
}
#[derive(Clone, PartialEq, Eq, Debug, Default)]
pub struct Body {
pub imports: Vec<Import>,
pub python_bindings: Vec<PythonBinding>,
pub references: Vec<Reference>,
pub call_sites: Vec<CallSite>,
pub outline: Option<Vec<OutlineSymbol>>,
pub dead_code: Option<Vec<u8>>,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum RecordBody {
Parsed {
surface: Box<Surface>,
body: Box<Body>,
},
Config { source: Vec<u8> },
Markers,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct TodoMarker {
pub line: u32,
pub marker: String,
pub author: Option<String>,
pub text: String,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct TodoMarkers {
pub language: String,
pub items: Option<Vec<TodoMarker>>,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct ContentRecord {
pub language: String,
pub extractor_version: String,
pub data: RecordBody,
pub todos: Option<TodoMarkers>,
}
const PARSED: u8 = 1;
const CONFIG: u8 = 2;
const PARSED_WITH_OUTLINE: u8 = 3;
const PARSED_WITH_ENTRIES: u8 = 4;
const PARSED_WITH_ENTRIES_AND_OUTLINE: u8 = 5;
const PARSED_WITH_ENTRIES_OUTLINE_AND_DEAD_CODE: u8 = 6;
const PARSED_WITH_ENTRIES_AND_DEAD_CODE: u8 = 7;
const PARSED_WITH_TODOS: u8 = 8;
const CONFIG_WITH_TODOS: u8 = 9;
const MARKERS: u8 = 10;
const HAS_OUTLINE: u8 = 1;
const HAS_DEAD_CODE: u8 = 2;
impl ContentRecord {
#[must_use]
pub fn encode(&self) -> (Vec<u8>, Vec<u8>) {
let mut head = Encoder::default();
head.str(&self.language);
head.str(&self.extractor_version);
match &self.data {
RecordBody::Config { source } => {
match &self.todos {
None => head.byte(CONFIG),
Some(todos) => {
head.byte(CONFIG_WITH_TODOS);
encode_todos(&mut head, todos);
}
}
head.blob(source);
(head.finish(), Vec::new())
}
RecordBody::Markers => {
head.byte(MARKERS);
head.bool(self.todos.is_some());
if let Some(todos) = &self.todos {
encode_todos(&mut head, todos);
}
(head.finish(), Vec::new())
}
RecordBody::Parsed { surface, body } => {
match &self.todos {
None => head.byte(match (body.outline.is_some(), body.dead_code.is_some()) {
(true, true) => PARSED_WITH_ENTRIES_OUTLINE_AND_DEAD_CODE,
(true, false) => PARSED_WITH_ENTRIES_AND_OUTLINE,
(false, true) => PARSED_WITH_ENTRIES_AND_DEAD_CODE,
(false, false) => PARSED_WITH_ENTRIES,
}),
Some(todos) => {
head.byte(PARSED_WITH_TODOS);
let mut flags = 0;
if body.outline.is_some() {
flags |= HAS_OUTLINE;
}
if body.dead_code.is_some() {
flags |= HAS_DEAD_CODE;
}
head.byte(flags);
encode_todos(&mut head, todos);
}
}
encode_surface(&mut head, surface);
encode_entries(&mut head, surface);
let mut tail = Encoder::default();
encode_body(&mut tail, body);
(head.finish(), tail.finish())
}
}
}
pub fn decode(surface: &[u8], body: &[u8]) -> Result<Self, DecodeError> {
let mut head = Decoder::new(surface)?;
let language = head.string()?;
let extractor_version = head.string()?;
let mut todos = None;
let data = match head.byte()? {
kind @ (CONFIG | CONFIG_WITH_TODOS) => {
if kind == CONFIG_WITH_TODOS {
todos = Some(decode_todos(&mut head)?);
}
let source = head.blob()?.to_vec();
head.finish()?;
if !body.is_empty() {
return Err(DecodeError("a configuration record has a body"));
}
RecordBody::Config { source }
}
MARKERS => {
if head.bool()? {
todos = Some(decode_todos(&mut head)?);
}
head.finish()?;
if !body.is_empty() {
return Err(DecodeError("a markers record has a body"));
}
RecordBody::Markers
}
kind @ (PARSED
| PARSED_WITH_OUTLINE
| PARSED_WITH_ENTRIES
| PARSED_WITH_ENTRIES_AND_OUTLINE
| PARSED_WITH_ENTRIES_OUTLINE_AND_DEAD_CODE
| PARSED_WITH_ENTRIES_AND_DEAD_CODE
| PARSED_WITH_TODOS) => {
let (entries, outline, dead_code) = if kind == PARSED_WITH_TODOS {
let flags = head.byte()?;
if flags & !(HAS_OUTLINE | HAS_DEAD_CODE) != 0 {
return Err(DecodeError("parsed record flags"));
}
todos = Some(decode_todos(&mut head)?);
(true, flags & HAS_OUTLINE != 0, flags & HAS_DEAD_CODE != 0)
} else {
(
matches!(
kind,
PARSED_WITH_ENTRIES
| PARSED_WITH_ENTRIES_AND_OUTLINE
| PARSED_WITH_ENTRIES_OUTLINE_AND_DEAD_CODE
| PARSED_WITH_ENTRIES_AND_DEAD_CODE
),
matches!(
kind,
PARSED_WITH_OUTLINE
| PARSED_WITH_ENTRIES_AND_OUTLINE
| PARSED_WITH_ENTRIES_OUTLINE_AND_DEAD_CODE
),
matches!(
kind,
PARSED_WITH_ENTRIES_OUTLINE_AND_DEAD_CODE
| PARSED_WITH_ENTRIES_AND_DEAD_CODE
),
)
};
let mut surface = decode_surface(&mut head)?;
if entries {
decode_entries(&mut head, &mut surface)?;
}
head.finish()?;
let mut tail = Decoder::new(body)?;
let mut body = decode_body(&mut tail)?;
if outline {
body.outline = Some(decode_outline(&mut tail)?);
}
if dead_code {
body.dead_code = Some(tail.blob()?.to_vec());
}
tail.finish()?;
RecordBody::Parsed {
surface: Box::new(surface),
body: Box::new(body),
}
}
_ => return Err(DecodeError("record kind")),
};
Ok(Self {
language,
extractor_version,
data,
todos,
})
}
pub fn todos_of(surface: &[u8]) -> Result<Option<TodoMarkers>, DecodeError> {
let mut head = Decoder::new(surface)?;
head.str()?;
head.str()?;
match head.byte()? {
CONFIG_WITH_TODOS => decode_todos(&mut head).map(Some),
MARKERS if head.bool()? => decode_todos(&mut head).map(Some),
PARSED_WITH_TODOS => {
head.byte()?;
decode_todos(&mut head).map(Some)
}
CONFIG
| MARKERS
| PARSED
| PARSED_WITH_OUTLINE
| PARSED_WITH_ENTRIES
| PARSED_WITH_ENTRIES_AND_OUTLINE
| PARSED_WITH_ENTRIES_OUTLINE_AND_DEAD_CODE
| PARSED_WITH_ENTRIES_AND_DEAD_CODE => Ok(None),
_ => Err(DecodeError("record kind")),
}
}
}
fn encode_todos(out: &mut Encoder, todos: &TodoMarkers) {
out.str(&todos.language);
out.bool(todos.items.is_some());
if let Some(items) = &todos.items {
out.usize(items.len());
for item in items {
out.u32(item.line);
out.str(&item.marker);
out.opt_str(item.author.as_deref());
out.str(&item.text);
}
}
}
fn decode_todos(input: &mut Decoder<'_>) -> Result<TodoMarkers, DecodeError> {
let language = input.string()?;
let items = if input.bool()? {
let count = input.count()?;
let mut items = Vec::with_capacity(count);
for _ in 0..count {
items.push(TodoMarker {
line: input.u32()?,
marker: input.string()?,
author: input.opt_string()?,
text: input.string()?,
});
}
Some(items)
} else {
None
};
Ok(TodoMarkers { language, items })
}
fn encode_surface(out: &mut Encoder, surface: &Surface) {
out.usize(surface.symbols.len());
for symbol in &surface.symbols {
out.byte(match symbol.is_test {
None => 0,
Some(false) => 1,
Some(true) => 2,
});
out.u32(symbol.ordinal);
out.str(&symbol.name);
out.str(&symbol.scoped_name);
out.str(&symbol.kind);
out.bool(symbol.exported);
out.bool(symbol.is_default_export);
out.u32(symbol.start_line);
out.u32(symbol.start_col);
out.u32(symbol.end_line);
out.u32(symbol.end_col);
out.opt_str(symbol.signature.as_deref());
}
out.opt_str(surface.default_export_symbol.as_deref());
out.strs(&surface.exported_symbols);
out.strs(&surface.callable_symbols);
out.strs(&surface.entry_point_symbols);
out.opt_str(surface.go_package.as_deref());
out.usize(surface.types.len());
for fact in &surface.types {
out.str(&fact.name);
out.strs(&fact.bases);
out.bool(fact.interface);
out.bool(fact.closed);
}
out.usize(surface.methods.len());
for fact in &surface.methods {
out.str(&fact.owner);
out.opt_str(fact.trait_name.as_deref());
out.str(&fact.name);
out.str(&fact.symbol);
out.bool(fact.has_body);
out.str(&fact.shape);
out.bool(fact.private);
}
out.usize(surface.fields.len());
for fact in &surface.fields {
out.str(&fact.owner);
out.str(&fact.name);
out.str(&fact.ty);
}
out.usize(surface.returns.len());
for fact in &surface.returns {
out.str(&fact.symbol);
out.str(&fact.ty);
}
}
fn decode_surface(input: &mut Decoder<'_>) -> Result<Surface, DecodeError> {
let count = input.count()?;
let mut symbols = Vec::with_capacity(count);
for _ in 0..count {
symbols.push(Symbol {
is_test: match input.byte()? {
0 => None,
1 => Some(false),
2 => Some(true),
_ => return Err(DecodeError("is_test")),
},
ordinal: input.u32()?,
name: input.string()?,
scoped_name: input.string()?,
kind: input.string()?,
exported: input.bool()?,
is_default_export: input.bool()?,
start_line: input.u32()?,
start_col: input.u32()?,
end_line: input.u32()?,
end_col: input.u32()?,
signature: input.opt_string()?,
program_start: false,
entry_attribute: None,
});
}
let default_export_symbol = input.opt_string()?;
let exported_symbols = input.strings()?;
let callable_symbols = input.strings()?;
let entry_point_symbols = input.strings()?;
let go_package = input.opt_string()?;
let count = input.count()?;
let mut types = Vec::with_capacity(count);
for _ in 0..count {
types.push(TypeFact {
name: input.string()?,
bases: input.strings()?,
interface: input.bool()?,
closed: input.bool()?,
});
}
let count = input.count()?;
let mut methods = Vec::with_capacity(count);
for _ in 0..count {
methods.push(MethodFact {
owner: input.string()?,
trait_name: input.opt_string()?,
name: input.string()?,
symbol: input.string()?,
has_body: input.bool()?,
shape: input.string()?,
private: input.bool()?,
});
}
let count = input.count()?;
let mut fields = Vec::with_capacity(count);
for _ in 0..count {
fields.push(FieldFact {
owner: input.string()?,
name: input.string()?,
ty: input.string()?,
});
}
let count = input.count()?;
let mut returns = Vec::with_capacity(count);
for _ in 0..count {
returns.push(ReturnFact {
symbol: input.string()?,
ty: input.string()?,
});
}
Ok(Surface {
symbols,
default_export_symbol,
exported_symbols,
callable_symbols,
entry_point_symbols,
go_package,
types,
methods,
fields,
returns,
})
}
fn encode_entries(out: &mut Encoder, surface: &Surface) {
for symbol in &surface.symbols {
out.bool(symbol.program_start);
out.opt_str(symbol.entry_attribute.as_deref());
}
}
fn decode_entries(input: &mut Decoder<'_>, surface: &mut Surface) -> Result<(), DecodeError> {
for symbol in &mut surface.symbols {
symbol.program_start = input.bool()?;
symbol.entry_attribute = input.opt_string()?;
}
Ok(())
}
fn encode_body(out: &mut Encoder, body: &Body) {
out.usize(body.imports.len());
for import in &body.imports {
out.u32(import.ordinal);
out.str(&import.module_path);
out.strs(&import.names);
out.opt_str(import.default_import.as_deref());
out.opt_str(import.namespace_import.as_deref());
out.u64(import.byte_start);
out.u64(import.byte_end);
out.str(&import.raw_text);
out.bool(import.type_only);
out.bool(import.side_effect);
}
out.usize(body.python_bindings.len());
for binding in &body.python_bindings {
out.u64(binding.byte_start);
out.u64(binding.byte_end);
match &binding.target {
None => out.bool(false),
Some((module, symbol)) => {
out.bool(true);
out.str(module);
out.str(symbol);
}
}
}
out.usize(body.references.len());
for reference in &body.references {
out.u32(reference.ordinal);
out.byte(reference.kind.code());
out.opt_str(reference.caller_symbol.as_deref());
out.opt_str(reference.short_name.as_deref());
out.opt_str(reference.full_ref.as_deref());
out.opt_str(reference.module_path.as_deref());
out.u32(reference.line);
out.u64(reference.byte_start);
out.u64(reference.byte_end);
out.opt_str(reference.path_override.as_deref());
out.opt_str(reference.local_name.as_deref());
out.opt_str(reference.requested_name.as_deref());
out.opt_str(reference.namespace_alias.as_deref());
out.bool(reference.wildcard);
out.opt_str(reference.import_kind.as_deref());
}
out.usize(body.call_sites.len());
for site in &body.call_sites {
out.u32(site.ordinal);
out.opt_str(site.caller.as_deref());
out.u32(site.line);
out.opt_str(site.member.as_deref());
out.opt_str(site.receiver.as_deref());
out.bool(site.dynamic);
}
if let Some(outline) = &body.outline {
out.usize(outline.len());
for symbol in outline {
out.str(&symbol.name);
out.str(&symbol.kind);
out.u32(symbol.start_line);
out.u32(symbol.start_col);
out.u32(symbol.end_line);
out.u32(symbol.end_col);
out.opt_str(symbol.signature.as_deref());
out.strs(&symbol.scope_chain);
out.bool(symbol.exported);
out.opt_str(symbol.parent.as_deref());
}
}
if let Some(facts) = &body.dead_code {
out.blob(facts);
}
}
fn decode_body(input: &mut Decoder<'_>) -> Result<Body, DecodeError> {
let count = input.count()?;
let mut imports = Vec::with_capacity(count);
for _ in 0..count {
imports.push(Import {
ordinal: input.u32()?,
module_path: input.string()?,
names: input.strings()?,
default_import: input.opt_string()?,
namespace_import: input.opt_string()?,
byte_start: input.u64()?,
byte_end: input.u64()?,
raw_text: input.string()?,
type_only: input.bool()?,
side_effect: input.bool()?,
});
}
let count = input.count()?;
let mut python_bindings = Vec::with_capacity(count);
for _ in 0..count {
let byte_start = input.u64()?;
let byte_end = input.u64()?;
let target = if input.bool()? {
Some((input.string()?, input.string()?))
} else {
None
};
python_bindings.push(PythonBinding {
byte_start,
byte_end,
target,
});
}
let count = input.count()?;
let mut references = Vec::with_capacity(count);
for _ in 0..count {
references.push(Reference {
ordinal: input.u32()?,
kind: RefKind::from_code(input.byte()?)?,
caller_symbol: input.opt_string()?,
short_name: input.opt_string()?,
full_ref: input.opt_string()?,
module_path: input.opt_string()?,
line: input.u32()?,
byte_start: input.u64()?,
byte_end: input.u64()?,
path_override: input.opt_string()?,
local_name: input.opt_string()?,
requested_name: input.opt_string()?,
namespace_alias: input.opt_string()?,
wildcard: input.bool()?,
import_kind: input.opt_string()?,
});
}
let count = input.count()?;
let mut call_sites = Vec::with_capacity(count);
for _ in 0..count {
call_sites.push(CallSite {
ordinal: input.u32()?,
caller: input.opt_string()?,
line: input.u32()?,
member: input.opt_string()?,
receiver: input.opt_string()?,
dynamic: input.bool()?,
});
}
Ok(Body {
imports,
python_bindings,
references,
call_sites,
outline: None,
dead_code: None,
})
}
fn decode_outline(input: &mut Decoder<'_>) -> Result<Vec<OutlineSymbol>, DecodeError> {
let count = input.count()?;
let mut outline = Vec::with_capacity(count);
for _ in 0..count {
outline.push(OutlineSymbol {
name: input.string()?,
kind: input.string()?,
start_line: input.u32()?,
start_col: input.u32()?,
end_line: input.u32()?,
end_col: input.u32()?,
signature: input.opt_string()?,
scope_chain: input.strings()?,
exported: input.bool()?,
parent: input.opt_string()?,
});
}
Ok(outline)
}
crate::heap_bytes_none!(RecordKey, RefKind);
crate::heap_bytes_of_fields!(Symbol {
is_test,
ordinal,
name,
scoped_name,
kind,
exported,
is_default_export,
start_line,
start_col,
end_line,
end_col,
signature,
program_start,
entry_attribute,
});
crate::heap_bytes_of_fields!(Reference {
ordinal,
kind,
caller_symbol,
short_name,
full_ref,
module_path,
line,
byte_start,
byte_end,
path_override,
local_name,
requested_name,
namespace_alias,
wildcard,
import_kind,
});
crate::heap_bytes_of_fields!(Import {
ordinal,
module_path,
names,
default_import,
namespace_import,
byte_start,
byte_end,
raw_text,
type_only,
side_effect,
});
crate::heap_bytes_of_fields!(PythonBinding {
byte_start,
byte_end,
target,
});
crate::heap_bytes_of_fields!(TypeFact {
name,
bases,
interface,
closed,
});
crate::heap_bytes_of_fields!(MethodFact {
owner,
trait_name,
name,
symbol,
has_body,
shape,
private,
});
crate::heap_bytes_of_fields!(FieldFact { owner, name, ty });
crate::heap_bytes_of_fields!(ReturnFact { symbol, ty });
crate::heap_bytes_of_fields!(CallSite {
ordinal,
caller,
line,
member,
receiver,
dynamic,
});
crate::heap_bytes_of_fields!(Surface {
symbols,
default_export_symbol,
exported_symbols,
callable_symbols,
entry_point_symbols,
go_package,
types,
methods,
fields,
returns,
});
crate::heap_bytes_of_fields!(OutlineSymbol {
name,
kind,
start_line,
start_col,
end_line,
end_col,
signature,
scope_chain,
exported,
parent,
});
crate::heap_bytes_of_fields!(Body {
imports,
python_bindings,
references,
call_sites,
outline,
dead_code,
});
crate::heap_bytes_of_fields!(TodoMarker {
line,
marker,
author,
text,
});
crate::heap_bytes_of_fields!(TodoMarkers { language, items });
crate::heap_bytes_of_fields!(ContentRecord {
language,
extractor_version,
data,
todos,
});
impl crate::heap::HeapBytes for RecordBody {
fn heap_bytes(&self, seen: &mut crate::heap::Seen) -> usize {
match self {
Self::Parsed { surface, body } => {
crate::heap::HeapBytes::heap_bytes(surface, seen)
+ crate::heap::HeapBytes::heap_bytes(body, seen)
}
Self::Config { source } => crate::heap::HeapBytes::heap_bytes(source, seen),
Self::Markers => 0,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn sample() -> ContentRecord {
ContentRecord {
language: "rust".into(),
extractor_version: "v1".into(),
data: RecordBody::Parsed {
surface: Box::new(Surface {
symbols: vec![Symbol {
is_test: Some(false),
ordinal: 7,
name: "open".into(),
scoped_name: "Store::open".into(),
kind: "method".into(),
exported: true,
is_default_export: false,
start_line: 3,
start_col: 4,
end_line: 9,
end_col: 5,
signature: Some("fn open() -> Self".into()),
program_start: false,
entry_attribute: Some("no_mangle".into()),
}],
exported_symbols: vec!["Store".into()],
callable_symbols: vec!["Store::open".into()],
go_package: None,
types: vec![TypeFact {
name: "Store".into(),
bases: vec!["Base".into()],
interface: false,
closed: true,
}],
methods: vec![MethodFact {
owner: "Store".into(),
trait_name: None,
name: "open".into(),
symbol: "Store::open".into(),
has_body: true,
shape: "()".into(),
private: false,
}],
fields: vec![FieldFact {
owner: "Store".into(),
name: "path".into(),
ty: "PathBuf".into(),
}],
returns: vec![ReturnFact {
symbol: "Store::open".into(),
ty: "Store".into(),
}],
..Surface::default()
}),
body: Box::new(Body {
imports: vec![Import {
ordinal: 1,
module_path: "std::path".into(),
names: vec!["PathBuf".into()],
default_import: None,
namespace_import: None,
byte_start: 0,
byte_end: 22,
raw_text: "use std::path::PathBuf;".into(),
type_only: false,
side_effect: false,
}],
python_bindings: vec![PythonBinding {
byte_start: 2,
byte_end: 3,
target: Some(("m".into(), "f".into())),
}],
references: vec![Reference {
ordinal: 9,
kind: RefKind::Call,
caller_symbol: Some("Store::open".into()),
short_name: Some("new".into()),
full_ref: Some("PathBuf::new".into()),
module_path: None,
line: 5,
byte_start: 100,
byte_end: 112,
path_override: None,
local_name: None,
requested_name: None,
namespace_alias: None,
wildcard: false,
import_kind: None,
}],
call_sites: vec![CallSite {
ordinal: 11,
caller: Some("Store::open".into()),
line: 6,
member: Some("join".into()),
receiver: Some("self.path".into()),
dynamic: false,
}],
outline: None,
dead_code: None,
}),
},
todos: None,
}
}
fn todos(language: &str) -> TodoMarkers {
TodoMarkers {
language: language.into(),
items: Some(vec![
TodoMarker {
line: 4,
marker: "TODO".into(),
author: Some("ana".into()),
text: "close the store".into(),
},
TodoMarker {
line: 9,
marker: "FIXME".into(),
author: None,
text: String::new(),
},
]),
}
}
#[test]
fn todo_markers_round_trip_in_every_kind_and_read_from_the_first_part() {
let mut parsed = sample();
let RecordBody::Parsed { body, .. } = &mut parsed.data else {
unreachable!("the sample is parsed");
};
body.outline = Some(outline());
body.dead_code = Some(b"facts".to_vec());
let without = parsed.clone();
parsed.todos = Some(todos("rust"));
let config = ContentRecord {
language: "config".into(),
extractor_version: "v1".into(),
data: RecordBody::Config {
source: b"# TODO: x\n".to_vec(),
},
todos: Some(todos("toml")),
};
let markers = ContentRecord {
language: "markers-markdown".into(),
extractor_version: "v1".into(),
data: RecordBody::Markers,
todos: Some(todos("markdown")),
};
let not_text = ContentRecord {
todos: Some(TodoMarkers {
language: "markdown".into(),
items: None,
}),
..markers.clone()
};
let empty_markers = ContentRecord {
todos: None,
..markers.clone()
};
for record in [&parsed, &config, &markers, ¬_text, &empty_markers] {
let (surface, body) = record.encode();
assert_eq!(&ContentRecord::decode(&surface, &body).unwrap(), record);
assert_eq!(ContentRecord::todos_of(&surface).unwrap(), record.todos);
for cut in 0..surface.len() {
assert!(ContentRecord::decode(&surface[..cut], &body).is_err());
}
}
let (surface, body) = without.encode();
assert_eq!(ContentRecord::todos_of(&surface).unwrap(), None);
assert_eq!(ContentRecord::decode(&surface, &body).unwrap(), without);
let mut head = Decoder::new(&surface).unwrap();
head.str().unwrap();
head.str().unwrap();
assert_eq!(
head.byte().unwrap(),
PARSED_WITH_ENTRIES_OUTLINE_AND_DEAD_CODE
);
}
fn outline() -> Vec<OutlineSymbol> {
vec![
OutlineSymbol {
name: "Store".into(),
kind: "struct".into(),
start_line: 1,
start_col: 0,
end_line: 1,
end_col: 33,
signature: Some("pub struct Store".into()),
scope_chain: Vec::new(),
exported: true,
parent: None,
},
OutlineSymbol {
name: "open".into(),
kind: "method".into(),
start_line: 3,
start_col: 4,
end_line: 9,
end_col: 5,
signature: Some("fn open() -> Self".into()),
scope_chain: vec!["Store".into()],
exported: true,
parent: Some("Store".into()),
},
]
}
#[test]
fn an_outline_round_trips_and_its_absence_is_told_by_the_record_kind() {
let without = sample();
let mut with = sample();
let RecordBody::Parsed { body, .. } = &mut with.data else {
unreachable!("the sample is parsed");
};
body.outline = Some(outline());
let (surface, body) = with.encode();
assert_eq!(ContentRecord::decode(&surface, &body).unwrap(), with);
let (plain_surface, plain_body) = without.encode();
let decoded = ContentRecord::decode(&plain_surface, &plain_body).unwrap();
assert_eq!(decoded, without);
assert_eq!(surface.len(), plain_surface.len());
let differing = surface
.iter()
.zip(&plain_surface)
.filter(|(a, b)| a != b)
.map(|(a, b)| (*a, *b))
.collect::<Vec<_>>();
assert_eq!(
differing,
vec![(PARSED_WITH_ENTRIES_AND_OUTLINE, PARSED_WITH_ENTRIES)]
);
assert!(ContentRecord::decode(&surface, &plain_body).is_err());
assert!(ContentRecord::decode(&plain_surface, &body).is_err());
for cut in 0..body.len() {
assert!(ContentRecord::decode(&surface, &body[..cut]).is_err());
}
let mut empty = sample();
let RecordBody::Parsed { body, .. } = &mut empty.data else {
unreachable!("the sample is parsed");
};
body.outline = Some(Vec::new());
let (surface, body) = empty.encode();
assert_eq!(ContentRecord::decode(&surface, &body).unwrap(), empty);
}
#[test]
fn dead_code_facts_round_trip_with_and_without_an_outline() {
for outline in [None, Some(outline())] {
let without = {
let mut record = sample();
let RecordBody::Parsed { body, .. } = &mut record.data else {
unreachable!("the sample is parsed");
};
body.outline.clone_from(&outline);
record
};
let mut with = without.clone();
let RecordBody::Parsed { body, .. } = &mut with.data else {
unreachable!("the sample is parsed");
};
body.dead_code = Some(b"{\"facts\":1}".to_vec());
let (surface, body) = with.encode();
assert_eq!(ContentRecord::decode(&surface, &body).unwrap(), with);
let (plain_surface, plain_body) = without.encode();
assert_eq!(
ContentRecord::decode(&plain_surface, &plain_body).unwrap(),
without
);
assert!(ContentRecord::decode(&surface, &plain_body).is_err());
assert!(ContentRecord::decode(&plain_surface, &body).is_err());
for cut in 0..body.len() {
assert!(ContentRecord::decode(&surface, &body[..cut]).is_err());
}
}
}
#[test]
fn records_round_trip_and_refuse_damage() {
let record = sample();
let (surface, body) = record.encode();
assert_eq!(ContentRecord::decode(&surface, &body).unwrap(), record);
let text = String::from_utf8_lossy(&surface);
assert_eq!(text.matches("Store::open").count(), 1);
let config = ContentRecord {
language: "config".into(),
extractor_version: "v1".into(),
data: RecordBody::Config {
source: b"[package]\n".to_vec(),
},
todos: None,
};
let (head, tail) = config.encode();
assert!(tail.is_empty());
assert_eq!(ContentRecord::decode(&head, &tail).unwrap(), config);
for cut in 0..body.len() {
assert!(ContentRecord::decode(&surface, &body[..cut]).is_err());
}
for cut in 0..surface.len() {
assert!(ContentRecord::decode(&surface[..cut], &body).is_err());
}
}
#[test]
fn entry_facts_round_trip_and_older_records_read_without_them() {
let mut record = sample();
let RecordBody::Parsed { surface, .. } = &mut record.data else {
unreachable!("the sample is parsed");
};
surface.symbols[0].program_start = true;
let (head, tail) = record.encode();
assert_eq!(ContentRecord::decode(&head, &tail).unwrap(), record);
let RecordBody::Parsed { surface, body } = &record.data else {
unreachable!("the sample is parsed");
};
let mut old = Encoder::default();
old.str(&record.language);
old.str(&record.extractor_version);
old.byte(PARSED);
encode_surface(&mut old, surface);
let mut old_tail = Encoder::default();
encode_body(&mut old_tail, body);
let decoded = ContentRecord::decode(&old.finish(), &old_tail.finish()).unwrap();
let RecordBody::Parsed { surface, .. } = decoded.data else {
unreachable!("the record is parsed");
};
assert!(!surface.symbols[0].program_start);
assert_eq!(surface.symbols[0].entry_attribute, None);
assert_eq!(surface.symbols[0].scoped_name, "Store::open");
}
}