use crate::misc::Sealed;
pub trait Config: Sealed {
#[doc(hidden)]
fn comma(&self) -> bool;
#[doc(hidden)]
fn trailing_comma(&self) -> bool;
#[doc(hidden)]
#[cfg(feature = "comment")]
fn comments(&self) -> bool;
}
pub struct RTConfig {
comma: bool,
trailing_comma: bool,
#[cfg(feature = "comment")]
comments: bool,
}
impl RTConfig {
pub fn new() -> Self {
Self {
comma: false,
trailing_comma: false,
#[cfg(feature = "comment")]
comments: false,
}
}
pub fn require_comma(mut self, v: bool) -> Self {
self.trailing_comma = !v;
self.comma = !v;
self
}
pub fn allow_trailing_comma(mut self, v: bool) -> Self {
self.trailing_comma = v | self.comma;
self
}
#[cfg(feature = "comment")]
pub fn allow_comments(mut self, v: bool) -> Self {
self.comments = v;
self
}
}
impl Config for RTConfig {
#[inline(always)]
fn comma(&self) -> bool {
self.comma
}
#[inline(always)]
fn trailing_comma(&self) -> bool {
self.trailing_comma
}
#[inline(always)]
#[cfg(feature = "comment")]
fn comments(&self) -> bool {
self.comments
}
}
impl Sealed for RTConfig {}
pub struct CTConfig<
const COMMA: bool = true,
const TRAILING_COMMA: bool = false,
#[cfg(feature = "comment")] const COMMENTS: bool = false,
>;
impl CTConfig {
pub fn new() -> Self {
Self
}
}
impl<const A: bool, const B: bool> CTConfig<A, B> {
#[inline]
#[cfg(feature = "comment")]
pub fn allow_comments(self) -> CTConfig<A, B, true> {
CTConfig
}
}
#[cfg(feature = "comment")]
mod __ {
use super::{CTConfig, Config, Sealed};
impl<const A: bool, const B: bool> CTConfig<true, A, B> {
#[inline]
pub fn optional_comma(self) -> CTConfig<false, true, B> {
CTConfig
}
}
impl<const A: bool, const B: bool> CTConfig<A, false, B> {
#[inline]
pub fn allow_trailing_comma(self) -> CTConfig<A, true, B> {
CTConfig
}
}
impl<const COMMA: bool, const TRAILING_COMMA: bool, const COMMENTS: bool> Config
for CTConfig<COMMA, TRAILING_COMMA, COMMENTS>
{
#[inline(always)]
fn comma(&self) -> bool {
!COMMA
}
#[inline(always)]
fn trailing_comma(&self) -> bool {
TRAILING_COMMA | !COMMA
}
#[inline(always)]
fn comments(&self) -> bool {
COMMENTS
}
}
impl<const A: bool, const B: bool, const C: bool> Sealed for CTConfig<A, B, C> {}
}
#[cfg(not(feature = "comment"))]
mod __ {
use super::{CTConfig, Config, Sealed};
impl<const A: bool> CTConfig<true, A> {
#[inline]
pub fn optional_comma(self) -> CTConfig<false, true> {
CTConfig
}
}
impl<const V: bool> CTConfig<V> {
#[inline]
pub fn allow_trailing_comma(self) -> CTConfig<V, true> {
CTConfig
}
}
impl<const COMMA: bool, const TRAILING_COMMA: bool> Config for CTConfig<COMMA, TRAILING_COMMA> {
#[inline(always)]
fn comma(&self) -> bool {
!COMMA
}
#[inline(always)]
fn trailing_comma(&self) -> bool {
TRAILING_COMMA | !COMMA
}
}
impl<const A: bool, const B: bool> Sealed for CTConfig<A, B> {}
}