Skip to content

Commit

Permalink
Implement Borrow for Key
Browse files Browse the repository at this point in the history
  • Loading branch information
Jake-Shadle committed Jun 26, 2024
1 parent 05c05d7 commit 1212005
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
2 changes: 1 addition & 1 deletion integ-tests/tests/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl<'de> Deserialize<'de> for Package {
ValueInner::Table(tab) => {
let mut th = TableHelper::from((tab, value.span));

if let Some(mut val) = th.table.remove(&"crate".into()) {
if let Some(mut val) = th.table.remove("crate") {
let (name, version) = match val.take() {
ValueInner::String(s) => from_str(s),
found => {
Expand Down
10 changes: 5 additions & 5 deletions toml-span/src/de_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ impl<'de> TableHelper<'de> {

/// Returns true if the table contains the specified key
#[inline]
pub fn contains(&self, name: &'de str) -> bool {
self.table.contains_key(&name.into())
pub fn contains(&self, name: &str) -> bool {
self.table.contains_key(name)
}

/// Takes the specified key and its value if it exists
#[inline]
pub fn take(&mut self, name: &'static str) -> Option<(value::Key<'de>, Value<'de>)> {
self.expected.push(name);
self.table.remove_entry(&name.into())
self.table.remove_entry(name)
}

/// Attempts to deserialize the specified key
Expand All @@ -114,7 +114,7 @@ impl<'de> TableHelper<'de> {
) -> Result<Spanned<T>, Error> {
self.expected.push(name);

let Some(mut val) = self.table.remove(&name.into()) else {
let Some(mut val) = self.table.remove(name) else {
let missing = Error {
kind: ErrorKind::MissingField(name),
span: self.span,
Expand Down Expand Up @@ -144,7 +144,7 @@ impl<'de> TableHelper<'de> {
pub fn optional_s<T: Deserialize<'de>>(&mut self, name: &'static str) -> Option<Spanned<T>> {
self.expected.push(name);

let mut val = self.table.remove(&name.into())?;
let mut val = self.table.remove(name)?;

match Spanned::<T>::deserialize(&mut val) {
Ok(v) => Some(v),
Expand Down
27 changes: 15 additions & 12 deletions toml-span/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl<'de> Value<'de> {
pub fn has_key(&self, key: &str) -> bool {
self.value.as_ref().map_or(false, |val| {
if let ValueInner::Table(table) = val {
table.contains_key(&key.into())
table.contains_key(key)
} else {
false
}
Expand Down Expand Up @@ -144,7 +144,7 @@ impl<'de> Value<'de> {
/// Note that this is JSON pointer**-like** because `/` is not supported in
/// key names because I don't see the point. If you want this it is easy to
/// implement.
pub fn pointer(&self, pointer: &'de str) -> Option<&Self> {
pub fn pointer(&self, pointer: &str) -> Option<&Self> {
if pointer.is_empty() {
return Some(self);
} else if !pointer.starts_with('/') {
Expand All @@ -157,9 +157,9 @@ impl<'de> Value<'de> {
// Don't support / or ~ in key names unless someone actually opens
// an issue about it
//.map(|x| x.replace("~1", "/").replace("~0", "~"))
.try_fold(self, |target, token| {
.try_fold(self, move |target, token| {
(match &target.value {
Some(ValueInner::Table(tab)) => tab.get(&token.into()),
Some(ValueInner::Table(tab)) => tab.get(token),
Some(ValueInner::Array(list)) => parse_index(token).and_then(|x| list.get(x)),
_ => None,
})
Expand All @@ -183,7 +183,7 @@ impl<'de> Value<'de> {
//.map(|x| x.replace("~1", "/").replace("~0", "~"))
.try_fold(self, |target, token| {
(match &mut target.value {
Some(ValueInner::Table(tab)) => tab.get_mut(&token.into()),
Some(ValueInner::Table(tab)) => tab.get_mut(token),
Some(ValueInner::Array(list)) => {
parse_index(token).and_then(|x| list.get_mut(x))
}
Expand Down Expand Up @@ -225,18 +225,21 @@ pub struct Key<'de> {
pub span: Span,
}

impl<'de> From<&'de str> for Key<'de> {
fn from(k: &'de str) -> Self {
Self {
name: Cow::Borrowed(k),
span: Span::default(),
}
impl<'de> std::borrow::Borrow<str> for Key<'de> {
fn borrow(&self) -> &str {
self.name.as_ref()
}
}

impl<'de> fmt::Debug for Key<'de> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.name)
f.write_str(&self.name)
}
}

impl<'de> fmt::Display for Key<'de> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.name)
}
}

Expand Down

0 comments on commit 1212005

Please sign in to comment.