use crate::{FormatOptions, IndentStyle, IndentWidth, LineWidth};
#[derive(Clone, Debug, Eq, PartialEq, Default)]
pub struct PrinterOptions {
pub indent_width: IndentWidth,
pub indent_style: IndentStyle,
pub line_width: LineWidth,
pub line_ending: LineEnding,
}
impl<'a, O> From<&'a O> for PrinterOptions
where
O: FormatOptions,
{
fn from(options: &'a O) -> Self {
PrinterOptions::default()
.with_indent(options.indent_style())
.with_line_width(options.line_width())
}
}
impl PrinterOptions {
#[must_use]
pub fn with_line_width(mut self, width: LineWidth) -> Self {
self.line_width = width;
self
}
#[must_use]
pub fn with_indent(mut self, style: IndentStyle) -> Self {
self.indent_style = style;
self
}
#[must_use]
pub fn with_tab_width(mut self, width: IndentWidth) -> Self {
self.indent_width = width;
self
}
pub(crate) fn indent_style(&self) -> IndentStyle {
self.indent_style
}
pub(super) const fn indent_width(&self) -> u32 {
self.indent_width.value()
}
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct PrintWidth(u16);
impl PrintWidth {
pub fn new(width: u16) -> Self {
Self(width)
}
}
impl Default for PrintWidth {
fn default() -> Self {
LineWidth::default().into()
}
}
impl From<LineWidth> for PrintWidth {
fn from(width: LineWidth) -> Self {
Self(u16::from(width))
}
}
impl From<PrintWidth> for u32 {
fn from(width: PrintWidth) -> Self {
u32::from(width.0)
}
}
impl From<PrintWidth> for u16 {
fn from(width: PrintWidth) -> Self {
width.0
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum SourceMapGeneration {
#[default]
Disabled,
Enabled,
}
impl SourceMapGeneration {
pub const fn is_enabled(self) -> bool {
matches!(self, SourceMapGeneration::Enabled)
}
pub const fn is_disabled(self) -> bool {
matches!(self, SourceMapGeneration::Disabled)
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum LineEnding {
#[default]
LineFeed,
CarriageReturnLineFeed,
CarriageReturn,
}
impl LineEnding {
#[inline]
pub const fn as_str(&self) -> &'static str {
match self {
LineEnding::LineFeed => "\n",
LineEnding::CarriageReturnLineFeed => "\r\n",
LineEnding::CarriageReturn => "\r",
}
}
#[inline]
pub const fn as_setting_str(&self) -> &'static str {
match self {
LineEnding::LineFeed => "lf",
LineEnding::CarriageReturnLineFeed => "crlf",
LineEnding::CarriageReturn => "cr",
}
}
}