136 lines
3.9 KiB
Rust
136 lines
3.9 KiB
Rust
use std::path::{Path, PathBuf};
|
|
|
|
use chrono::NaiveDate;
|
|
use tavern::{Adventurer, Database, Legend, Lore, Tale, Tavern};
|
|
|
|
|
|
|
|
#[cfg(feature = "publisher")]
|
|
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!") };
|
|
|
|
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()
|
|
{
|
|
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] }
|
|
}
|
|
|
|
|
|
#[cfg(feature = "publisher")]
|
|
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")
|
|
}
|
|
|
|
|
|
#[cfg(feature = "publisher")]
|
|
async 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("/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);
|
|
// }
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(not(feature = "publisher"))]
|
|
pub fn main()
|
|
{
|
|
}
|