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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright © 2021 Alexandra Frydl
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

use crate::prelude::*;
use crate::string::SharedString;

/// A qualified test path, including scope names.
#[derive(Clone, Default)]
pub struct Path {
  pub(super) components: im::Vector<SharedString>,
}

/// An iterator over the components of a path.
pub type Components<'a> = im::vector::Iter<'a, SharedString>;

/// An iterator over the component string slices of a path.
pub type ComponentStrs<'a> = iter::Map<Components<'a>, fn(&'a SharedString) -> &'a str>;

impl Path {
  /// Returns a reference to the components in the path.
  pub fn components(&self) -> Components {
    self.components.iter()
  }

  /// Returns an iterator over the component string slices.
  pub fn component_strs(&self) -> ComponentStrs {
    self.components.iter().map(SharedString::as_str)
  }
}

impl Debug for Path {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    write!(f, "{:?}", self.components)
  }
}

impl Display for Path {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    if self.components.is_empty() {
      return write!(f, "(root)");
    }

    let mut components = self.components.iter();

    if let Some(first) = components.next() {
      write!(f, "{}", first)?;
    }

    for component in components {
      let first_char = match component.chars().next() {
        Some(c) => c,
        None => continue,
      };

      if first_char.is_whitespace() || [':', '(', '<'].contains(&first_char) {
        write!(f, "{}", component)?;
      } else {
        write!(f, " {}", component)?;
      }
    }

    Ok(())
  }
}