summaryrefslogtreecommitdiffstats
path: root/third_party/rust/uniffi_udl/src/literal.rs
blob: 5fbf022644590a25732b4967eefbd24eb7a2b218 (plain)
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use anyhow::{bail, Result};
use uniffi_meta::{LiteralMetadata, Radix, Type};

// We are able to use LiteralMetadata directly.
pub type 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 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),
        })
    }

    fn convert_float(literal: &weedle::literal::FloatLit<'_>, type_: &Type) -> Result<Literal> {
        let string = match literal {
            weedle::literal::FloatLit::Value(v) => v.0,

            _ => bail!("Infinity and NaN is not currently supported"),
        };

        Ok(match type_ {
            Type::Float32 | Type::Float64 => Literal::Float(string.to_string(), type_.clone()),
            _ => bail!("Cannot coerce literal {} into a non-float type", string),
        })
    }

    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::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(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 {
    use super::*;
    use weedle::Parse;

    fn parse_and_convert(expr: &str, t: Type) -> Result<Literal> {
        let (_, node) = weedle::literal::DefaultValue::parse(expr).unwrap();
        convert_default_value(&node, &t)
    }

    #[test]
    fn test_default_value_conversion() -> Result<()> {
        assert!(matches!(
            parse_and_convert("0", Type::UInt8)?,
            Literal::UInt(0, Radix::Decimal, Type::UInt8)
        ));
        assert!(matches!(
            parse_and_convert("-12", Type::Int32)?,
            Literal::Int(-12, Radix::Decimal, Type::Int32)
        ));
        assert!(
            matches!(parse_and_convert("3.14", Type::Float32)?, Literal::Float(v, Type::Float32) if v == "3.14")
        );
        assert!(matches!(
            parse_and_convert("false", Type::Boolean)?,
            Literal::Boolean(false)
        ));
        assert!(
            matches!(parse_and_convert("\"TEST\"", Type::String)?, Literal::String(v) if v == "TEST")
        );
        assert!(
            matches!(parse_and_convert("\"one\"", Type::Enum { name: "E".into(), module_path: "".into() })?, Literal::Enum(v, Type::Enum { name, .. }) if v == "one" && name == "E")
        );
        assert!(matches!(
            parse_and_convert(
                "[]",
                Type::Sequence {
                    inner_type: Box::new(Type::String)
                }
            )?,
            Literal::EmptySequence
        ));
        assert!(matches!(
            parse_and_convert(
                "null",
                Type::Optional {
                    inner_type: Box::new(Type::String)
                }
            )?,
            Literal::None
        ));
        Ok(())
    }
    #[test]
    fn test_error_on_type_mismatch() {
        assert_eq!(
            parse_and_convert("0", Type::Boolean)
                .unwrap_err()
                .to_string(),
            "Cannot coerce literal 0 into a non-integer type"
        );
        assert!(parse_and_convert("{}", Type::Boolean)
            .unwrap_err()
            .to_string()
            .starts_with("No support for"));
    }
}