| 227 | } |
| 228 | |
| 229 | pub fn parse_resource_coupling( |
| 230 | input: &str, |
| 231 | resources: &[ResourceDescriptorItem], |
| 232 | ) -> anyhow::Result<ResourceDescriptorCoupling> { |
| 233 | let mut weights = input |
| 234 | .split(",") |
| 235 | .map(|s| { |
| 236 | let s = s.trim(); |
| 237 | let (rs, weight) = (if let Some((rs, ws)) = s.split_once('=') { |
| 238 | let ws = ws.trim(); |
| 239 | let Ok(w) = ws.parse() else { |
| 240 | return Err(anyhow::anyhow!("Invalid weight: {ws}")); |
| 241 | }; |
| 242 | anyhow::Ok((rs, w)) |
| 243 | } else { |
| 244 | anyhow::Ok((s, 256)) |
| 245 | })?; |
| 246 | let Some((left, right)) = rs.split_once(':') else { |
| 247 | return Err(anyhow::anyhow!("Missing ':'")); |
| 248 | }; |
| 249 | let (resource1_idx, group1_idx) = parse_resource_coupling_group(left, resources)?; |
| 250 | let (resource2_idx, group2_idx) = parse_resource_coupling_group(right, resources)?; |
| 251 | let mut item = ResourceDescriptorCouplingItem { |
| 252 | resource1_idx, |
| 253 | group1_idx, |
| 254 | resource2_idx, |
| 255 | group2_idx, |
| 256 | weight, |
| 257 | }; |
| 258 | item.normalize(); |
| 259 | Ok(item) |
| 260 | }) |
| 261 | .collect::<Result<Vec<_>, _>>()?; |
| 262 | weights.sort_unstable(); |
| 263 | Ok(ResourceDescriptorCoupling { weights }) |
| 264 | } |
| 265 | |
| 266 | #[cfg(test)] |
| 267 | mod test { |