|
| 1 | +use anyhow::{Result, bail}; |
| 2 | +use std::collections::HashSet; |
| 3 | +use std::fmt; |
| 4 | +use wit_parser::{Function, FunctionKind, Resolve, WorldKey}; |
| 5 | + |
| 6 | +/// Structure used to parse the command line argument `--chainable-method` consistently |
| 7 | +/// across guest generators. |
| 8 | +#[cfg_attr(feature = "clap", derive(clap::Parser))] |
| 9 | +#[cfg_attr(feature = "serde", derive(serde::Deserialize))] |
| 10 | +#[derive(Clone, Default, Debug)] |
| 11 | +pub struct ChainableMethodFilterSet { |
| 12 | + /// Determines which resource methods should have chaining enabled. |
| 13 | + /// Chaining takes a WIT method import returning nothing, and modifies bindgen |
| 14 | + /// in a language-dependent way to return `self` in the glue code. This does |
| 15 | + /// not affect the ABI in any way. |
| 16 | + /// |
| 17 | + /// This option can be passed multiple times and additionally accepts |
| 18 | + /// comma-separated values for each option passed. Each individual argument |
| 19 | + /// passed here can be one of: |
| 20 | + /// |
| 21 | + /// - `all` - all applicable methods will be chainable |
| 22 | + /// - `-all` - no methods will be chainable |
| 23 | + /// - `foo:bar/baz#my-resource` - enable chaining for all methods in a resource |
| 24 | + /// - `foo:bar/baz#my-resource.some-method` - enable chaining for particular method |
| 25 | + /// |
| 26 | + /// Options are processed in the order they are passed here, so if a method |
| 27 | + /// matches two directives passed the least-specific one should be last. |
| 28 | + #[cfg_attr( |
| 29 | + feature = "clap", |
| 30 | + arg( |
| 31 | + long = "chainable-methods", |
| 32 | + value_parser = parse_chainable_method, |
| 33 | + value_delimiter =',', |
| 34 | + value_name = "FILTER", |
| 35 | + ), |
| 36 | + )] |
| 37 | + chainable_methods: Vec<ChainableMethod>, |
| 38 | + |
| 39 | + #[cfg_attr(feature = "clap", arg(skip))] |
| 40 | + #[cfg_attr(feature = "serde", serde(skip))] |
| 41 | + used_options: HashSet<usize>, |
| 42 | +} |
| 43 | + |
| 44 | +#[cfg(feature = "clap")] |
| 45 | +fn parse_chainable_method(s: &str) -> Result<ChainableMethod, String> { |
| 46 | + Ok(ChainableMethod::parse(s)) |
| 47 | +} |
| 48 | + |
| 49 | +impl ChainableMethodFilterSet { |
| 50 | + /// Returns a set where all functions should be chainable or not depending on |
| 51 | + /// `enable` provided. |
| 52 | + pub fn all(enable: bool) -> ChainableMethodFilterSet { |
| 53 | + ChainableMethodFilterSet { |
| 54 | + chainable_methods: vec![ChainableMethod { |
| 55 | + enabled: enable, |
| 56 | + filter: ChainableMethodFilter::All, |
| 57 | + }], |
| 58 | + used_options: HashSet::new(), |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /// Returns whether the `func` provided should be made chainable |
| 63 | + pub fn should_be_chainable( |
| 64 | + &mut self, |
| 65 | + resolve: &Resolve, |
| 66 | + interface: Option<&WorldKey>, |
| 67 | + func: &Function, |
| 68 | + is_import: bool, |
| 69 | + ) -> bool { |
| 70 | + if !is_import { |
| 71 | + return false; |
| 72 | + } |
| 73 | + |
| 74 | + if func.result.is_some() { |
| 75 | + return false; |
| 76 | + } |
| 77 | + |
| 78 | + match func.kind { |
| 79 | + FunctionKind::AsyncMethod(resource) | FunctionKind::Method(resource) => { |
| 80 | + let interface_name = match interface.map(|key| resolve.name_world_key(key)) { |
| 81 | + Some(str) => str + "#", |
| 82 | + None => "".into(), |
| 83 | + }; |
| 84 | + |
| 85 | + let resource_name_to_test = format!( |
| 86 | + "{}{}", |
| 87 | + interface_name, |
| 88 | + resolve.types[resource].name.as_ref().unwrap() |
| 89 | + ); |
| 90 | + |
| 91 | + let method_name_to_test = format!("{}{}", interface_name, func.name); |
| 92 | + |
| 93 | + for (i, opt) in self.chainable_methods.iter().enumerate() { |
| 94 | + match &opt.filter { |
| 95 | + ChainableMethodFilter::All => { |
| 96 | + self.used_options.insert(i); |
| 97 | + return opt.enabled; |
| 98 | + } |
| 99 | + ChainableMethodFilter::Resource(s) => { |
| 100 | + if *s == resource_name_to_test { |
| 101 | + self.used_options.insert(i); |
| 102 | + return opt.enabled; |
| 103 | + } |
| 104 | + } |
| 105 | + ChainableMethodFilter::Method(s) => { |
| 106 | + if *s == method_name_to_test { |
| 107 | + self.used_options.insert(i); |
| 108 | + return opt.enabled; |
| 109 | + } |
| 110 | + } |
| 111 | + }; |
| 112 | + } |
| 113 | + |
| 114 | + return false; |
| 115 | + } |
| 116 | + _ => { |
| 117 | + return false; |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + /// Intended to be used in the header comment of generated code to help |
| 123 | + /// indicate what options were specified. |
| 124 | + pub fn debug_opts(&self) -> impl Iterator<Item = String> + '_ { |
| 125 | + self.chainable_methods.iter().map(|opt| opt.to_string()) |
| 126 | + } |
| 127 | + |
| 128 | + /// Tests whether all `--chainable-method` options were used throughout bindings |
| 129 | + /// generation, returning an error if any were unused. |
| 130 | + pub fn ensure_all_used(&self) -> Result<()> { |
| 131 | + for (i, opt) in self.chainable_methods.iter().enumerate() { |
| 132 | + if self.used_options.contains(&i) { |
| 133 | + continue; |
| 134 | + } |
| 135 | + if !matches!(opt.filter, ChainableMethodFilter::All) { |
| 136 | + bail!("unused chainable option: {opt}"); |
| 137 | + } |
| 138 | + } |
| 139 | + Ok(()) |
| 140 | + } |
| 141 | + |
| 142 | + /// Pushes a new option into this set. |
| 143 | + pub fn push(&mut self, directive: &str) { |
| 144 | + self.chainable_methods |
| 145 | + .push(ChainableMethod::parse(directive)); |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +#[derive(Debug, Clone)] |
| 150 | +#[cfg_attr(feature = "serde", derive(serde::Deserialize))] |
| 151 | +struct ChainableMethod { |
| 152 | + enabled: bool, |
| 153 | + filter: ChainableMethodFilter, |
| 154 | +} |
| 155 | + |
| 156 | +impl ChainableMethod { |
| 157 | + fn parse(s: &str) -> ChainableMethod { |
| 158 | + let (s, enabled) = match s.strip_prefix('-') { |
| 159 | + Some(s) => (s, false), |
| 160 | + None => (s, true), |
| 161 | + }; |
| 162 | + let filter = match s { |
| 163 | + "all" => ChainableMethodFilter::All, |
| 164 | + other => { |
| 165 | + if other.contains("[method]") { |
| 166 | + ChainableMethodFilter::Method(other.to_string()) |
| 167 | + } else { |
| 168 | + ChainableMethodFilter::Resource(other.to_string()) |
| 169 | + } |
| 170 | + } |
| 171 | + }; |
| 172 | + ChainableMethod { enabled, filter } |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +impl fmt::Display for ChainableMethod { |
| 177 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 178 | + if !self.enabled { |
| 179 | + write!(f, "-")?; |
| 180 | + } |
| 181 | + self.filter.fmt(f) |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +#[derive(Debug, Clone)] |
| 186 | +#[cfg_attr(feature = "serde", derive(serde::Deserialize))] |
| 187 | +enum ChainableMethodFilter { |
| 188 | + All, |
| 189 | + Resource(String), |
| 190 | + Method(String), |
| 191 | +} |
| 192 | + |
| 193 | +impl fmt::Display for ChainableMethodFilter { |
| 194 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 195 | + match self { |
| 196 | + ChainableMethodFilter::All => write!(f, "all"), |
| 197 | + ChainableMethodFilter::Resource(s) => write!(f, "{s}"), |
| 198 | + ChainableMethodFilter::Method(s) => write!(f, "{s}"), |
| 199 | + } |
| 200 | + } |
| 201 | +} |
0 commit comments