use sherpack_core::{Dependency, LoadedPack};
use std::path::PathBuf;
#[derive(Debug, Clone)]
pub struct SubchartConfig {
pub max_depth: usize,
pub subcharts_dir: String,
pub strict: bool,
}
impl Default for SubchartConfig {
fn default() -> Self {
Self {
max_depth: 10,
subcharts_dir: "charts".to_string(),
strict: false,
}
}
}
impl SubchartConfig {
pub fn new() -> Self {
Self::default()
}
pub fn with_max_depth(mut self, depth: usize) -> Self {
self.max_depth = depth;
self
}
pub fn with_subcharts_dir(mut self, dir: impl Into<String>) -> Self {
self.subcharts_dir = dir.into();
self
}
pub fn strict(mut self) -> Self {
self.strict = true;
self
}
}
#[derive(Debug)]
pub struct SubchartInfo {
pub name: String,
pub path: PathBuf,
pub pack: LoadedPack,
pub enabled: bool,
pub dependency: Option<Dependency>,
pub disabled_reason: Option<String>,
}
impl SubchartInfo {
pub fn should_render(&self) -> bool {
self.enabled
}
pub fn scope_name(&self) -> &str {
&self.name
}
}
#[derive(Debug, Default)]
pub struct DiscoveryResult {
pub subcharts: Vec<SubchartInfo>,
pub warnings: Vec<String>,
pub missing: Vec<String>,
}
impl DiscoveryResult {
pub fn new() -> Self {
Self::default()
}
pub fn enabled_subcharts(&self) -> impl Iterator<Item = &SubchartInfo> {
self.subcharts.iter().filter(|s| s.enabled)
}
pub fn disabled_subcharts(&self) -> impl Iterator<Item = &SubchartInfo> {
self.subcharts.iter().filter(|s| !s.enabled)
}
pub fn has_issues(&self) -> bool {
!self.warnings.is_empty() || !self.missing.is_empty()
}
pub fn total_count(&self) -> usize {
self.subcharts.len()
}
pub fn enabled_count(&self) -> usize {
self.subcharts.iter().filter(|s| s.enabled).count()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_subchart_config_default() {
let config = SubchartConfig::default();
assert_eq!(config.max_depth, 10);
assert_eq!(config.subcharts_dir, "charts");
assert!(!config.strict);
}
#[test]
fn test_subchart_config_builder() {
let config = SubchartConfig::new()
.with_max_depth(5)
.with_subcharts_dir("packs")
.strict();
assert_eq!(config.max_depth, 5);
assert_eq!(config.subcharts_dir, "packs");
assert!(config.strict);
}
#[test]
fn test_discovery_result_counts() {
let mut result = DiscoveryResult::new();
assert_eq!(result.total_count(), 0);
assert_eq!(result.enabled_count(), 0);
assert!(!result.has_issues());
result.warnings.push("test warning".to_string());
assert!(result.has_issues());
result.missing.push("missing-chart".to_string());
assert!(result.has_issues());
}
}