cairo_lang_semantic/items/
fmt.rs1use cairo_lang_debug::DebugWithDb;
2use cairo_lang_defs::ids::NamedLanguageElementId;
3use salsa::Database;
4
5use super::constant::ConstValue;
6use crate::{ConcreteVariant, MatchArmSelector};
7
8impl<'db> DebugWithDb<'db> for ConstValue<'db> {
9 type Db = dyn Database;
10
11 fn fmt(&self, f: &mut std::fmt::Formatter<'_>, db: &'db dyn Database) -> std::fmt::Result {
12 match self {
13 ConstValue::Int(value, _ty) => write!(f, "{value}"),
14 ConstValue::Struct(inner, _) => {
15 write!(f, "{{")?;
16 let mut inner = inner.iter().peekable();
17 while let Some(value) = inner.next() {
18 write!(f, " ")?;
19 value.fmt(f, db)?;
20 write!(f, ": ")?;
21 value.ty(db).unwrap().fmt(f, db)?;
22 if inner.peek().is_some() {
23 write!(f, ",")?;
24 } else {
25 write!(f, " ")?;
26 }
27 }
28 write!(f, "}}")
29 }
30 ConstValue::Enum(variant, inner) => {
31 variant.fmt(f, db)?;
32 write!(f, "(")?;
33 inner.fmt(f, db)?;
34 write!(f, ")")
35 }
36 ConstValue::NonZero(value) => {
37 write!(f, "NonZero(")?;
38 value.fmt(f, db)?;
39 write!(f, ")")
40 }
41 ConstValue::Generic(param) => write!(f, "{}", param.debug_name(db).long(db)),
42 ConstValue::Var(var, _) => write!(f, "?{}", var.id.0),
43 ConstValue::Missing(_) => write!(f, "missing"),
44 ConstValue::ImplConstant(id) => id.fmt(f, db),
45 }
46 }
47}
48
49impl<'db> DebugWithDb<'db> for ConcreteVariant<'db> {
50 type Db = dyn Database;
51
52 fn fmt(&self, f: &mut std::fmt::Formatter<'_>, db: &'db dyn Database) -> std::fmt::Result {
53 let enum_name = self.concrete_enum_id.enum_id(db).name(db).long(db);
54 let variant_name = self.id.name(db).long(db);
55 write!(f, "{enum_name}::{variant_name}")
56 }
57}
58
59impl<'db> DebugWithDb<'db> for MatchArmSelector<'db> {
60 type Db = dyn Database;
61
62 fn fmt(
63 &self,
64 f: &mut std::fmt::Formatter<'_>,
65 semantic_db: &'db dyn Database,
66 ) -> std::fmt::Result {
67 match self {
68 MatchArmSelector::VariantId(variant_id) => {
69 write!(f, "{:?}", variant_id.debug(semantic_db))
70 }
71 MatchArmSelector::Value(s) => {
72 write!(f, "{:?}", s.value)
73 }
74 }
75 }
76}