Skip to content

Commit

Permalink
marked-yaml: Check that we never reach the expecting() function in Sp…
Browse files Browse the repository at this point in the history
…anned<T>

Signed-off-by: Daniel Silverstone <dsilvers@digital-scurf.org>
  • Loading branch information
kinnison committed Apr 13, 2024
1 parent 3266213 commit 221a0fa
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions marked-yaml/src/spanned_serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ where
{
type Value = Spanned<T>;

fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("a MarkedNode of some kind")
fn expecting(&self, _formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
unreachable!()
}

fn visit_map<V>(self, mut visitor: V) -> Result<Self::Value, V::Error>
Expand Down Expand Up @@ -222,6 +222,36 @@ where
}
}

#[cfg(test)]
mod spanned_tests {
use super::Spanned;
use serde::{forward_to_deserialize_any, Deserialize, Deserializer};

#[test]
#[should_panic]
fn spanned_always_map() {
struct NotSpanned;
impl<'de> Deserializer<'de> for NotSpanned {
type Error = super::Error;

fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: serde::de::Visitor<'de>,
{
visitor.visit_bool(false)
}

forward_to_deserialize_any! [
bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes byte_buf
unit unit_struct newtype_struct seq tuple tuple_struct
map identifier ignored_any option struct enum
];
}
type T = Spanned<bool>;
let _ = T::deserialize(NotSpanned);
}
}

// -------------------------------------------------------------------------------

/// Errors which can come from deserialisation
Expand Down

0 comments on commit 221a0fa

Please sign in to comment.