genco/fmt/config.rs
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
use crate::lang::Lang;
/// Indentation configuration.
///
/// ```
/// use genco::prelude::*;
/// use genco::fmt;
///
/// let tokens: rust::Tokens = quote! {
/// fn foo() -> u32 {
/// 42u32
/// }
/// };
///
/// let mut w = fmt::VecWriter::new();
///
/// let fmt = fmt::Config::from_lang::<Rust>()
/// .with_indentation(fmt::Indentation::Tab);
/// let config = rust::Config::default();
///
/// tokens.format_file(&mut w.as_formatter(&fmt), &config)?;
///
/// assert_eq! {
/// vec![
/// "fn foo() -> u32 {",
/// "\t42u32",
/// "}",
/// ],
/// w.into_vec(),
/// };
/// # Ok::<_, genco::fmt::Error>(())
/// ```
#[derive(Debug, Clone, Copy)]
pub enum Indentation {
/// Each indentation is the given number of spaces.
Space(usize),
/// Each indentation is a tab.
Tab,
}
/// Configuration to use for formatting output.
#[derive(Debug, Clone)]
pub struct Config {
/// Indentation level to use.
pub(super) indentation: Indentation,
/// What to use as a newline.
pub(super) newline: &'static str,
}
impl Config {
/// Construct a new default formatter configuration for the specified
/// language.
pub fn from_lang<L>() -> Self
where
L: Lang,
{
Self {
indentation: L::default_indentation(),
newline: "\n",
}
}
/// Modify indentation to use.
pub fn with_indentation(self, indentation: Indentation) -> Self {
Self {
indentation,
..self
}
}
/// Set what to use as newline.
pub fn with_newline(self, newline: &'static str) -> Self {
Self { newline, ..self }
}
}