| 222 | |
| 223 | impl ChangeAssets { |
| 224 | pub fn apply(&self, assets: &mut Vec<AccountAsset>) { |
| 225 | match self { |
| 226 | ChangeAssets::ReplaceWith { assets: new_assets } => { |
| 227 | *assets = new_assets |
| 228 | .iter() |
| 229 | .map(|asset_id| AccountAsset { |
| 230 | asset_id: *asset_id, |
| 231 | balance: None, |
| 232 | }) |
| 233 | .collect(); |
| 234 | } |
| 235 | ChangeAssets::Change { |
| 236 | add_assets, |
| 237 | remove_assets, |
| 238 | } => { |
| 239 | let existing_assets: HashSet<_> = assets.iter().map(|a| a.asset_id).collect(); |
| 240 | for asset_id in add_assets { |
| 241 | if !existing_assets.contains(asset_id) { |
| 242 | assets.push(AccountAsset { |
| 243 | asset_id: *asset_id, |
| 244 | balance: None, |
| 245 | }); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | assets.retain(|a| !remove_assets.contains(&a.asset_id)); |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | #[storable] |