wiggle_generate/
codegen_settings.rs1use crate::config::{AsyncConf, ErrorConf, ErrorConfField, TracingConf};
2use anyhow::{Error, anyhow};
3use proc_macro2::{Ident, TokenStream};
4use quote::quote;
5use std::collections::HashMap;
6use std::rc::Rc;
7use witx::{Document, Id, InterfaceFunc, Module, NamedType, TypeRef};
8
9pub use crate::config::Asyncness;
10
11pub struct CodegenSettings {
12 pub errors: ErrorTransform,
13 pub async_: AsyncConf,
14 pub wasmtime: bool,
15 pub tracing: TracingConf,
19 pub mutable: bool,
22}
23impl CodegenSettings {
24 pub fn new(
25 error_conf: &ErrorConf,
26 async_: &AsyncConf,
27 doc: &Document,
28 wasmtime: bool,
29 tracing: &TracingConf,
30 mutable: bool,
31 ) -> Result<Self, Error> {
32 let errors = ErrorTransform::new(error_conf, doc)?;
33 Ok(Self {
34 errors,
35 async_: async_.clone(),
36 wasmtime,
37 tracing: tracing.clone(),
38 mutable,
39 })
40 }
41 pub fn get_async(&self, module: &Module, func: &InterfaceFunc) -> Asyncness {
42 self.async_.get(module.name.as_str(), func.name.as_str())
43 }
44}
45
46pub struct ErrorTransform {
47 m: Vec<ErrorType>,
48}
49
50impl ErrorTransform {
51 pub fn empty() -> Self {
52 Self { m: Vec::new() }
53 }
54 pub fn new(conf: &ErrorConf, doc: &Document) -> Result<Self, Error> {
55 let mut richtype_identifiers = HashMap::new();
56 let m = conf.iter().map(|(ident, field)|
57 match field {
58 ErrorConfField::Trappable(field) => if let Some(abi_type) = doc.typename(&Id::new(ident.to_string())) {
59 Ok(ErrorType::Generated(TrappableErrorType { abi_type, rich_type: field.rich_error.clone() }))
60 } else {
61 Err(anyhow!("No witx typename \"{}\" found", ident.to_string()))
62 },
63 ErrorConfField::User(field) => if let Some(abi_type) = doc.typename(&Id::new(ident.to_string())) {
64 if let Some(ident) = field.rich_error.get_ident() {
65 if let Some(prior_def) = richtype_identifiers.insert(ident.clone(), field.err_loc)
66 {
67 return Err(anyhow!(
68 "duplicate rich type identifier of {ident:?} not allowed. prior definition at {prior_def:?}",
69 ));
70 }
71 Ok(ErrorType::User(UserErrorType {
72 abi_type,
73 rich_type: field.rich_error.clone(),
74 method_fragment: ident.to_string()
75 }))
76 } else {
77 return Err(anyhow!(
78 "rich error type must be identifier for now - TODO add ability to provide a corresponding identifier: {:?}",
79 field.err_loc
80 ))
81 }
82 }
83 else { Err(anyhow!("No witx typename \"{}\" found", ident.to_string())) }
84 }
85 ).collect::<Result<Vec<_>, Error>>()?;
86 Ok(Self { m })
87 }
88
89 pub fn iter(&self) -> impl Iterator<Item = &ErrorType> {
90 self.m.iter()
91 }
92
93 pub fn for_abi_error(&self, tref: &TypeRef) -> Option<&ErrorType> {
94 match tref {
95 TypeRef::Name(nt) => self.for_name(nt),
96 TypeRef::Value { .. } => None,
97 }
98 }
99
100 pub fn for_name(&self, nt: &NamedType) -> Option<&ErrorType> {
101 self.m.iter().find(|e| e.abi_type().name == nt.name)
102 }
103}
104
105pub enum ErrorType {
106 User(UserErrorType),
107 Generated(TrappableErrorType),
108}
109impl ErrorType {
110 pub fn abi_type(&self) -> &NamedType {
111 match self {
112 Self::User(u) => &u.abi_type,
113 Self::Generated(r) => &r.abi_type,
114 }
115 }
116}
117
118pub struct TrappableErrorType {
119 abi_type: Rc<NamedType>,
120 rich_type: Ident,
121}
122
123impl TrappableErrorType {
124 pub fn abi_type(&self) -> TypeRef {
125 TypeRef::Name(self.abi_type.clone())
126 }
127 pub fn typename(&self) -> TokenStream {
128 let richtype = &self.rich_type;
129 quote!(#richtype)
130 }
131}
132
133pub struct UserErrorType {
134 abi_type: Rc<NamedType>,
135 rich_type: syn::Path,
136 method_fragment: String,
137}
138
139impl UserErrorType {
140 pub fn abi_type(&self) -> TypeRef {
141 TypeRef::Name(self.abi_type.clone())
142 }
143 pub fn typename(&self) -> TokenStream {
144 let t = &self.rich_type;
145 quote!(#t)
146 }
147 pub fn method_fragment(&self) -> &str {
148 &self.method_fragment
149 }
150}