Skip to content

Commit

Permalink
fix(edit): Don't include decor in Key's Display
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Feb 5, 2024
1 parent 16c8353 commit 4e89856
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 35 deletions.
61 changes: 27 additions & 34 deletions crates/toml_edit/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,7 @@ use crate::value::{
};
use crate::{Array, InlineTable, Item, Table, Value};

pub(crate) fn encode_key(
this: &Key,
buf: &mut dyn Write,
input: Option<&str>,
default_decor: (&str, &str),
) -> Result {
let decor = this.decor();
decor.prefix_encode(buf, input, default_decor.0)?;

pub(crate) fn encode_key(this: &Key, buf: &mut dyn Write, input: Option<&str>) -> Result {
if let Some(input) = input {
let repr = this
.as_repr()
Expand All @@ -33,7 +25,6 @@ pub(crate) fn encode_key(
write!(buf, "{}", repr)?;
};

decor.suffix_encode(buf, input, default_decor.1)?;
Ok(())
}

Expand All @@ -44,24 +35,25 @@ fn encode_key_path(
default_decor: (&str, &str),
) -> Result {
for (i, key) in this.iter().enumerate() {
let decor = key.decor();

let first = i == 0;
let last = i + 1 == this.len();

let prefix = if first {
default_decor.0
} else {
DEFAULT_KEY_PATH_DECOR.0
};
let suffix = if last {
default_decor.1
if first {
decor.prefix_encode(buf, input, default_decor.0)?;
} else {
DEFAULT_KEY_PATH_DECOR.1
};

if !first {
write!(buf, ".")?;
decor.prefix_encode(buf, input, DEFAULT_KEY_PATH_DECOR.0)?;
}

encode_key(key, buf, input)?;

if last {
decor.suffix_encode(buf, input, default_decor.1)?;
} else {
decor.suffix_encode(buf, input, DEFAULT_KEY_PATH_DECOR.1)?;
}
encode_key(key, buf, input, (prefix, suffix))?;
}
Ok(())
}
Expand All @@ -73,24 +65,25 @@ pub(crate) fn encode_key_path_ref(
default_decor: (&str, &str),
) -> Result {
for (i, key) in this.iter().enumerate() {
let decor = key.decor();

let first = i == 0;
let last = i + 1 == this.len();

let prefix = if first {
default_decor.0
} else {
DEFAULT_KEY_PATH_DECOR.0
};
let suffix = if last {
default_decor.1
if first {
decor.prefix_encode(buf, input, default_decor.0)?;
} else {
DEFAULT_KEY_PATH_DECOR.1
};

if !first {
write!(buf, ".")?;
decor.prefix_encode(buf, input, DEFAULT_KEY_PATH_DECOR.0)?;
}

encode_key(key, buf, input)?;

if last {
decor.suffix_encode(buf, input, default_decor.1)?;
} else {
decor.suffix_encode(buf, input, DEFAULT_KEY_PATH_DECOR.1)?;
}
encode_key(key, buf, input, (prefix, suffix))?;
}
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion crates/toml_edit/src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ impl PartialEq<String> for Key {
#[cfg(feature = "display")]
impl std::fmt::Display for Key {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
crate::encode::encode_key(self, f, None, ("", ""))
crate::encode::encode_key(self, f, None)
}
}

Expand Down

0 comments on commit 4e89856

Please sign in to comment.