summaryrefslogtreecommitdiff
path: root/cli/doc/display.rs
blob: 9da04363f6f10d87000f0fd8eecc1aaf7c467c7e (plain)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
use crate::colors;
use crate::swc_ecma_ast;
use std::fmt::{Display, Formatter, Result};

pub(crate) struct Indent(pub i64);

impl Display for Indent {
  fn fmt(&self, f: &mut Formatter<'_>) -> Result {
    for _ in 0..self.0 {
      write!(f, "  ")?;
    }
    Ok(())
  }
}

pub(crate) struct SliceDisplayer<'a, T: Display>(&'a [T], &'a str, bool);

impl<'a, T: Display> SliceDisplayer<'a, T> {
  pub fn new(
    slice: &'a [T],
    separator: &'a str,
    trailing: bool,
  ) -> SliceDisplayer<'a, T> {
    SliceDisplayer(slice, separator, trailing)
  }
}

impl<T: Display> Display for SliceDisplayer<'_, T> {
  fn fmt(&self, f: &mut Formatter<'_>) -> Result {
    if self.0.is_empty() {
      return Ok(());
    }

    write!(f, "{}", self.0[0])?;
    for v in &self.0[1..] {
      write!(f, "{}{}", self.1, v)?;
    }

    if self.2 {
      write!(f, "{}", self.1)?;
    }

    Ok(())
  }
}

pub(crate) fn display_abstract(is_abstract: bool) -> impl Display {
  colors::magenta(if is_abstract { "abstract " } else { "" })
}

pub(crate) fn display_accessibility(
  accessibility: Option<swc_ecma_ast::Accessibility>,
) -> impl Display {
  colors::magenta(
    match accessibility.unwrap_or(swc_ecma_ast::Accessibility::Public) {
      swc_ecma_ast::Accessibility::Public => "",
      swc_ecma_ast::Accessibility::Protected => "protected ",
      swc_ecma_ast::Accessibility::Private => "private ",
    },
  )
}

pub(crate) fn display_async(is_async: bool) -> impl Display {
  colors::magenta(if is_async { "async " } else { "" })
}

pub(crate) fn display_generator(is_generator: bool) -> impl Display {
  colors::magenta(if is_generator { "*" } else { "" })
}

pub(crate) fn display_method(method: swc_ecma_ast::MethodKind) -> impl Display {
  colors::magenta(match method {
    swc_ecma_ast::MethodKind::Getter => "get ",
    swc_ecma_ast::MethodKind::Setter => "set ",
    _ => "",
  })
}

pub(crate) fn display_optional(is_optional: bool) -> impl Display {
  colors::magenta(if is_optional { "?" } else { "" })
}

pub(crate) fn display_readonly(is_readonly: bool) -> impl Display {
  colors::magenta(if is_readonly { "readonly " } else { "" })
}

pub(crate) fn display_static(is_static: bool) -> impl Display {
  colors::magenta(if is_static { "static " } else { "" })
}