Adjusting to make the database better tested.
This commit is contained in:
@ -1,51 +1,66 @@
|
||||
use std::path::{Path, PathBuf};
|
||||
#![cfg(feature = "publisher")]
|
||||
|
||||
use chrono::NaiveDate;
|
||||
use tavern::{Adventurer, Database, Legend, Lore, Tale, Tavern};
|
||||
|
||||
use tavern::{Adventurer, Legend, Lore, Story, Tale, Tavern};
|
||||
use tavern::Database;
|
||||
|
||||
|
||||
|
||||
#[cfg(feature = "publisher")]
|
||||
/// This will generate a tavern that we can create a Toml file from.
|
||||
fn generate_tavern() -> Tavern
|
||||
{
|
||||
let legend: Legend = Legend
|
||||
{
|
||||
profile:
|
||||
String::from("https://cybermages.tech/about/myrddin"),
|
||||
image:
|
||||
String::from("https://cybermages.tech/about/myrddin/pic"),
|
||||
blurb: String::from("I love code!") };
|
||||
profile: String::from("https://cybermages.tech/about/myrddin"),
|
||||
image: String::from("https://cybermages.tech/about/myrddin/pic"),
|
||||
blurb: String::from("I love code!")
|
||||
};
|
||||
|
||||
let author: Adventurer =
|
||||
Adventurer { name: String::from("Jason Smith"),
|
||||
handle: String::from("myrddin"),
|
||||
legend };
|
||||
|
||||
let lore: Lore =
|
||||
Lore { 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() };
|
||||
|
||||
let tale: Tale = Tale { lore,
|
||||
story: PathBuf::from("posts/test_post.md") };
|
||||
|
||||
|
||||
// Create a dummy posts directory and file for this example to work
|
||||
if !Path::new("posts").exists()
|
||||
let author: Adventurer = Adventurer
|
||||
{
|
||||
std::fs::create_dir("posts").unwrap();
|
||||
}
|
||||
std::fs::write("posts/the-rustacean.md",
|
||||
"# Hello, Rust!\n\nThis is a **test** post.").unwrap();
|
||||
name: String::from("Jason Smith"),
|
||||
handle: String::from("myrddin"),
|
||||
legend: legend
|
||||
};
|
||||
|
||||
let lore: Lore = Lore
|
||||
{
|
||||
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()
|
||||
};
|
||||
|
||||
let tale: Tale = Tale
|
||||
{
|
||||
lore: lore,
|
||||
story: Story::Html("#Test Post\nThis is a test post.\n\n##Subsection\nMini post.\n* Test 1\n* Test 2\n* Test 3".to_string())
|
||||
};
|
||||
|
||||
let lore2: Lore = Lore
|
||||
{
|
||||
title: String::from("Test Tale"),
|
||||
slug: String::from("test_tale"),
|
||||
author: author.handle.clone(),
|
||||
summary: String::from("The Moon is made of rocks!"),
|
||||
tags: vec![String::from("Space"), String::from("Rocks")],
|
||||
publish_date:
|
||||
NaiveDate::from_ymd_opt(2025, 12, 25).unwrap()
|
||||
.and_hms_opt(13, 10, 41)
|
||||
.unwrap()
|
||||
};
|
||||
|
||||
let tale2: Tale = Tale
|
||||
{
|
||||
lore: lore2,
|
||||
story: Story::Html("#Test Tale\nThis is a test tale.\n\n##Subsection\nMini tale.\n* Test 1\n* Test 2\n* Test 3".to_string())
|
||||
};
|
||||
|
||||
Tavern { title: String::from("Runes & Ramblings"),
|
||||
description: String::from("Join software engineer Jason Smith \
|
||||
@ -53,14 +68,52 @@ fn generate_tavern() -> Tavern
|
||||
Explore program design, tech \
|
||||
stacks, and more on this blog from \
|
||||
CybeMages, LLC."),
|
||||
tales: vec![tale],
|
||||
tales: vec![tale, tale2],
|
||||
authors: vec![author] }
|
||||
}
|
||||
|
||||
|
||||
#[cfg(feature = "publisher")]
|
||||
fn create_temp_file<P>(filename: P) -> std::path::PathBuf
|
||||
where P: AsRef<std::path::Path>
|
||||
{
|
||||
let mut path = std::env::temp_dir();
|
||||
path.push(filename);
|
||||
|
||||
path
|
||||
}
|
||||
|
||||
fn cleanup_temp_file<P>(path: P)
|
||||
where P: AsRef<std::path::Path>
|
||||
{
|
||||
match path.as_ref().try_exists()
|
||||
{
|
||||
Ok(exists) =>
|
||||
{
|
||||
if exists
|
||||
{
|
||||
let _ = std::fs::remove_file(path);
|
||||
}
|
||||
}
|
||||
|
||||
Err(e) =>
|
||||
{
|
||||
eprintln!("{}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn write_to_file<P>(tavern: Tavern, config_file: P) -> Result<(), Box<dyn std::error::Error>>
|
||||
where P: AsRef<std::path::Path>
|
||||
{
|
||||
let toml_string = toml::to_string_pretty(&tavern)?;
|
||||
|
||||
std::fs::write(&config_file, &toml_string)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn read_from_file<P>(config_file: P) -> Tavern
|
||||
where P: AsRef<Path>
|
||||
where P: AsRef<std::path::Path>
|
||||
{
|
||||
// Read the previously written TOML file
|
||||
let toml_data =
|
||||
@ -71,65 +124,36 @@ fn read_from_file<P>(config_file: P) -> Tavern
|
||||
}
|
||||
|
||||
|
||||
#[cfg(feature = "publisher")]
|
||||
async fn create_database() -> Result<(), Box<dyn std::error::Error>>
|
||||
{
|
||||
// First we need to generate a TOML file to work with.
|
||||
let config_path = create_temp_file("tavern.toml");
|
||||
cleanup_temp_file(&config_path);
|
||||
|
||||
write_to_file(generate_tavern(), &config_path)?;
|
||||
|
||||
// This part would be the entry point of your CI/CD script
|
||||
// It would load your data and then save it to the database
|
||||
|
||||
// Create a Tavern object
|
||||
let _tavern = read_from_file("Tavern.toml");
|
||||
// let tavern = generate_tavern();
|
||||
let tavern = read_from_file(&config_path);
|
||||
|
||||
// Open the database and save the Tavern content
|
||||
let _db = Database::open("/home/myrddin/cybermages/blog/tavern.db").await?;
|
||||
//db.insert_tavern(&tavern.title, &tavern.description)?;
|
||||
//println!("Saved site settings: Title='{}', Description='{}'",
|
||||
//tavern.title, tavern.description);
|
||||
|
||||
//for author in &tavern.authors
|
||||
// {
|
||||
// db.insert_adventurer(author)?;
|
||||
// println!("Saved adventurer: {}", author.name);
|
||||
// }
|
||||
//
|
||||
// for tale in &tavern.tales
|
||||
// {
|
||||
// db.insert_tale(tale)?;
|
||||
// println!("Saved tale: {}", tale.title);
|
||||
// }
|
||||
let db_path = create_temp_file("tavern.db");
|
||||
cleanup_temp_file(&db_path);
|
||||
|
||||
let db = Database::open(db_path).await?;
|
||||
db.insert_tavern(&tavern).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(feature = "publisher")]
|
||||
#[tokio::main]
|
||||
pub async fn main()
|
||||
{
|
||||
match std::env::set_current_dir("/home/myrddin/cybermages/blog/")
|
||||
{
|
||||
Ok(_) =>
|
||||
{
|
||||
println!("Successfully changed working directory.");
|
||||
}
|
||||
Err(e) =>
|
||||
{
|
||||
eprintln!("Failed to change directory: {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
match create_database().await
|
||||
{
|
||||
Ok(_) =>
|
||||
{}
|
||||
Err(e) =>
|
||||
{
|
||||
eprintln!("Error: {}", e);
|
||||
}
|
||||
Ok(_) => {}
|
||||
Err(e) => { eprintln!("Error: {}", e); }
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "publisher"))]
|
||||
pub fn main()
|
||||
{
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user