cstats-cli 0.1.1

Command line interface for cstats
//! Command implementations for cstats CLI

use std::collections::HashMap;

use chrono::{DateTime, Utc};
use cstats_core::Result;

use crate::OutputFormat;

#[allow(unused_imports)] // Commands are exported for submodule use
use crate::{
    AnalyzeArgs, CacheAction, CacheArgs, CollectArgs, ConfigAction, ConfigArgs, HookArgs, InitArgs,
    ShellType, StatsArgs, StatsPeriod,
};

mod analyze;
mod cache;
mod collect;
mod config;
mod hook;
mod init;
mod stats;

pub use analyze::*;
pub use cache::*;
pub use collect::*;
pub use config::*;
pub use hook::*;
pub use init::*;
pub use stats::*;

/// Format and print output based on the specified format
pub fn print_output<T>(data: &T, format: OutputFormat) -> Result<()>
where
    T: serde::Serialize,
{
    match format {
        OutputFormat::Json => {
            let json = serde_json::to_string_pretty(data)?;
            println!("{}", json);
        }
        OutputFormat::Yaml => {
            // For now, we'll use JSON format as a fallback
            // In a real implementation, you'd add serde_yaml dependency
            let json = serde_json::to_string_pretty(data)?;
            println!("{}", json);
        }
        OutputFormat::Text => {
            // For text format, we'll implement custom Display logic in each command
            let json = serde_json::to_string_pretty(data)?;
            println!("{}", json);
        }
    }
    Ok(())
}

/// Parse metadata from key=value strings
pub fn parse_metadata(metadata_strings: Vec<String>) -> HashMap<String, String> {
    let mut metadata = HashMap::new();

    for item in metadata_strings {
        if let Some((key, value)) = item.split_once('=') {
            metadata.insert(key.to_string(), value.to_string());
        }
    }

    metadata
}

/// Parse ISO 8601 datetime string
pub fn parse_datetime(datetime_str: &str) -> Result<DateTime<Utc>> {
    datetime_str
        .parse::<DateTime<Utc>>()
        .map_err(|e| cstats_core::Error::config(format!("Invalid datetime format: {}", e)))
}