2025-08-27 17:36:06 -04:00
use chrono ::{ NaiveDate , NaiveDateTime } ;
2025-08-22 10:46:38 -04:00
use tavern ::{ Adventurer , Tale , Tavern } ;
fn generate_tavern ( ) -> Tavern
{
2025-08-27 17:36:06 -04:00
let author : Adventurer =
Adventurer { name : String ::from ( " Jason Smith " ) ,
handle : String ::from ( " myrddin " ) ,
profile :
String ::from ( " https://cybermages.tech/about/myrddin " ) ,
image :
String ::from ( " https://cybermages.tech/about/myrddin/pic " ) ,
blurb : String ::from ( " I love code! " ) } ;
let tale : Tale =
Tale { title : String ::from ( " Test post " ) ,
slug : String ::from ( " test_post " ) ,
author : author . handle . clone ( ) ,
summary : String ::from ( " The Moon is made of cheese! " ) ,
tags : vec ! [ String ::from ( " Space " ) , String ::from ( " Cheese " ) ] ,
publish_date : NaiveDate ::from_ymd_opt ( 2025 , 12 , 25 ) . unwrap ( ) . and_hms_opt ( 13 , 10 , 41 ) . unwrap ( ) ,
content : std ::path ::PathBuf ::from ( " posts/test_post.md " ) } ;
Tavern { title : String ::from ( " Runes & Ramblings " ) ,
description : String ::from ( " Join software engineer Jason Smith on his Rust programming journey. Explore program design, tech stacks, and more on this blog from CybeMages, LLC. " ) ,
tales : vec ! [ tale ] ,
authors : vec ! [ author ] }
2025-08-22 10:46:38 -04:00
}
2025-08-27 17:36:06 -04:00
2025-08-22 10:46:38 -04:00
fn cleanup_temp_file ( path : & std ::path ::PathBuf )
{
if path . exists ( )
{
let _ = std ::fs ::remove_file ( path ) ;
}
}
#[ test ]
fn to_file ( )
{
let tavern = generate_tavern ( ) ;
2025-08-27 17:36:06 -04:00
let toml_string = toml ::to_string_pretty ( & tavern ) . expect ( " Serialization \
to TOML should \
succeed " );
2025-08-22 10:46:38 -04:00
// Save the TOML to a temporary file.
let mut path = std ::env ::temp_dir ( ) ;
path . push ( " tavern_test_out.toml " ) ;
2025-08-27 17:36:06 -04:00
std ::fs ::write ( & path , & toml_string ) . expect ( " Failed to write TOML to file " ) ;
2025-08-22 10:46:38 -04:00
cleanup_temp_file ( & path ) ;
}
#[ test ]
fn from_file ( )
{
let tavern = generate_tavern ( ) ;
2025-08-27 17:36:06 -04:00
let toml_string = toml ::to_string_pretty ( & tavern ) . expect ( " Serialization \
to TOML should \
succeed " );
2025-08-22 10:46:38 -04:00
// Save the TOML to a temporary file.
let mut path = std ::env ::temp_dir ( ) ;
path . push ( " tavern_test_in.toml " ) ;
2025-08-27 17:36:06 -04:00
std ::fs ::write ( & path , & toml_string ) . expect ( " Failed to write TOML to file " ) ;
2025-08-22 10:46:38 -04:00
// Read the previously written TOML file
2025-08-27 17:36:06 -04:00
let toml_data =
std ::fs ::read_to_string ( & path ) . expect ( " Failed to read TOML file " ) ;
2025-08-22 10:46:38 -04:00
// Deserialize it
2025-08-27 17:36:06 -04:00
let tavern : Tavern =
toml ::from_str ( & toml_data ) . expect ( " Failed to parse TOML " ) ;
2025-08-22 10:46:38 -04:00
// Assert some known values to make this a real test
let tale = & tavern . tales [ 0 ] ;
assert_eq! ( tale . title , " Test post " ) ;
assert_eq! ( tale . slug , " test_post " ) ;
assert_eq! ( tale . author , " myrddin " ) ;
cleanup_temp_file ( & path ) ;
}