wrpc_introspect/
lib.rs

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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
use std::collections::{BTreeSet, VecDeque};

use wit_parser::{
    Case, Field, Flags, Function, FunctionKind, Handle, Int, Record, Resolve, Stream, Type,
    TypeDefKind, TypeId,
};

#[must_use]
pub fn flag_repr(ty: &Flags) -> Int {
    match ty.flags.len() {
        ..=8 => Int::U8,
        9..=16 => Int::U16,
        17..=32 => Int::U32,
        33.. => Int::U64,
    }
}

#[must_use]
pub fn rpc_func_name(func: &Function) -> &str {
    match &func.kind {
        FunctionKind::Constructor(..) => func
            .name
            .strip_prefix("[constructor]")
            .expect("failed to strip `[constructor]` prefix"),
        FunctionKind::Static(..) => func
            .name
            .strip_prefix("[static]")
            .expect("failed to strip `[static]` prefix"),
        FunctionKind::Method(..) => func
            .name
            .strip_prefix("[method]")
            .expect("failed to strip `[method]` prefix"),
        FunctionKind::Freestanding => &func.name,
    }
}

#[must_use]
pub fn is_ty(resolve: &Resolve, expected: Type, ty: &Type) -> bool {
    let mut ty = *ty;
    loop {
        if ty == expected {
            return true;
        }
        if let Type::Id(id) = ty {
            if let TypeDefKind::Type(t) = resolve.types[id].kind {
                ty = t;
                continue;
            }
        }
        return false;
    }
}

#[must_use]
pub fn is_list_of(resolve: &Resolve, expected: Type, ty: &Type) -> bool {
    let mut ty = *ty;
    loop {
        if let Type::Id(id) = ty {
            match resolve.types[id].kind {
                TypeDefKind::Type(t) => {
                    ty = t;
                    continue;
                }
                TypeDefKind::List(t) => return is_ty(resolve, expected, &t),
                _ => return false,
            }
        }
        return false;
    }
}

#[must_use]
pub fn is_tuple(resolve: &Resolve, ty: &Type) -> bool {
    let mut ty = *ty;
    loop {
        if let Type::Id(id) = ty {
            match resolve.types[id].kind {
                TypeDefKind::Type(t) => {
                    ty = t;
                    continue;
                }
                TypeDefKind::Tuple(_) => return true,
                _ => return false,
            }
        }
        return false;
    }
}

#[must_use]
pub fn async_paths_ty(resolve: &Resolve, ty: &Type) -> (BTreeSet<VecDeque<Option<u32>>>, bool) {
    if let Type::Id(ty) = ty {
        async_paths_tyid(resolve, *ty)
    } else {
        (BTreeSet::default(), false)
    }
}

#[must_use]
pub fn async_paths_tyid(resolve: &Resolve, id: TypeId) -> (BTreeSet<VecDeque<Option<u32>>>, bool) {
    match &resolve.types[id].kind {
        TypeDefKind::List(ty) => {
            let mut paths = BTreeSet::default();
            let (nested, fut) = async_paths_ty(resolve, ty);
            for mut path in nested {
                path.push_front(None);
                paths.insert(path);
            }
            if fut {
                paths.insert(vec![None].into());
            }
            (paths, false)
        }
        TypeDefKind::Option(ty) => async_paths_ty(resolve, ty),
        TypeDefKind::Result(ty) => {
            let mut paths = BTreeSet::default();
            let mut is_fut = false;
            if let Some(ty) = ty.ok.as_ref() {
                let (nested, fut) = async_paths_ty(resolve, ty);
                for path in nested {
                    paths.insert(path);
                }
                if fut {
                    is_fut = true;
                }
            }
            if let Some(ty) = ty.err.as_ref() {
                let (nested, fut) = async_paths_ty(resolve, ty);
                for path in nested {
                    paths.insert(path);
                }
                if fut {
                    is_fut = true;
                }
            }
            (paths, is_fut)
        }
        TypeDefKind::Variant(ty) => {
            let mut paths = BTreeSet::default();
            let mut is_fut = false;
            for Case { ty, .. } in &ty.cases {
                if let Some(ty) = ty {
                    let (nested, fut) = async_paths_ty(resolve, ty);
                    for path in nested {
                        paths.insert(path);
                    }
                    if fut {
                        is_fut = true;
                    }
                }
            }
            (paths, is_fut)
        }
        TypeDefKind::Tuple(ty) => {
            let mut paths = BTreeSet::default();
            for (i, ty) in ty.types.iter().enumerate() {
                let (nested, fut) = async_paths_ty(resolve, ty);
                for mut path in nested {
                    path.push_front(Some(i.try_into().unwrap()));
                    paths.insert(path);
                }
                if fut {
                    let path = vec![Some(i.try_into().unwrap())].into();
                    paths.insert(path);
                }
            }
            (paths, false)
        }
        TypeDefKind::Record(Record { fields }) => {
            let mut paths = BTreeSet::default();
            for (i, Field { ty, .. }) in fields.iter().enumerate() {
                let (nested, fut) = async_paths_ty(resolve, ty);
                for mut path in nested {
                    path.push_front(Some(i.try_into().unwrap()));
                    paths.insert(path);
                }
                if fut {
                    let path = vec![Some(i.try_into().unwrap())].into();
                    paths.insert(path);
                }
            }
            (paths, false)
        }
        TypeDefKind::Future(ty) => {
            let mut paths = BTreeSet::default();
            if let Some(ty) = ty {
                (paths, _) = async_paths_ty(resolve, ty);
            }
            (paths, true)
        }
        TypeDefKind::Stream(Stream { element, .. }) => {
            let mut paths = BTreeSet::new();
            if let Some(ty) = element {
                let (nested, fut) = async_paths_ty(resolve, ty);
                for mut path in nested {
                    path.push_front(None);
                    paths.insert(path);
                }
                if fut {
                    paths.insert(vec![None].into());
                }
            }
            (paths.into_iter().collect(), true)
        }
        TypeDefKind::Type(ty) => async_paths_ty(resolve, ty),
        TypeDefKind::Resource
        | TypeDefKind::Flags(..)
        | TypeDefKind::Enum(..)
        | TypeDefKind::Handle(Handle::Own(..) | Handle::Borrow(..)) => (BTreeSet::default(), false),
        TypeDefKind::Unknown => unreachable!(),
    }
}