/* This Source Code Form is subject to the terms of the Mozilla Public *License,v.2.0.IfacopyoftheMPLwasnotdistributedwiththis
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use anyhow::{bail, Result}; use uniffi_meta::{DefaultValueMetadata, LiteralMetadata, Radix, Type};
// We are able to use LiteralMetadata directly. pubtype Literal = LiteralMetadata;
// Convert weedle/udl syntax. pub(super) fn convert_default_value(
default_value: &weedle::literal::DefaultValue<'_>,
type_: &Type,
) -> Result<LiteralMetadata> { 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 (default_value, type_) {
(weedle::literal::DefaultValue::Boolean(b), Type::Boolean) => Literal::Boolean(b.0),
(weedle::literal::DefaultValue::String(s), Type::String) => { // Note that weedle doesn't parse escaped double quotes. // Keeping backends using double quotes (possible for all to date) // means we don't need to escape single quotes. But we haven't spent a lot of time // trying to break default values with weird escapes and quotes.
Literal::String(s.0.to_string())
}
(weedle::literal::DefaultValue::EmptyArray(_), Type::Sequence { .. }) => {
Literal::EmptySequence
}
(weedle::literal::DefaultValue::EmptyDictionary(_), Type::Map { .. }) => Literal::EmptyMap,
(weedle::literal::DefaultValue::String(s), Type::Enum { .. }) => {
Literal::Enum(s.0.to_string(), type_.clone())
}
(weedle::literal::DefaultValue::Null(_), Type::Optional { .. }) => Literal::None,
(_, Type::Optional { inner_type, .. }) => Literal::Some {
inner: Box::new(DefaultValueMetadata::Literal(convert_default_value(
default_value,
inner_type,
)?)),
},
// We'll ensure the type safety in the convert_* number methods.
(weedle::literal::DefaultValue::Integer(i), _) => convert_integer(i, type_)?,
(weedle::literal::DefaultValue::Float(i), _) => convert_float(i, type_)?,
_ => bail!("No support for {:?} literal yet", default_value),
})
}
#[cfg(test)] mod test { usesuper::*; use weedle::Parse;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.