cairo_lang_doc/
documentable_item.rs

1use cairo_lang_defs::diagnostic_utils::StableLocation;
2use cairo_lang_defs::ids::{
3    LanguageElementId, LookupItemId, MemberId, NamedLanguageElementId, VariantId,
4};
5use cairo_lang_filesystem::ids::{CrateId, SmolStrId};
6use salsa::Database;
7
8/// Item whose documentation can be fetched from source code.
9#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, salsa::Update)]
10pub enum DocumentableItemId<'db> {
11    Crate(CrateId<'db>),
12    LookupItem(LookupItemId<'db>),
13    Member(MemberId<'db>),
14    Variant(VariantId<'db>),
15}
16
17impl<'db> DocumentableItemId<'db> {
18    pub fn stable_location(&self, db: &'db dyn Database) -> Option<StableLocation<'db>> {
19        match self {
20            DocumentableItemId::Crate(_) => None,
21            DocumentableItemId::LookupItem(lookup_item_id) => {
22                Some(lookup_item_id.stable_location(db))
23            }
24            DocumentableItemId::Member(member_id) => Some(member_id.stable_location(db)),
25            DocumentableItemId::Variant(variant_id) => Some(variant_id.stable_location(db)),
26        }
27    }
28
29    /// Gets the name of the item.
30    pub fn name(&self, db: &'db dyn Database) -> SmolStrId<'db> {
31        match self {
32            DocumentableItemId::LookupItem(LookupItemId::ModuleItem(id)) => id.name(db),
33            DocumentableItemId::LookupItem(LookupItemId::ImplItem(id)) => id.name(db),
34            DocumentableItemId::LookupItem(LookupItemId::TraitItem(id)) => id.name(db),
35            DocumentableItemId::Crate(id) => id.long(db).name(),
36            DocumentableItemId::Member(id) => id.name(db),
37            DocumentableItemId::Variant(id) => id.name(db),
38        }
39    }
40}
41
42impl<'db> From<CrateId<'db>> for DocumentableItemId<'db> {
43    fn from(value: CrateId<'db>) -> Self {
44        DocumentableItemId::Crate(value)
45    }
46}
47
48impl<'db> From<LookupItemId<'db>> for DocumentableItemId<'db> {
49    fn from(value: LookupItemId<'db>) -> Self {
50        DocumentableItemId::LookupItem(value)
51    }
52}
53
54impl<'db> From<MemberId<'db>> for DocumentableItemId<'db> {
55    fn from(value: MemberId<'db>) -> Self {
56        DocumentableItemId::Member(value)
57    }
58}
59impl<'db> From<VariantId<'db>> for DocumentableItemId<'db> {
60    fn from(value: VariantId<'db>) -> Self {
61        DocumentableItemId::Variant(value)
62    }
63}