Skip to content

Commit

Permalink
Introduce combined data model to generate output from
Browse files Browse the repository at this point in the history
  • Loading branch information
homeworkprod committed May 19, 2024
1 parent c85fe97 commit 251a81b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 22 deletions.
24 changes: 17 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ mod io;
mod model;
mod templating;

use crate::config::{Config, DiscordConfig};
use crate::model::Data;

fn main() -> Result<()> {
let args = cli::parse_args();

Expand All @@ -25,24 +28,31 @@ fn main() -> Result<()> {
None => fetch_roles_from_discord(&config.discord)?,
};

let generated_at = Utc::now();
let data = assemble_data(config, roles);

match args.output_format {
cli::OutputFormat::Html => {
templating::render_html(config.title, config.subtitle, roles, generated_at, writer)?
}
cli::OutputFormat::Json => model::write_roles(roles, writer)?,
cli::OutputFormat::Text => templating::render_text(roles, writer)?,
cli::OutputFormat::Html => templating::render_html(data, writer)?,
cli::OutputFormat::Json => model::write_roles(data.roles, writer)?,
cli::OutputFormat::Text => templating::render_text(data, writer)?,
};

Ok(())
}

fn fetch_roles_from_discord(config: &config::DiscordConfig) -> Result<Vec<model::Role>> {
fn fetch_roles_from_discord(config: &DiscordConfig) -> Result<Vec<model::Role>> {
let api_client = discord_api::Client::new(&config.bot_token);
let guild_members = api_client.get_guild_members(&config.guild_id)?;
let guild_roles = api_client.get_guild_roles(&config.guild_id)?;

let roles = assembly::assemble_roles(&guild_members, &guild_roles, &config.roles_excluded);
Ok(roles)
}

fn assemble_data(config: Config, roles: Vec<model::Role>) -> Data {
Data {
title: config.title,
subtitle: config.subtitle,
roles,
generated_at: Utc::now(),
}
}
9 changes: 9 additions & 0 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,21 @@
*/

use anyhow::Result;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::io::Write;
use std::path::PathBuf;

use crate::io;

#[derive(Deserialize, Serialize, Debug)]
pub(crate) struct Data {
pub title: String,
pub subtitle: Option<String>,
pub roles: Vec<Role>,
pub generated_at: DateTime<Utc>,
}

#[derive(Deserialize, Serialize, Debug)]
pub(crate) struct Role {
pub name: String,
Expand Down
23 changes: 8 additions & 15 deletions src/templating.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
*/

use anyhow::Result;
use chrono::{DateTime, Utc};
use std::io::Write;
use std::sync::OnceLock;
use tera::{Context, Tera};

use crate::model::Role;
use crate::model::Data;

fn get_tera() -> &'static Tera {
static TERA: OnceLock<Tera> = OnceLock::new();
Expand All @@ -24,28 +23,22 @@ fn get_tera() -> &'static Tera {
}

/// Render roles as HTML representation.
pub(crate) fn render_html(
title: String,
subtitle: Option<String>,
roles: Vec<Role>,
generated_at: DateTime<Utc>,
writer: impl Write,
) -> Result<()> {
pub(crate) fn render_html(data: Data, writer: impl Write) -> Result<()> {
let mut context = Context::new();
context.insert("roles", &roles);
context.insert("title", &title);
context.insert("subtitle", &subtitle);
context.insert("generated_at", &generated_at);
context.insert("roles", &data.roles);
context.insert("title", &data.title);
context.insert("subtitle", &data.subtitle);
context.insert("generated_at", &data.generated_at);

render("index.html", &context, writer)?;

Ok(())
}

/// Render roles as text representation.
pub(crate) fn render_text(roles: Vec<Role>, writer: impl Write) -> Result<()> {
pub(crate) fn render_text(data: Data, writer: impl Write) -> Result<()> {
let mut context = Context::new();
context.insert("roles", &roles);
context.insert("roles", &data.roles);

render("index.txt", &context, writer)?;

Expand Down

0 comments on commit 251a81b

Please sign in to comment.