Posts and Authors can now be inserted and retrieved from the database created. It was decided to use a SQLite database for it high read spead and ease of use/maintenance. A build feature was created to seperate how the library is being used. If you are making the database and storing posts, then use the publisher flag. If you are just reading from a database then do not use the publisher flag. This was also set to change the tale contents from a PathBuf to the String of HTML blog data without having to create a whole new data object. An example and a test were made. Test coverage needs to be increased however.
108 lines
3.1 KiB
Rust
108 lines
3.1 KiB
Rust
use std::path::{Path, PathBuf};
|
|
|
|
use chrono::{NaiveDate, NaiveDateTime};
|
|
|
|
use tavern::{Adventurer, Database, Tale, Tavern};
|
|
|
|
|
|
|
|
fn generate_tavern() -> Tavern
|
|
{
|
|
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: PathBuf::from("posts/test_post.md") };
|
|
|
|
|
|
// Create a dummy posts directory and file for this example to work
|
|
if !Path::new("posts").exists()
|
|
{
|
|
std::fs::create_dir("posts").unwrap();
|
|
}
|
|
std::fs::write("posts/the-rustacean.md",
|
|
"# Hello, Rust!\n\nThis is a **test** post.").unwrap();
|
|
|
|
|
|
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] }
|
|
}
|
|
|
|
|
|
fn read_from_file<P>(config_file: P) -> Tavern
|
|
where P: AsRef<Path>
|
|
{
|
|
// Read the previously written TOML file
|
|
let toml_data =
|
|
std::fs::read_to_string(&config_file).expect("Failed to read TOML file");
|
|
|
|
// Deserialize it
|
|
toml::from_str(&toml_data).expect("Failed to parse TOML")
|
|
}
|
|
|
|
|
|
fn create_database() -> Result<(), Box<dyn std::error::Error>>
|
|
{
|
|
// 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();
|
|
|
|
// Open the database and save the Tavern content
|
|
let db = Database::open(Path::new("tavern.db"))?;
|
|
|
|
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);
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub 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()
|
|
{
|
|
Ok(_) => {}
|
|
Err(e) => { eprintln!("Error: {}", e); }
|
|
}
|
|
}
|