cairo_lang_semantic/items/
fmt.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use cairo_lang_debug::DebugWithDb;
use cairo_lang_defs::ids::NamedLanguageElementId;
use cairo_lang_utils::Upcast;

use super::constant::ConstValue;
use crate::db::SemanticGroup;
use crate::{ConcreteVariant, MatchArmSelector};

impl<Db: ?Sized + Upcast<dyn SemanticGroup + 'static>> DebugWithDb<Db> for ConstValue {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>, db: &Db) -> std::fmt::Result {
        let db = db.upcast();
        match self {
            ConstValue::Int(value, _ty) => write!(f, "{}", value),
            ConstValue::Struct(inner, _) => {
                write!(f, "{{")?;
                let mut inner = inner.iter().peekable();
                while let Some(value) = inner.next() {
                    write!(f, " ")?;
                    value.fmt(f, db)?;
                    write!(f, ": ")?;
                    value.ty(db).unwrap().fmt(f, db.elongate())?;
                    if inner.peek().is_some() {
                        write!(f, ",")?;
                    } else {
                        write!(f, " ")?;
                    }
                }
                write!(f, "}}")
            }
            ConstValue::Enum(variant, inner) => {
                variant.fmt(f, db)?;
                write!(f, "(")?;
                inner.fmt(f, db)?;
                write!(f, ")")
            }
            ConstValue::NonZero(value) => value.fmt(f, db),
            ConstValue::Boxed(value) => {
                value.fmt(f, db)?;
                write!(f, ".into_box()")
            }
            ConstValue::Generic(param) => write!(f, "{}", param.debug_name(db.upcast())),
            ConstValue::Var(var, _) => write!(f, "?{}", var.id.0),
            ConstValue::Missing(_) => write!(f, "missing"),
            ConstValue::ImplConstant(id) => id.fmt(f, db),
            ConstValue::TraitConstant(id) => id.fmt(f, db),
        }
    }
}

impl<Db: ?Sized + Upcast<dyn SemanticGroup + 'static>> DebugWithDb<Db> for ConcreteVariant {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>, db: &Db) -> std::fmt::Result {
        let db = db.upcast();
        let enum_name = self.concrete_enum_id.enum_id(db.upcast()).name(db.upcast());
        let variant_name = self.id.name(db.upcast());
        write!(f, "{enum_name}::{variant_name}")
    }
}

impl<Db: ?Sized + Upcast<dyn SemanticGroup + 'static>> DebugWithDb<Db> for MatchArmSelector {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>, db: &Db) -> std::fmt::Result {
        let db = db.upcast();
        match self {
            MatchArmSelector::VariantId(variant_id) => {
                write!(f, "{:?}", variant_id.debug(db))
            }
            MatchArmSelector::Value(s) => {
                write!(f, "{:?}", s.value)
            }
        }
    }
}