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
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
//! Code formatting utilities.
//!
//! So you have a token stream and it's time to format it into a
//! file/string/whatever? You've come to the right place!
//!
//! Formatting is done through the following utilities:
//!
//! * [fmt::VecWriter][VecWriter] - To write result into a vector.
//! * [fmt::FmtWriter][FmtWriter] - To write the result into something
//!   implementing [fmt::Write][std::fmt::Write].
//! * [fmt::IoWriter][IoWriter]- To write the result into something implementing
//!   [io::Write][std::io::Write].
//!
//! # Examples
//!
//! The following is an example, showcasing how you can format directly to
//! [stdout].
//!
//! [stdout]: std::io::stdout
//!
//! # Examples
//!
//! ```rust,no_run
//! use genco::prelude::*;
//! use genco::fmt;
//!
//! # fn main() -> fmt::Result {
//! let map = rust::import("std::collections", "HashMap");
//!
//! let tokens: rust::Tokens = quote! {
//!     let mut m = #map::new();
//!     m.insert(1u32, 2u32);
//! };
//!
//! let stdout = std::io::stdout();
//! let mut w = fmt::IoWriter::new(stdout.lock());
//!
//! let fmt_config = fmt::Config::from_lang::<Rust>()
//!     .with_indentation(fmt::Indentation::Space(2));
//! let mut formatter = w.as_formatter(fmt_config);
//! let config = rust::Config::default();
//!
//! // Default format state for Rust.
//! let format = rust::Format::default();
//!
//! tokens.format(&mut formatter, &config, &format)?;
//! # Ok(())
//! # }
//! ```

use std::fmt;
use std::mem;
use std::num::NonZeroI16;

mod config;
mod fmt_writer;
mod io_writer;
mod vec_writer;

pub use self::config::{Config, Indentation};
pub use self::fmt_writer::FmtWriter;
pub use self::io_writer::IoWriter;
pub use self::vec_writer::VecWriter;

/// Result type for the `fmt` module.
pub type Result<T = ()> = std::result::Result<T, std::fmt::Error>;
/// Error for the `fmt` module.
pub type Error = std::fmt::Error;

/// Buffer used as indentation source.
static SPACES: &str = "                                                                                                    ";

static TABS: &str =
    "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";

/// Trait that defines a line writer.
pub(crate) trait Write: std::fmt::Write {
    fn write_line(&mut self, config: &Config) -> Result;
}

#[derive(Clone, Copy)]
enum Line {
    Initial,
    None,
    Push,
    Line,
}

impl Line {
    /// Convert into an indentation level.
    ///
    /// If we return `None`, no indentation nor lines should be written since we
    /// are at the initial stage of the file.
    fn into_indent(self) -> Option<usize> {
        match self {
            Self::Initial => Some(0),
            Self::Push => Some(1),
            Self::Line => Some(2),
            Self::None => return None,
        }
    }
}

impl Default for Line {
    fn default() -> Self {
        Self::None
    }
}

/// Token stream formatter. Keeps track of everything we need to know in order
/// to enforce genco's indentation and whitespace rules.
pub struct Formatter<'a> {
    write: &'a mut (dyn Write + 'a),
    /// How many lines we want to add to the output stream.
    ///
    /// This will only be realized if we push non-whitespace.
    line: Line,
    /// How many spaces we want to add to the output stream.
    ///
    /// This will only be realized if we push non-whitespace, and will be reset
    /// if a new line is pushed or indentation changes.
    spaces: usize,
    /// Current indentation level.
    indent: i16,
    /// Number of indentations per level.
    config: Config,
}

impl<'a> Formatter<'a> {
    /// Create a new write formatter.
    pub(crate) fn new(write: &'a mut (dyn Write + 'a), config: Config) -> Formatter<'a> {
        Formatter {
            write,
            line: Line::Initial,
            spaces: 0usize,
            indent: 0i16,
            config,
        }
    }

    /// Write the given string.
    pub(crate) fn write_str(&mut self, s: &str) -> fmt::Result {
        if s.len() > 0 {
            self.flush_whitespace()?;
            self.write.write_str(s)?;
        }

        Ok(())
    }

    pub(crate) fn push(&mut self) {
        self.line = match self.line {
            Line::Initial => return,
            Line::Line => return,
            _ => Line::Push,
        };

        self.spaces = 0;
    }

    /// Push a new line.
    pub(crate) fn line(&mut self) {
        self.line = match self.line {
            Line::Initial => return,
            _ => Line::Line,
        };

        self.spaces = 0;
    }

    /// Push a space.
    pub(crate) fn space(&mut self) {
        self.spaces += 1;
    }

    /// Increase indentation level.
    pub(crate) fn indentation(&mut self, n: NonZeroI16) {
        self.push();
        self.indent += n.get();
    }

    // Realize any pending whitespace just prior to writing a non-whitespace
    // item.
    fn flush_whitespace(&mut self) -> fmt::Result {
        let mut spaces = mem::take(&mut self.spaces);

        if let Some(lines) = mem::take(&mut self.line).into_indent() {
            for _ in 0..lines {
                self.write.write_line(&self.config)?;
            }

            let level = i16::max(self.indent, 0) as usize;

            match self.config.indentation {
                Indentation::Space(n) => {
                    spaces += level * n;
                }
                Indentation::Tab => {
                    let mut tabs = level;

                    while tabs > 0 {
                        let len = usize::min(tabs, TABS.len());
                        self.write.write_str(&TABS[0..len])?;
                        tabs -= len;
                    }
                }
            }
        }

        while spaces > 0 {
            let len = usize::min(spaces, SPACES.len());
            self.write.write_str(&SPACES[0..len])?;
            spaces -= len;
        }

        Ok(())
    }
}

impl<'a> fmt::Write for Formatter<'a> {
    fn write_str(&mut self, s: &str) -> fmt::Result {
        if s.len() > 0 {
            Formatter::write_str(self, s)?;
        }

        Ok(())
    }
}