pub struct Codebase {
pub schema_version: u32,
pub name: String,
pub files: BTreeMap<SourcePath, SourceFile>,
pub symbols: BTreeMap<SymbolId, Symbol>,
pub relations: BTreeSet<Relation>,
}Expand description
The whole analysed codebase: files, symbols and relations.
All collections are ordered, so serialising a Codebase (for example with
serde_json) is deterministic. Query methods return iterators in key
order.
Fields§
§schema_version: u32Schema version, see MODEL_SCHEMA_VERSION.
name: StringHuman name of the codebase (workspace or package name).
files: BTreeMap<SourcePath, SourceFile>Files keyed by path.
symbols: BTreeMap<SymbolId, Symbol>Symbols keyed by id.
relations: BTreeSet<Relation>All relations, ordered.
Implementations§
Source§impl Codebase
impl Codebase
Sourcepub fn from_json(text: &str) -> Result<Self, ModelJsonError>
pub fn from_json(text: &str) -> Result<Self, ModelJsonError>
Read a model serialised as JSON, refusing any schema version other
than MODEL_SCHEMA_VERSION.
use sealmap_model::{Codebase, ModelJsonError};
let json = serde_json::to_string(&Codebase::new("demo")).unwrap();
assert_eq!(Codebase::from_json(&json).unwrap().name, "demo");
let v1 = r#"{"schema":1,"name":"old","files":{},"symbols":{},"relations":[]}"#;
assert!(matches!(Codebase::from_json(v1), Err(ModelJsonError::Version { found: Some(1), .. })));Sourcepub fn add_file(&mut self, file: SourceFile)
pub fn add_file(&mut self, file: SourceFile)
Add or replace a file.
Sourcepub fn add_symbol(&mut self, symbol: Symbol)
pub fn add_symbol(&mut self, symbol: Symbol)
Add a symbol. If the id already exists the two are merged: the first
definition wins for scalar fields, list fields are concatenated, and
the fingerprints are folded with Fingerprint::merge
so an edit to either definition shows. This handles e.g.
#[cfg]-gated duplicate definitions deterministically.
Flows are kept, not dropped: twins whose flows differ become the arms
of one Step::Branch, one arm per definition, each labelled
cfg twin at <file>:<line> (an arm is empty when its definition makes
no calls). Flows that differ only in line numbers stay one flat flow.
use sealmap_model::*;
let id = |s: &str| SymbolId::parse(s).unwrap();
let file = SourcePath::new("src/lib.rs").unwrap();
let twin = |line, callee: &str| {
let mut s = Symbol::new(id("sym:cargo app . plat()."), "plat", SymbolKind::Function, file.clone());
s.span.start_line = line;
s.flow = Some(Flow::new(vec![Step::Call(Call::new(id(callee), "f", Confidence::Exact))]));
s
};
let mut cb = Codebase::new("app");
cb.add_symbol(twin(2, "sym:cargo app . unix_impl()."));
cb.add_symbol(twin(5, "sym:cargo app . windows_impl()."));
let flow = cb.symbol(&id("sym:cargo app . plat().")).unwrap().flow.as_ref().unwrap();
let names: Vec<_> = flow.calls().map(|c| c.target.name()).collect();
assert_eq!(names, ["unix_impl", "windows_impl"]);
let Step::Branch { arms } = &flow.steps[0] else { unreachable!() };
assert_eq!(arms[1].label, "cfg twin at src/lib.rs:5");Sourcepub fn add_relation(&mut self, relation: Relation)
pub fn add_relation(&mut self, relation: Relation)
Add a relation (duplicates are ignored). Self-loops are dropped.
Sourcepub fn file(&self, path: &SourcePath) -> Option<&SourceFile>
pub fn file(&self, path: &SourcePath) -> Option<&SourceFile>
Look up a file.
Sourcepub fn is_internal(&self, id: &SymbolId) -> bool
pub fn is_internal(&self, id: &SymbolId) -> bool
true if id is defined inside the codebase.
Sourcepub fn symbols_in_file<'a>(
&'a self,
path: &'a SourcePath,
) -> impl Iterator<Item = &'a Symbol>
pub fn symbols_in_file<'a>( &'a self, path: &'a SourcePath, ) -> impl Iterator<Item = &'a Symbol>
Symbols defined in path, in id order.
Sourcepub fn symbols_of_kind(&self, kind: SymbolKind) -> impl Iterator<Item = &Symbol>
pub fn symbols_of_kind(&self, kind: SymbolKind) -> impl Iterator<Item = &Symbol>
Symbols of kind, in id order.
Sourcepub fn children<'a>(
&'a self,
id: &'a SymbolId,
) -> impl Iterator<Item = &'a Symbol>
pub fn children<'a>( &'a self, id: &'a SymbolId, ) -> impl Iterator<Item = &'a Symbol>
Direct children of id (methods of a type, items of a module).
Sourcepub fn relations_from<'a>(
&'a self,
id: &'a SymbolId,
) -> impl Iterator<Item = &'a Relation>
pub fn relations_from<'a>( &'a self, id: &'a SymbolId, ) -> impl Iterator<Item = &'a Relation>
Relations whose source is id.
Relation orders by from first, so this is a range scan
(O(log n + k)), not a pass over every relation.
Sourcepub fn relations_to<'a>(
&'a self,
id: &'a SymbolId,
) -> impl Iterator<Item = &'a Relation>
pub fn relations_to<'a>( &'a self, id: &'a SymbolId, ) -> impl Iterator<Item = &'a Relation>
Relations whose target is id.
Sourcepub fn relations_of_kind(
&self,
kind: RelationKind,
) -> impl Iterator<Item = &Relation>
pub fn relations_of_kind( &self, kind: RelationKind, ) -> impl Iterator<Item = &Relation>
Relations of one kind.
Sourcepub fn owner_of(&self, id: &SymbolId) -> Option<SymbolId>
pub fn owner_of(&self, id: &SymbolId) -> Option<SymbolId>
The innermost type that owns id (the type for a method), or the
module for free items. This is the “participant” a symbol belongs to
in a sequence diagram.
For external ids the parent path is returned as-is.
Sourcepub fn stats(&self) -> CodebaseStats
pub fn stats(&self) -> CodebaseStats
Summary counts.