in uniffi_udl/src/literal.rs [16:57]
fn convert_integer(literal: &weedle::literal::IntegerLit<'_>, type_: &Type) -> Result<Literal> {
let (string, radix) = match literal {
weedle::literal::IntegerLit::Dec(v) => (v.0, Radix::Decimal),
weedle::literal::IntegerLit::Hex(v) => (v.0, Radix::Hexadecimal),
weedle::literal::IntegerLit::Oct(v) => (v.0, Radix::Octal),
};
// This is the radix of the parsed number, passed to `from_str_radix`.
let src_radix = radix as u32;
// This radix tells the backends how to represent the number in the output languages.
let dest_radix = if string == "0" || string.starts_with('-') {
// 1. weedle parses "0" as an octal literal, but we most likely want to treat this as a decimal.
// 2. Explicitly negatively signed hex numbers won't convert via i64 very well if they're not 64 bit.
// For ease of implementation, output will use decimal.
Radix::Decimal
} else {
radix
};
// Clippy seems to think we should be using `strip_prefix` here, but
// it seems confused as to what this is actually doing.
#[allow(clippy::manual_strip)]
let string = if string.starts_with('-') {
("-".to_string() + string[1..].trim_start_matches("0x")).to_lowercase()
} else {
string.trim_start_matches("0x").to_lowercase()
};
Ok(match type_ {
Type::Int8 | Type::Int16 | Type::Int32 | Type::Int64 => Literal::Int(
i64::from_str_radix(&string, src_radix)?,
dest_radix,
type_.clone(),
),
Type::UInt8 | Type::UInt16 | Type::UInt32 | Type::UInt64 => Literal::UInt(
u64::from_str_radix(&string, src_radix)?,
dest_radix,
type_.clone(),
),
_ => bail!("Cannot coerce literal {} into a non-integer type", string),
})
}