lib-humus 0.6.0

Helps creating configurable frontends for humans and computers using axum, Tera and toml.
Documentation
// SPDX-FileCopyrightText: 2026 Slatian <baschdel@disroot.org>
//
// SPDX-License-Identifier: AGPL-3.0-or-later

use mime::Mime;
use tinystr::tinystr;

use crate::templating::HumusFormatIdentifier;

/// Trait for types that describe the API format, that is managed by the application.
///
/// It is best implemented on an Enum type;
pub trait HumusApiFormat: Clone + Send {
	/// Returns the name of the format
	fn get_name(&self) -> HumusFormatIdentifier;

	/// Returns the Mimetype that is expected for this output format.
	fn get_media_type(&self) -> Mime;

	/// Constructs a view from its name.
	fn from_name(name: &HumusFormatIdentifier) -> Option<Self>;

	/// Returns a Vec of all variants for the purpose of enumerating them with their `get_name()` methods.
	fn get_all() -> Vec<Self>;
}

/// Implementation of [HumusApiFormat] that can handle the cases where there is only a JSON API next to the templates.
#[derive(Clone)]
pub struct JsonOnlyApiFormat;

impl HumusApiFormat for JsonOnlyApiFormat {
	fn get_name(&self) -> HumusFormatIdentifier {
		tinystr!(16, "json")
	}

	fn get_media_type(&self) -> Mime {
		mime::APPLICATION_JSON
	}

	fn from_name(name: &HumusFormatIdentifier) -> Option<Self> {
		match name.as_str() {
			"json" => Some(Self),
			_ => None,
		}
	}

	fn get_all() -> Vec<Self> {
		vec![Self]
	}
}