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
//! # Closure

use crate::{Name, QualifiedName};
use std::collections::btree_set::Iter;
use std::collections::BTreeSet;
use std::fmt;

#[derive(Debug, Default, Clone, PartialEq)]
pub struct Closure(BTreeSet<QualifiedName>);

impl fmt::Display for Closure {
  /// Converts a closure to string.
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    write!(f, "[{}]", self.0.iter().map(|v| v.to_string()).collect::<Vec<String>>().join(","))
  }
}

impl From<Vec<QualifiedName>> for Closure {
  /// Creates a [Closure] from a vector of [QualifiedNames](QualifiedName).
  fn from(value: Vec<QualifiedName>) -> Self {
    Self(value.iter().cloned().collect())
  }
}

impl Closure {
  /// Returns an iterator over closure items.
  pub fn iter(&self) -> Iter<QualifiedName> {
    self.0.iter()
  }

  /// Removes a closure item with specified name.
  pub fn remove(&mut self, name: Name) {
    let qname: QualifiedName = name.into();
    self.0.remove(&qname);
  }
}