From 9b9e713ebaa9e934d291c4e220872433b5fcc9f6 Mon Sep 17 00:00:00 2001 From: Ruben Beltran del Rio Date: Sun, 6 Apr 2025 01:09:42 +0200 Subject: Use gets all across --- src/post.rs | 41 +++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 18 deletions(-) (limited to 'src/post.rs') diff --git a/src/post.rs b/src/post.rs index 20322b4..2e2b0e6 100644 --- a/src/post.rs +++ b/src/post.rs @@ -84,9 +84,9 @@ mod tests { fn test_creates_context_with_empty_posts() { let context = Post::to_template_context(&[]); - assert_eq!(context["has_posts"], Value::Bool(false)); - assert_eq!(context["posts_length"], Value::Unsigned(0)); - if let Value::Collection(posts) = &context["posts"] { + assert_eq!(context.get("has_posts"), Some(&Value::Bool(false))); + assert_eq!(context.get("posts_length"), Some(&Value::Unsigned(0))); + if let Some(Value::Collection(posts)) = &context.get("posts") { assert!(posts.is_empty()); } else { panic!("The posts context was not the right type."); @@ -107,9 +107,9 @@ mod tests { let context = Post::to_template_context(&[post]); - assert_eq!(context["has_posts"], Value::Bool(true)); - assert_eq!(context["posts_length"], Value::Unsigned(1)); - if let Value::Collection(posts) = &context["posts"] { + assert_eq!(context.get("has_posts"), Some(&Value::Bool(true))); + assert_eq!(context.get("posts_length"), Some(&Value::Unsigned(1))); + if let Some(Value::Collection(posts)) = &context.get("posts") { if let Some(post) = posts.first() { assert_eq!(post["id"], Value::String("cool".to_string())); } else { @@ -137,29 +137,34 @@ beep boop" let context = post.to_template_value(); - assert_eq!(context["id"], Value::String("cool".to_string())); - assert_eq!(context["created_on"], Value::Unsigned(1_736_035_200_000)); + assert_eq!(context.get("id"), Some(&Value::String("cool".to_string()))); assert_eq!( - context["created_on_utc"], - Value::String("Sun, 05 Jan 2025 00:00:00 +0000".to_string()) + context.get("created_on"), + Some(&Value::Unsigned(1_736_035_200_000)) ); assert_eq!( - context["title"], - Value::String("Hello everybody".to_string()) + context.get("created_on_utc"), + Some(&Value::String( + "Sun, 05 Jan 2025 00:00:00 +0000".to_string() + )) ); - assert_eq!(context["index"], Value::Unsigned(28)); assert_eq!( - context["html"], - Value::String("

beep boop

".to_string()) + context.get("title"), + Some(&Value::String("Hello everybody".to_string())) ); + assert_eq!(context.get("index"), Some(&Value::Unsigned(28))); assert_eq!( - context["raw"], - Value::String( + context.get("html"), + Some(&Value::String("

beep boop

".to_string())) + ); + assert_eq!( + context.get("raw"), + Some(&Value::String( "\ # Hello everybody beep boop" .to_string() - ) + )) ); } } -- cgit