75 lines
1.6 KiB
Rust
75 lines
1.6 KiB
Rust
use std::path::{Path, PathBuf};
|
|
|
|
use serde::Deserialize;
|
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
pub enum ContentError {
|
|
#[error("Base64 decoding error: {0}")]
|
|
Base64(#[from] base64::DecodeError),
|
|
}
|
|
|
|
#[derive(Debug, serde::Deserialize)]
|
|
#[serde(rename_all = "lowercase")]
|
|
enum ContentEncoding {
|
|
Raw,
|
|
Base64,
|
|
}
|
|
|
|
#[derive(Debug, Default, Deserialize)]
|
|
pub struct Hooks {
|
|
#[serde(default)]
|
|
pub changed: Vec<String>,
|
|
}
|
|
|
|
#[derive(Debug, serde::Deserialize)]
|
|
pub struct ConfigEntry {
|
|
path: PathBuf,
|
|
owner: Option<String>,
|
|
group: Option<String>,
|
|
mode: Option<String>,
|
|
#[serde(default)]
|
|
content_encoding: ContentEncoding,
|
|
content: String,
|
|
#[serde(default)]
|
|
hooks: Hooks,
|
|
}
|
|
|
|
impl Default for ContentEncoding {
|
|
fn default() -> Self {
|
|
Self::Raw
|
|
}
|
|
}
|
|
|
|
impl ConfigEntry {
|
|
pub fn path(&self) -> &Path {
|
|
self.path.as_ref()
|
|
}
|
|
|
|
pub fn owner(&self) -> Option<&str> {
|
|
self.owner.as_deref()
|
|
}
|
|
|
|
pub fn group(&self) -> Option<&str> {
|
|
self.group.as_deref()
|
|
}
|
|
|
|
pub fn mode(&self) -> Option<&str> {
|
|
self.mode.as_deref()
|
|
}
|
|
|
|
pub fn content(&self) -> Result<Vec<u8>, ContentError> {
|
|
match &self.content_encoding {
|
|
ContentEncoding::Raw => Ok(self.content.as_bytes().into()),
|
|
ContentEncoding::Base64 => Ok(base64_decode(&self.content)?),
|
|
}
|
|
}
|
|
|
|
pub fn hooks(&self) -> &Hooks {
|
|
&self.hooks
|
|
}
|
|
}
|
|
|
|
fn base64_decode(value: &str) -> Result<Vec<u8>, base64::DecodeError> {
|
|
base64::Engine::decode(&base64::engine::general_purpose::STANDARD, value)
|
|
}
|