Skip to main content

Codebase

Struct Codebase 

Source
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: u32

Schema version, see MODEL_SCHEMA_VERSION.

§name: String

Human 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

Source

pub fn new(name: impl Into<String>) -> Self

An empty codebase called name.

Source

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), .. })));
Source

pub fn add_file(&mut self, file: SourceFile)

Add or replace a file.

Source

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");
Source

pub fn add_relation(&mut self, relation: Relation)

Add a relation (duplicates are ignored). Self-loops are dropped.

Source

pub fn file(&self, path: &SourcePath) -> Option<&SourceFile>

Look up a file.

Source

pub fn symbol(&self, id: &SymbolId) -> Option<&Symbol>

Look up a symbol.

Source

pub fn is_internal(&self, id: &SymbolId) -> bool

true if id is defined inside the codebase.

Source

pub fn symbols_in_file<'a>( &'a self, path: &'a SourcePath, ) -> impl Iterator<Item = &'a Symbol>

Symbols defined in path, in id order.

Source

pub fn symbols_of_kind(&self, kind: SymbolKind) -> impl Iterator<Item = &Symbol>

Symbols of kind, in id order.

Source

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).

Source

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.

Source

pub fn relations_to<'a>( &'a self, id: &'a SymbolId, ) -> impl Iterator<Item = &'a Relation>

Relations whose target is id.

Source

pub fn relations_of_kind( &self, kind: RelationKind, ) -> impl Iterator<Item = &Relation>

Relations of one kind.

Source

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.

Source

pub fn stats(&self) -> CodebaseStats

Summary counts.

Trait Implementations§

Source§

impl Clone for Codebase

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Codebase

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Codebase

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Codebase

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Eq for Codebase

Source§

impl PartialEq for Codebase

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Serialize for Codebase

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for Codebase

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.