use std::fmt;
use std::path::Path;
use std::str::FromStr;
use crate::render::config_loader;
use crate::render::{
CopperlaceValue, ProcessorRegistry, RenderContext, RenderError, RenderOptions, RuleSet,
};
#[derive(Debug, PartialEq, Eq)]
pub enum ConfigError {
Parse(String),
Render(RenderError),
}
impl fmt::Display for ConfigError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ConfigError::Parse(error) => write!(formatter, "failed to parse config: {error}"),
ConfigError::Render(error) => write!(formatter, "{error}"),
}
}
}
impl std::error::Error for ConfigError {}
impl From<RenderError> for ConfigError {
fn from(error: RenderError) -> Self {
ConfigError::Render(error)
}
}
pub struct Copperlace {
ruleset: RuleSet,
}
impl Copperlace {
#[allow(clippy::should_implement_trait)]
pub fn from_str(config: &str) -> Result<Self, ConfigError> {
Ok(Self {
ruleset: ruleset_from_str(config)?,
})
}
pub fn from_file(path: impl AsRef<Path>) -> Result<Self, ConfigError> {
Ok(Self {
ruleset: ruleset_from_file(path)?,
})
}
pub fn render(&self, rule_name: &str) -> Result<String, RenderError> {
self.ruleset.render_rule(rule_name)
}
pub fn render_with_options(
&self,
rule_name: &str,
options: RenderOptions,
) -> Result<String, RenderError> {
self.ruleset.render_rule_with_options(rule_name, options)
}
pub fn render_with_context(
&self,
rule_name: &str,
context: RenderContext,
) -> Result<String, RenderError> {
self.ruleset.render_rule_with_context(rule_name, context)
}
pub fn render_with_context_and_options(
&self,
rule_name: &str,
context: RenderContext,
options: RenderOptions,
) -> Result<String, RenderError> {
self.ruleset
.render_rule_with_context_and_options(rule_name, context, options)
}
pub fn render_inferred(&self, rule_name: &str) -> Result<String, RenderError> {
self.ruleset.render_rule_inferred(rule_name)
}
pub fn render_inferred_with_options(
&self,
rule_name: &str,
options: RenderOptions,
) -> Result<String, RenderError> {
self.ruleset
.render_rule_inferred_with_options(rule_name, options)
}
pub fn render_inferred_with_context(
&self,
rule_name: &str,
context: RenderContext,
) -> Result<String, RenderError> {
self.ruleset
.render_rule_inferred_with_context(rule_name, context)
}
pub fn render_inferred_with_context_and_options(
&self,
rule_name: &str,
context: RenderContext,
options: RenderOptions,
) -> Result<String, RenderError> {
self.ruleset
.render_rule_inferred_with_context_and_options(rule_name, context, options)
}
pub fn render_structured(&self, rule_name: &str) -> Result<CopperlaceValue, RenderError> {
self.ruleset.render_rule_structured(rule_name)
}
pub fn render_structured_with_options(
&self,
rule_name: &str,
options: RenderOptions,
) -> Result<CopperlaceValue, RenderError> {
self.ruleset
.render_rule_structured_with_options(rule_name, options)
}
pub fn render_structured_with_context(
&self,
rule_name: &str,
context: RenderContext,
) -> Result<CopperlaceValue, RenderError> {
self.ruleset
.render_rule_structured_with_context(rule_name, context)
}
pub fn render_structured_with_context_and_options(
&self,
rule_name: &str,
context: RenderContext,
options: RenderOptions,
) -> Result<CopperlaceValue, RenderError> {
self.ruleset
.render_rule_structured_with_context_and_options(rule_name, context, options)
}
}
impl FromStr for Copperlace {
type Err = ConfigError;
fn from_str(config: &str) -> Result<Self, Self::Err> {
Ok(Self {
ruleset: ruleset_from_str(config)?,
})
}
}
pub fn ruleset_from_str(config: &str) -> Result<RuleSet, ConfigError> {
ruleset_from_str_with_processors(config, ProcessorRegistry::new())
}
pub(crate) fn ruleset_from_str_with_processors(
config: &str,
processors: ProcessorRegistry,
) -> Result<RuleSet, ConfigError> {
let parsed = config_loader::load_str(config, None).map_err(ConfigError::Parse)?;
RuleSet::from_template_config(parsed, processors).map_err(ConfigError::Render)
}
fn config_options_for_file(path: &Path) -> hocon_rs::ConfigOptions {
let mut locations = Vec::new();
if let Some(parent) = path.parent() {
locations.push(parent.to_string_lossy().to_string());
}
if let Ok(current_dir) = std::env::current_dir() {
let current_dir = current_dir.to_string_lossy().to_string();
if !locations.iter().any(|location| location == ¤t_dir) {
locations.push(current_dir);
}
}
hocon_rs::ConfigOptions::new(false, locations)
}
pub fn ruleset_from_file(path: impl AsRef<Path>) -> Result<RuleSet, ConfigError> {
ruleset_from_file_with_processors(path, ProcessorRegistry::new())
}
pub(crate) fn ruleset_from_file_with_processors(
path: impl AsRef<Path>,
processors: ProcessorRegistry,
) -> Result<RuleSet, ConfigError> {
let path = path.as_ref();
let parsed = config_loader::load_file(path, config_options_for_file(path))
.map_err(ConfigError::Parse)?;
RuleSet::from_template_config(parsed, processors).map_err(ConfigError::Render)
}
pub fn render_str(config: &str, rule_name: &str) -> Result<String, ConfigError> {
render_str_with_context(config, rule_name, RenderContext::new())
}
pub fn render_str_with_context(
config: &str,
rule_name: &str,
context: RenderContext,
) -> Result<String, ConfigError> {
render_str_with_context_and_options(config, rule_name, context, RenderOptions::default())
}
pub fn render_str_with_context_and_options(
config: &str,
rule_name: &str,
context: RenderContext,
options: RenderOptions,
) -> Result<String, ConfigError> {
ruleset_from_str(config)?
.render_rule_with_context_and_options(rule_name, context, options)
.map_err(ConfigError::Render)
}
pub fn render_str_inferred(config: &str, rule_name: &str) -> Result<String, ConfigError> {
render_str_inferred_with_context(config, rule_name, RenderContext::new())
}
pub fn render_str_inferred_with_context(
config: &str,
rule_name: &str,
context: RenderContext,
) -> Result<String, ConfigError> {
render_str_inferred_with_context_and_options(
config,
rule_name,
context,
RenderOptions::default(),
)
}
pub fn render_str_inferred_with_context_and_options(
config: &str,
rule_name: &str,
context: RenderContext,
options: RenderOptions,
) -> Result<String, ConfigError> {
ruleset_from_str(config)?
.render_rule_inferred_with_context_and_options(rule_name, context, options)
.map_err(ConfigError::Render)
}
pub fn render_str_structured(
config: &str,
rule_name: &str,
) -> Result<CopperlaceValue, ConfigError> {
render_str_structured_with_context(config, rule_name, RenderContext::new())
}
pub fn render_str_structured_with_context(
config: &str,
rule_name: &str,
context: RenderContext,
) -> Result<CopperlaceValue, ConfigError> {
render_str_structured_with_context_and_options(
config,
rule_name,
context,
RenderOptions::default(),
)
}
pub fn render_str_structured_with_context_and_options(
config: &str,
rule_name: &str,
context: RenderContext,
options: RenderOptions,
) -> Result<CopperlaceValue, ConfigError> {
ruleset_from_str(config)?
.render_rule_structured_with_context_and_options(rule_name, context, options)
.map_err(ConfigError::Render)
}
pub fn render_file(path: impl AsRef<Path>, rule_name: &str) -> Result<String, ConfigError> {
render_file_with_context(path, rule_name, RenderContext::new())
}
pub fn render_file_with_context(
path: impl AsRef<Path>,
rule_name: &str,
context: RenderContext,
) -> Result<String, ConfigError> {
render_file_with_context_and_options(path, rule_name, context, RenderOptions::default())
}
pub fn render_file_with_context_and_options(
path: impl AsRef<Path>,
rule_name: &str,
context: RenderContext,
options: RenderOptions,
) -> Result<String, ConfigError> {
ruleset_from_file(path)?
.render_rule_with_context_and_options(rule_name, context, options)
.map_err(ConfigError::Render)
}
pub fn render_file_inferred(
path: impl AsRef<Path>,
rule_name: &str,
) -> Result<String, ConfigError> {
render_file_inferred_with_context(path, rule_name, RenderContext::new())
}
pub fn render_file_inferred_with_context(
path: impl AsRef<Path>,
rule_name: &str,
context: RenderContext,
) -> Result<String, ConfigError> {
render_file_inferred_with_context_and_options(
path,
rule_name,
context,
RenderOptions::default(),
)
}
pub fn render_file_inferred_with_context_and_options(
path: impl AsRef<Path>,
rule_name: &str,
context: RenderContext,
options: RenderOptions,
) -> Result<String, ConfigError> {
ruleset_from_file(path)?
.render_rule_inferred_with_context_and_options(rule_name, context, options)
.map_err(ConfigError::Render)
}
pub fn render_file_structured(
path: impl AsRef<Path>,
rule_name: &str,
) -> Result<CopperlaceValue, ConfigError> {
render_file_structured_with_context(path, rule_name, RenderContext::new())
}
pub fn render_file_structured_with_context(
path: impl AsRef<Path>,
rule_name: &str,
context: RenderContext,
) -> Result<CopperlaceValue, ConfigError> {
render_file_structured_with_context_and_options(
path,
rule_name,
context,
RenderOptions::default(),
)
}
pub fn render_file_structured_with_context_and_options(
path: impl AsRef<Path>,
rule_name: &str,
context: RenderContext,
options: RenderOptions,
) -> Result<CopperlaceValue, ConfigError> {
ruleset_from_file(path)?
.render_rule_structured_with_context_and_options(rule_name, context, options)
.map_err(ConfigError::Render)
}