Skip to content

Commit

Permalink
refactor(encode): Extract all str repr inferring
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Jul 17, 2024
1 parent 00fb5ee commit 330b590
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions crates/toml_edit/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,12 +332,7 @@ pub(crate) fn to_string_repr(
style: Option<StringStyle>,
literal: Option<bool>,
) -> Repr {
let (style, literal) = match (style, literal) {
(Some(style), Some(literal)) => (style, literal),
(_, Some(literal)) => (infer_style(value).0, literal),
(Some(style), _) => (style, infer_style(value).1),
(_, _) => infer_style(value),
};
let (style, literal) = infer_style(value, style, literal);

let mut output = String::with_capacity(value.len() * 2);
if literal {
Expand Down Expand Up @@ -415,7 +410,20 @@ impl StringStyle {
}
}

fn infer_style(value: &str) -> (StringStyle, bool) {
fn infer_style(
value: &str,
style: Option<StringStyle>,
literal: Option<bool>,
) -> (StringStyle, bool) {
match (style, literal) {
(Some(style), Some(literal)) => (style, literal),
(_, Some(literal)) => (infer_all_style(value).0, literal),
(Some(style), _) => (style, infer_all_style(value).1),
(_, _) => infer_all_style(value),
}
}

fn infer_all_style(value: &str) -> (StringStyle, bool) {
// We need to determine:
// - if we are a "multi-line" pretty (if there are \n)
// - if ['''] appears if multi or ['] if single
Expand Down

0 comments on commit 330b590

Please sign in to comment.