Debian Codegraph @

third_party/rust/uniffi_udl/src/literal.rs

raw
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 use anyhow::{bail, Result};
6 use uniffi_meta::{LiteralMetadata, Radix, Type};
7
8 // We are able to use LiteralMetadata directly.
9 pub type Literal = LiteralMetadata;
10
11 // Convert weedle/udl syntax.
12 pub(super) fn convert_default_value(
13 default_value: &weedle::literal::DefaultValue<'_>,
14 type_: &Type,
15 ) -> Result<LiteralMetadata> {
16 fn convert_integer(literal: &weedle::literal::IntegerLit<'_>, type_: &Type) -> Result<Literal> {
17 let (string, radix) = match literal {
18 weedle::literal::IntegerLit::Dec(v) => (v.0, Radix::Decimal),
19 weedle::literal::IntegerLit::Hex(v) => (v.0, Radix::Hexadecimal),
20 weedle::literal::IntegerLit::Oct(v) => (v.0, Radix::Octal),
21 };
22 // This is the radix of the parsed number, passed to `from_str_radix`.
23 let src_radix = radix as u32;
24 // This radix tells the backends how to represent the number in the output languages.
25 let dest_radix = if string == "0" || string.starts_with('-') {
26 // 1. weedle parses "0" as an octal literal, but we most likely want to treat this as a decimal.
27 // 2. Explicitly negatively signed hex numbers won't convert via i64 very well if they're not 64 bit.
28 // For ease of implementation, output will use decimal.
29 Radix::Decimal
30 } else {
31 radix
32 };
33
34 // Clippy seems to think we should be using `strip_prefix` here, but
35 // it seems confused as to what this is actually doing.
36 #[allow(clippy::manual_strip)]
37 let string = if string.starts_with('-') {
38 ("-".to_string() + string[1..].trim_start_matches("0x")).to_lowercase()
39 } else {
40 string.trim_start_matches("0x").to_lowercase()
41 };
42
43 Ok(match type_ {
44 Type::Int8 | Type::Int16 | Type::Int32 | Type::Int64 => Literal::Int(
45 i64::from_str_radix(&string, src_radix)?,
46 dest_radix,
47 type_.clone(),
48 ),
49 Type::UInt8 | Type::UInt16 | Type::UInt32 | Type::UInt64 => Literal::UInt(
50 u64::from_str_radix(&string, src_radix)?,
51 dest_radix,
52 type_.clone(),
53 ),
54
55 _ => bail!("Cannot coerce literal {} into a non-integer type", string),
56 })
57 }
58
59 fn convert_float(literal: &weedle::literal::FloatLit<'_>, type_: &Type) -> Result<Literal> {
60 let string = match literal {
61 weedle::literal::FloatLit::Value(v) => v.0,
62
63 _ => bail!("Infinity and NaN is not currently supported"),
64 };
65
66 Ok(match type_ {
67 Type::Float32 | Type::Float64 => Literal::Float(string.to_string(), type_.clone()),
68 _ => bail!("Cannot coerce literal {} into a non-float type", string),
69 })
70 }
71
72 Ok(match (default_value, type_) {
73 (weedle::literal::DefaultValue::Boolean(b), Type::Boolean) => Literal::Boolean(b.0),
74 (weedle::literal::DefaultValue::String(s), Type::String) => {
75 // Note that weedle doesn't parse escaped double quotes.
76 // Keeping backends using double quotes (possible for all to date)
77 // means we don't need to escape single quotes. But we haven't spent a lot of time
78 // trying to break default values with weird escapes and quotes.
79 Literal::String(s.0.to_string())
80 }
81 (weedle::literal::DefaultValue::EmptyArray(_), Type::Sequence { .. }) => {
82 Literal::EmptySequence
83 }
84 (weedle::literal::DefaultValue::String(s), Type::Enum { .. }) => {
85 Literal::Enum(s.0.to_string(), type_.clone())
86 }
87 (weedle::literal::DefaultValue::Null(_), Type::Optional { .. }) => Literal::None,
88 (_, Type::Optional { inner_type, .. }) => Literal::Some {
89 inner: Box::new(convert_default_value(default_value, inner_type)?),
90 },
91
92 // We'll ensure the type safety in the convert_* number methods.
93 (weedle::literal::DefaultValue::Integer(i), _) => convert_integer(i, type_)?,
94 (weedle::literal::DefaultValue::Float(i), _) => convert_float(i, type_)?,
95
96 _ => bail!("No support for {:?} literal yet", default_value),
97 })
98 }
99
100 #[cfg(test)]
101 mod test {
102 use super::*;
103 use weedle::Parse;
104
105 fn parse_and_convert(expr: &str, t: Type) -> Result<Literal> {
106 let (_, node) = weedle::literal::DefaultValue::parse(expr).unwrap();
107 convert_default_value(&node, &t)
108 }
109
110 #[test]
111 fn test_default_value_conversion() -> Result<()> {
112 assert!(matches!(
113 parse_and_convert("0", Type::UInt8)?,
114 Literal::UInt(0, Radix::Decimal, Type::UInt8)
115 ));
116 assert!(matches!(
117 parse_and_convert("-12", Type::Int32)?,
118 Literal::Int(-12, Radix::Decimal, Type::Int32)
119 ));
120 assert!(
121 matches!(parse_and_convert("3.14", Type::Float32)?, Literal::Float(v, Type::Float32) if v == "3.14")
122 );
123 assert!(matches!(
124 parse_and_convert("false", Type::Boolean)?,
125 Literal::Boolean(false)
126 ));
127 assert!(
128 matches!(parse_and_convert("\"TEST\"", Type::String)?, Literal::String(v) if v == "TEST")
129 );
130 assert!(
131 matches!(parse_and_convert("\"one\"", Type::Enum { name: "E".into(), module_path: "".into() })?, Literal::Enum(v, Type::Enum { name, .. }) if v == "one" && name == "E")
132 );
133 assert!(matches!(
134 parse_and_convert(
135 "[]",
136 Type::Sequence {
137 inner_type: Box::new(Type::String)
138 }
139 )?,
140 Literal::EmptySequence
141 ));
142 assert!(matches!(
143 parse_and_convert(
144 "null",
145 Type::Optional {
146 inner_type: Box::new(Type::String)
147 }
148 )?,
149 Literal::None
150 ));
151 Ok(())
152 }
153 #[test]
154 fn test_error_on_type_mismatch() {
155 assert_eq!(
156 parse_and_convert("0", Type::Boolean)
157 .unwrap_err()
158 .to_string(),
159 "Cannot coerce literal 0 into a non-integer type"
160 );
161 assert!(parse_and_convert("{}", Type::Boolean)
162 .unwrap_err()
163 .to_string()
164 .starts_with("No support for"));
165 }
166 }