mod auto_comments;
mod comments_style;
mod doc_comments;
mod enums_style;
mod indent_style;
mod layout;
mod spacing;
mod values_style;
pub use self::{
auto_comments::*, comments_style::*, doc_comments::*, enums_style::*,
indent_style::*, layout::*, spacing::*, values_style::*,
};
use crate::*;
use std::any;
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
#[serde(default)]
pub struct Formatting {
pub auto_comments: AutoComments,
pub comments_style: CommentsStyle,
pub doc_comments: DocComments,
pub enums_style: EnumsStyle,
pub indent_style: IndentStyle,
pub layout: Layout,
pub spacing: Spacing,
pub values_style: ValuesStyle,
}
impl Formatting {
pub(crate) fn customize(&self, metas: impl Iterator<Item = Meta>) -> Self {
let mut this = serde_json::to_value(self).unwrap();
for meta in metas {
if meta.key() == "fmt" {
this =
serde_json::from_str(meta.value()).unwrap_or_else(|err| {
panic!(
"Not a valid {}: {}",
any::type_name::<Self>(),
err
)
});
} else if let Some(key) = meta.key().strip_prefix("fmt") {
let key = key.replace('.', "/");
let this = this.pointer_mut(&key).unwrap_or_else(|| {
panic!(
"Tried to overwrite a non-existing formatting option: {}",
key
);
});
*this = serde_json::from_str(meta.value())
.unwrap_or_else(|err| panic!("Not a valid JSON: {}", err));
}
}
serde_json::from_value(this).unwrap()
}
}