use std::borrow::Borrow;
use std::fmt;
use std::ops::{Deref, Range};
use std::sync::Arc;
use crate::facts::{FieldShape, ItemDetail, SpanFact, TestRole, Vis};
use crate::modtree::TargetId;
use crate::path::RelPath;
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct SharedStr(Arc<str>);
impl SharedStr {
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
impl Deref for SharedStr {
type Target = str;
fn deref(&self) -> &str {
&self.0
}
}
impl AsRef<str> for SharedStr {
fn as_ref(&self) -> &str {
&self.0
}
}
impl Borrow<str> for SharedStr {
fn borrow(&self) -> &str {
&self.0
}
}
impl From<&str> for SharedStr {
fn from(text: &str) -> Self {
Self(Arc::from(text))
}
}
impl From<String> for SharedStr {
fn from(text: String) -> Self {
Self(Arc::from(text))
}
}
impl fmt::Display for SharedStr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl fmt::Debug for SharedStr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&*self.0, f)
}
}
impl PartialEq<str> for SharedStr {
fn eq(&self, other: &str) -> bool {
*self.0 == *other
}
}
impl PartialEq<&str> for SharedStr {
fn eq(&self, other: &&str) -> bool {
*self.0 == **other
}
}
impl PartialEq<String> for SharedStr {
fn eq(&self, other: &String) -> bool {
*self.0 == **other
}
}
impl PartialEq<SharedStr> for str {
fn eq(&self, other: &SharedStr) -> bool {
*self == *other.0
}
}
impl PartialEq<SharedStr> for &str {
fn eq(&self, other: &SharedStr) -> bool {
**self == *other.0
}
}
impl PartialEq<SharedStr> for String {
fn eq(&self, other: &SharedStr) -> bool {
**self == *other.0
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[non_exhaustive]
pub enum Namespace {
Type,
Value,
Macro,
}
impl Namespace {
pub const ALL: [Namespace; 3] = [Namespace::Type, Namespace::Value, Namespace::Macro];
}
impl fmt::Display for Namespace {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::Type => "type",
Self::Value => "value",
Self::Macro => "macro",
})
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[non_exhaustive]
pub enum SymbolKind {
Fn,
Struct,
Enum,
Union,
Trait,
TypeAlias,
Const,
Static,
Mod,
MacroRules,
ExternCrate,
ForeignFn,
ForeignStatic,
ForeignType,
InherentMethod,
TraitImplMethod,
TraitDefaultMethod,
TraitRequiredMethod,
AssocConst,
AssocType,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ItemId(pub(crate) usize);
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct Unexpanded {
pub file: RelPath,
pub span: SpanFact,
pub module: SharedStr,
pub target: TargetId,
pub invoked: SharedStr,
pub test: TestRole,
pub identifiers: Vec<SharedStr>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct DisabledSyntax {
pub file: RelPath,
pub span: SpanFact,
pub module: SharedStr,
pub target: TargetId,
}
impl ItemId {
#[must_use]
pub const fn index(self) -> usize {
self.0
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[non_exhaustive]
pub enum TraitKey {
Canonical(String),
Unresolved {
written: String,
candidates: Vec<String>,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[non_exhaustive]
pub enum SelfType {
Local,
Trait,
Unresolved,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct UnresolvedImpl {
pub path: SharedStr,
pub self_type: SharedStr,
pub file: RelPath,
pub line: usize,
pub span: SpanFact,
pub target: TargetId,
pub test: TestRole,
pub members: Vec<ItemId>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ImplId(pub(crate) usize);
impl ImplId {
#[must_use]
pub const fn index(self) -> usize {
self.0
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct ImplBlock {
pub id: ImplId,
pub file: RelPath,
pub line: usize,
pub span: SpanFact,
pub module: SharedStr,
pub target: TargetId,
pub test: TestRole,
pub self_type: SharedStr,
pub trait_path: Option<SharedStr>,
pub self_items: Vec<ItemId>,
pub members: Vec<ItemId>,
pub(crate) module_index: usize,
pub(crate) unconditional_self: Option<usize>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct Item {
pub id: ItemId,
pub path: SharedStr,
pub name: SharedStr,
pub kind: SymbolKind,
pub namespace: Namespace,
pub file: RelPath,
pub name_line: usize,
pub span: SpanFact,
pub body: Option<SpanFact>,
pub(crate) detail: Option<Arc<ItemDetail>>,
pub visibility: Vis,
pub documented: bool,
pub doc_hidden: bool,
pub test: TestRole,
pub target: TargetId,
pub self_type: Option<SharedStr>,
pub trait_path: Option<SharedStr>,
pub trait_key: Option<Arc<TraitKey>>,
pub trait_definition_target: Option<TargetId>,
pub self_key: Option<SelfType>,
pub shape: Option<FieldShape>,
pub macro_export: bool,
}
impl Item {
#[must_use]
pub fn enum_roster_has_only_written_variants(&self) -> bool {
self.written_enum_variants().is_some()
}
pub(crate) fn written_enum_variants(&self) -> Option<&[crate::facts::VariantFact]> {
self.detail.as_ref()?.variants.as_deref()
}
#[must_use]
pub fn in_namespace(&self, namespace: Namespace) -> bool {
self.namespace == namespace
|| (namespace == Namespace::Value
&& self.kind == SymbolKind::Struct
&& matches!(self.shape, Some(FieldShape::Unit | FieldShape::Tuple)))
}
#[must_use]
pub fn parameter_bindings(&self) -> &[String] {
self.detail
.as_deref()
.map_or(&[], |detail| detail.bindings.as_slice())
}
#[must_use]
pub fn initializer(&self) -> Option<Range<usize>> {
self.detail
.as_deref()
.and_then(|detail| detail.initializer.clone())
}
#[must_use]
pub fn source_key(&self) -> (&RelPath, usize) {
(&self.file, self.span.start_byte)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn an_item_record_is_at_most_two_hundred_fifty_six_bytes() {
assert!(
size_of::<Item>() <= 256,
"an item record is {} bytes",
size_of::<Item>()
);
}
#[test]
fn shared_text_reads_and_compares_as_a_str() {
let text = SharedStr::from("demo::go");
assert_eq!(text, "demo::go");
assert_eq!(text, *"demo::go");
assert_eq!(text, "demo::go".to_owned());
assert_eq!("demo::go", text);
assert_eq!(text.as_str(), "demo::go");
assert_eq!(format!("{text} {text:?}"), "demo::go \"demo::go\"");
assert!(text.starts_with("demo"));
}
}