use std::{borrow::Cow, io, path::PathBuf};
use crate::DeprecatedType;
use super::comments;
#[derive(Debug)]
#[non_exhaustive]
pub struct CommentFormatterArgs<'a> {
pub docs: &'a Cow<'static, str>,
pub deprecated: Option<&'a DeprecatedType>,
}
pub type CommentFormatterFn = fn(CommentFormatterArgs) -> String;
pub type FormatterFn = fn(PathBuf) -> io::Result<()>;
#[derive(Debug, Clone)]
pub struct ExportConfig {
pub(crate) bigint: BigIntExportBehavior,
pub(crate) comment_exporter: Option<CommentFormatterFn>,
pub(crate) formatter: Option<FormatterFn>,
}
impl ExportConfig {
pub fn new() -> Self {
Default::default()
}
pub fn bigint(mut self, bigint: BigIntExportBehavior) -> Self {
self.bigint = bigint;
self
}
pub fn comment_style(mut self, exporter: Option<CommentFormatterFn>) -> Self {
self.comment_exporter = exporter;
self
}
pub fn formatter(mut self, formatter: FormatterFn) -> Self {
self.formatter = Some(formatter);
self
}
pub fn run_format(&self, path: PathBuf) -> io::Result<()> {
if let Some(formatter) = self.formatter {
formatter(path)?;
}
Ok(())
}
}
impl Default for ExportConfig {
fn default() -> Self {
Self {
bigint: Default::default(),
comment_exporter: Some(comments::js_doc),
formatter: None,
}
}
}
#[derive(Debug, Clone, Default)]
pub enum BigIntExportBehavior {
String,
Number,
BigInt,
#[default]
Fail,
#[doc(hidden)]
FailWithReason(&'static str),
}