cairo_lang_doc/
documentable_item.rs

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