pub mod c;
pub mod csharp;
pub mod dart;
pub mod go;
pub mod java;
pub mod js;
pub mod kotlin;
pub mod nix;
pub mod python;
pub mod rust;
pub mod swift;
pub use self::c::C;
pub use self::csharp::Csharp;
pub use self::dart::Dart;
pub use self::go::Go;
pub use self::java::Java;
pub use self::js::JavaScript;
pub use self::kotlin::Kotlin;
pub use self::nix::Nix;
pub use self::python::Python;
pub use self::rust::Rust;
pub use self::swift::Swift;
use core::fmt::Write as _;
use crate::fmt;
use crate::Tokens;
pub trait Lang: Sized {
type Config;
type Format: Default;
type Item: LangItem<Self>;
fn default_indentation() -> fmt::Indentation {
fmt::Indentation::Space(4)
}
fn open_quote(
out: &mut fmt::Formatter<'_>,
_config: &Self::Config,
_format: &Self::Format,
_has_eval: bool,
) -> fmt::Result {
out.write_char('"')?;
Ok(())
}
fn close_quote(
out: &mut fmt::Formatter<'_>,
_config: &Self::Config,
_format: &Self::Format,
_has_eval: bool,
) -> fmt::Result {
out.write_char('"')?;
Ok(())
}
fn string_eval_literal(
out: &mut fmt::Formatter<'_>,
config: &Self::Config,
format: &Self::Format,
literal: &str,
) -> fmt::Result {
Self::start_string_eval(out, config, format)?;
out.write_str(literal)?;
Self::end_string_eval(out, config, format)?;
Ok(())
}
fn start_string_eval(
_out: &mut fmt::Formatter<'_>,
_config: &Self::Config,
_format: &Self::Format,
) -> fmt::Result {
Ok(())
}
fn end_string_eval(
_out: &mut fmt::Formatter<'_>,
_config: &Self::Config,
_format: &Self::Format,
) -> fmt::Result {
Ok(())
}
fn write_quoted(out: &mut fmt::Formatter<'_>, input: &str) -> fmt::Result {
out.write_str(input)
}
fn format_file(
tokens: &Tokens<Self>,
out: &mut fmt::Formatter<'_>,
config: &Self::Config,
) -> fmt::Result {
let format = Self::Format::default();
tokens.format(out, config, &format)
}
}
pub trait LangSupportsEval: Lang {}
impl Lang for () {
type Config = ();
type Format = ();
type Item = ();
}
impl<L> LangItem<L> for ()
where
L: Lang,
{
fn format(&self, _: &mut fmt::Formatter<'_>, _: &L::Config, _: &L::Format) -> fmt::Result {
Ok(())
}
}
pub trait LangItem<L>
where
L: Lang,
{
fn format(
&self,
fmt: &mut fmt::Formatter<'_>,
config: &L::Config,
format: &L::Format,
) -> fmt::Result;
}
pub fn c_family_write_quoted(out: &mut fmt::Formatter, input: &str) -> fmt::Result {
for c in input.chars() {
match c {
'\u{0007}' => out.write_str("\\a")?,
'\u{0008}' => out.write_str("\\b")?,
'\u{0012}' => out.write_str("\\f")?,
'\n' => out.write_str("\\n")?,
'\r' => out.write_str("\\r")?,
'\t' => out.write_str("\\t")?,
'\u{0011}' => out.write_str("\\v")?,
'\'' => out.write_str("\\'")?,
'"' => out.write_str("\\\"")?,
'\\' => out.write_str("\\\\")?,
' ' => out.write_char(' ')?,
c if c.is_ascii() => {
if !c.is_control() {
out.write_char(c)?
} else {
write!(out, "\\x{:02x}", c as u32)?;
}
}
c if (c as u32) < 0x10000 => {
write!(out, "\\u{:04x}", c as u32)?;
}
c => {
write!(out, "\\U{:08x}", c as u32)?;
}
};
}
Ok(())
}