pub struct SymbolId(/* private fields */);Expand description
The canonical identity of a symbol. See the module docs for the grammar.
An id holds its canonical text (shared, so cloning is cheap). Because
printing is injective and only canonical text parses, two ids are equal
exactly when their texts are, and ids order by their text. A parent’s text
is a prefix of its children’s, so a parent always sorts before them.
Structural accessors (descriptors,
package, …) read the text on demand.
Implementations§
Source§impl SymbolId
impl SymbolId
Sourcepub fn global(package: Package, descriptors: Vec<Descriptor>) -> Self
pub fn global(package: Package, descriptors: Vec<Descriptor>) -> Self
A global id: package followed by descriptors (empty for the
package root).
Sourcepub fn package_root(package: Package) -> Self
pub fn package_root(package: Package) -> Self
The package root (a crate’s root module).
Sourcepub fn path<I, S>(segments: I) -> Result<Self, IdError>
pub fn path<I, S>(segments: I) -> Result<Self, IdError>
A path as written, for references whose kinds are unknown.
Sourcepub fn unresolved(name: impl Into<String>) -> Self
pub fn unresolved(name: impl Into<String>) -> Self
A method called on a receiver of unknown type.
Sourcepub fn parse(text: &str) -> Result<Self, IdError>
pub fn parse(text: &str) -> Result<Self, IdError>
Parse the canonical form. The exact inverse of Display:
only canonical text is accepted (a simple name in backticks, for
instance, is rejected), so parse(s) succeeding implies
parse(s)?.to_string() == s.
Sourcepub fn is_unresolved(&self) -> bool
pub fn is_unresolved(&self) -> bool
true for SymbolId::unresolved ids.
Sourcepub fn descriptors(&self) -> Vec<Descriptor>
pub fn descriptors(&self) -> Vec<Descriptor>
The descriptors of a global id (empty for the other forms).
Sourcepub fn last(&self) -> Option<Descriptor>
pub fn last(&self) -> Option<Descriptor>
The last descriptor of a global id.
Sourcepub fn name(&self) -> Cow<'_, str>
pub fn name(&self) -> Cow<'_, str>
The short name: the last descriptor’s name, the package name of a package root, the last path segment, or the unresolved method name.
assert_eq!(SymbolId::parse("sym:cargo a . b/C#go().").unwrap().name(), "go");
assert_eq!(SymbolId::parse("sym:cargo a .").unwrap().name(), "a");Sourcepub fn root(&self) -> Option<Cow<'_, str>>
pub fn root(&self) -> Option<Cow<'_, str>>
The first name: the package name, or the first path segment.
None for unresolved ids.
Sourcepub fn parent(&self) -> Option<SymbolId>
pub fn parent(&self) -> Option<SymbolId>
The enclosing definition. For a global id, the last descriptor is dropped, then any trailing type-parameter descriptors, so a method in a trait-impl block has its type as parent. For a path id, the last segment is dropped. A package root, a one-segment path and an unresolved id have no parent.
let m = SymbolId::parse("sym:cargo a . T#[Tr]f().").unwrap();
assert_eq!(m.parent().unwrap().to_string(), "sym:cargo a . T#");
assert_eq!(SymbolId::parse("sym:cargo a . T#").unwrap().parent().unwrap().to_string(), "sym:cargo a .");
assert!(SymbolId::parse("sym:cargo a .").unwrap().parent().is_none());Sourcepub fn child(&self, descriptor: Descriptor) -> Option<SymbolId>
pub fn child(&self, descriptor: Descriptor) -> Option<SymbolId>
Append a descriptor to a global id. None for the other forms,
which have no kinds to extend.
Sourcepub fn names(&self) -> Vec<Cow<'_, str>>
pub fn names(&self) -> Vec<Cow<'_, str>>
Every name in order: package and descriptor names for a global id, the segments of a path id, the name of an unresolved id.
Sourcepub fn extend_path(&self, segment: &str) -> SymbolId
pub fn extend_path(&self, segment: &str) -> SymbolId
Continue an id with a segment of unknown kind: the result is a path
id made of names plus segment. This is how a
reference that leaves known territory (krate::known::unknown) is
recorded.
let m = SymbolId::parse("sym:cargo a . net/").unwrap();
assert_eq!(m.extend_path("dial").to_string(), "sym:extern a::net::dial");Sourcepub fn view(&self) -> IdView<'_>
pub fn view(&self) -> IdView<'_>
A borrowed view of the id’s parts. Unlike package
and descriptors it allocates nothing but the
descriptor list (and a name only when it needs unescaping), so it
suits hot paths such as deriving a diagram id per call site.
use sealmap_model::{DescriptorKind, IdView, SymbolId};
let id = SymbolId::parse("sym:cargo shop . db/Db#[Store]put().").unwrap();
let IdView::Global(g) = id.view() else { unreachable!() };
assert_eq!((g.manager, &*g.package, g.version.is_none()), ("cargo", "shop", true));
let kinds: Vec<_> = g.descriptors.iter().map(|d| (d.kind, &*d.name)).collect();
assert_eq!(kinds, [
(DescriptorKind::Namespace, "db"),
(DescriptorKind::Type, "Db"),
(DescriptorKind::TypeParameter, "Store"),
(DescriptorKind::Method, "put"),
]);Sourcepub fn display_path(&self) -> String
pub fn display_path(&self) -> String
The names joined by ::, for human-facing labels. Not injective:
never use it as a key.