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
use num_bigint::BigInt;
use num_traits::Num;
use smol_str::SmolStr;

#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct LiteralLongId {
    pub value: BigInt,
}

impl TryFrom<SmolStr> for LiteralLongId {
    type Error = ();

    fn try_from(text: SmolStr) -> Result<Self, Self::Error> {
        Ok(Self {
            value: if let Some(num_no_prefix) = text.strip_prefix("0x") {
                BigInt::from_str_radix(num_no_prefix, 16)
            } else if let Some(num_no_prefix) = text.strip_prefix("0o") {
                BigInt::from_str_radix(num_no_prefix, 8)
            } else if let Some(num_no_prefix) = text.strip_prefix("0b") {
                BigInt::from_str_radix(num_no_prefix, 2)
            } else {
                text.parse::<BigInt>()
            }
            .map_err(|_| ())?,
        })
    }
}

use cairo_lang_utils::define_short_id;

use crate::db::SemanticGroup;

define_short_id!(LiteralId, LiteralLongId, SemanticGroup, lookup_intern_literal);
impl LiteralId {
    pub fn format(&self, db: &dyn SemanticGroup) -> String {
        db.lookup_intern_literal(*self).value.to_string()
    }
}