use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use serde_json::Value;
use sha1::{Digest, Sha1};
use crate::backgrounds::{LocalBackground, NullBackground, UnsplashBackground};
use crate::config::{Format, LaracardsConfig};
use crate::contracts::Background;
use crate::generator::CardGenerator;
use crate::support::Raster;
pub trait IntoBackground {
fn into_background(self) -> Arc<dyn Background>;
}
impl<B: Background + 'static> IntoBackground for B {
fn into_background(self) -> Arc<dyn Background> {
Arc::new(self)
}
}
impl IntoBackground for Arc<dyn Background> {
fn into_background(self) -> Arc<dyn Background> {
self
}
}
impl IntoBackground for &str {
fn into_background(self) -> Arc<dyn Background> {
Arc::new(LocalBackground::new(self))
}
}
impl IntoBackground for String {
fn into_background(self) -> Arc<dyn Background> {
Arc::new(LocalBackground::new(self))
}
}
impl IntoBackground for &Path {
fn into_background(self) -> Arc<dyn Background> {
Arc::new(LocalBackground::new(self))
}
}
impl IntoBackground for PathBuf {
fn into_background(self) -> Arc<dyn Background> {
Arc::new(LocalBackground::new(self))
}
}
impl<T: IntoBackground> IntoBackground for Option<T> {
fn into_background(self) -> Arc<dyn Background> {
match self {
Some(background) => background.into_background(),
None => Arc::new(NullBackground),
}
}
}
#[derive(Clone)]
pub struct Card {
key: String,
template: String,
data: BTreeMap<String, Value>,
background: Option<Arc<dyn Background>>,
output: Option<PathBuf>,
}
impl Card {
pub fn make(key: impl Into<String>) -> Self {
Self {
key: key.into(),
template: "post".to_owned(),
data: BTreeMap::new(),
background: None,
output: None,
}
}
#[must_use]
pub fn template(mut self, template: impl Into<String>) -> Self {
self.template = template.into();
self
}
#[must_use]
pub fn data(mut self, data: impl Into<Value>) -> Self {
if let Value::Object(fields) = data.into() {
self.data.extend(fields);
}
self
}
#[must_use]
pub fn background(mut self, background: impl IntoBackground) -> Self {
self.background = Some(background.into_background());
self
}
#[must_use]
pub fn unsplash(self, query: impl Into<String>) -> Self {
self.background(UnsplashBackground::new(query))
}
#[must_use]
pub fn output(mut self, path: impl Into<PathBuf>) -> Self {
self.output = Some(path.into());
self
}
pub fn key(&self) -> &str {
&self.key
}
pub fn template_name(&self) -> &str {
&self.template
}
pub fn payload(&self) -> &BTreeMap<String, Value> {
&self.data
}
pub fn scalar(&self, field: &str) -> String {
match self.data.get(field) {
Some(Value::String(text)) => text.clone(),
Some(Value::Number(number)) => number.to_string(),
Some(Value::Bool(true)) => "1".to_owned(),
_ => String::new(),
}
}
pub fn background_driver(&self) -> Arc<dyn Background> {
self.background.clone().unwrap_or_else(|| Arc::new(NullBackground))
}
pub fn output_path(&self) -> PathBuf {
if let Some(output) = &self.output {
return output.clone();
}
let config = LaracardsConfig::current();
let format = config
.templates
.get(&self.template)
.and_then(|template| template.format)
.unwrap_or(config.format);
LaracardsConfig::resolve(&LaracardsConfig::base(), &config.paths.output).join(format!(
"{}.{}",
self.key,
format.extension()
))
}
pub fn fingerprint(&self) -> String {
let parts = serde_json::json!({
"template": self.template,
"data": self.data,
"background": self.background_driver().fingerprint(),
});
let digest = Sha1::digest(parts.to_string());
digest.iter().map(|byte| format!("{byte:02x}")).collect()
}
pub async fn generate(&self, force: bool) -> laravel::Result<Option<PathBuf>> {
laravel::resolve::<CardGenerator>().await?.generate(self, force).await
}
pub(crate) fn output_format(&self) -> Option<Format> {
Raster::normalise(self.output_path().extension().and_then(|extension| extension.to_str()))
}
}