dsntk_feel/
closure.rs

1//! # Closure
2
3use crate::{Name, QualifiedName};
4use std::collections::btree_set::Iter;
5use std::collections::BTreeSet;
6use std::fmt;
7
8#[derive(Debug, Default, Clone, PartialEq)]
9pub struct Closure(BTreeSet<QualifiedName>);
10
11impl fmt::Display for Closure {
12  /// Converts a closure to string.
13  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
14    write!(f, "[{}]", self.0.iter().map(|v| v.to_string()).collect::<Vec<String>>().join(","))
15  }
16}
17
18impl From<Vec<QualifiedName>> for Closure {
19  /// Creates a [Closure] from a vector of [QualifiedNames](QualifiedName).
20  fn from(value: Vec<QualifiedName>) -> Self {
21    Self(value.iter().cloned().collect())
22  }
23}
24
25impl Closure {
26  /// Returns an iterator over closure items.
27  pub fn iter(&self) -> Iter<QualifiedName> {
28    self.0.iter()
29  }
30
31  /// Removes a closure item with specified name.
32  pub fn remove(&mut self, name: Name) {
33    let qname: QualifiedName = name.into();
34    self.0.remove(&qname);
35  }
36}