use serde::{Deserialize, Serialize};
use std::path::PathBuf;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StorageConfig {
pub global_path: PathBuf,
pub project_path: Option<PathBuf>,
pub mode: StorageMode,
pub first_run: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum StorageMode {
GlobalOnly,
ProjectOnly,
Merged,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum ResourceType {
Template,
Standard,
Spec,
Steering,
Boilerplate,
Rule,
CustomCommand,
LspLanguageConfig,
CompletionLanguageConfig,
HooksConfig,
RefactoringLanguageConfig,
}
impl ResourceType {
pub fn dir_name(&self) -> &'static str {
match self {
ResourceType::Template => "templates",
ResourceType::Standard => "standards",
ResourceType::Spec => "specs",
ResourceType::Steering => "steering",
ResourceType::Boilerplate => "boilerplates",
ResourceType::Rule => "rules",
ResourceType::CustomCommand => "commands",
ResourceType::LspLanguageConfig => "lsp/languages",
ResourceType::CompletionLanguageConfig => "completion/languages",
ResourceType::HooksConfig => "hooks",
ResourceType::RefactoringLanguageConfig => "refactoring/languages",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ConfigFormat {
Yaml,
Toml,
Json,
}
impl ConfigFormat {
pub fn extension(&self) -> &'static str {
match self {
ConfigFormat::Yaml => "yaml",
ConfigFormat::Toml => "toml",
ConfigFormat::Json => "json",
}
}
pub fn from_extension(ext: &str) -> Option<Self> {
match ext.to_lowercase().as_str() {
"yaml" | "yml" => Some(ConfigFormat::Yaml),
"toml" => Some(ConfigFormat::Toml),
"json" => Some(ConfigFormat::Json),
_ => None,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum DocumentFormat {
Yaml,
Markdown,
}
impl DocumentFormat {
pub fn extension(&self) -> &'static str {
match self {
DocumentFormat::Yaml => "yaml",
DocumentFormat::Markdown => "md",
}
}
pub fn from_extension(ext: &str) -> Option<Self> {
match ext.to_lowercase().as_str() {
"yaml" | "yml" => Some(DocumentFormat::Yaml),
"md" | "markdown" => Some(DocumentFormat::Markdown),
_ => None,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum StorageState {
Available,
Unavailable { reason: String },
ReadOnly { cached_at: String },
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_resource_type_dir_names() {
assert_eq!(ResourceType::Template.dir_name(), "templates");
assert_eq!(ResourceType::Standard.dir_name(), "standards");
assert_eq!(ResourceType::Spec.dir_name(), "specs");
assert_eq!(ResourceType::Steering.dir_name(), "steering");
assert_eq!(ResourceType::Boilerplate.dir_name(), "boilerplates");
assert_eq!(ResourceType::Rule.dir_name(), "rules");
assert_eq!(ResourceType::CustomCommand.dir_name(), "commands");
assert_eq!(ResourceType::HooksConfig.dir_name(), "hooks");
assert_eq!(
ResourceType::RefactoringLanguageConfig.dir_name(),
"refactoring/languages"
);
}
#[test]
fn test_config_format_extensions() {
assert_eq!(ConfigFormat::Yaml.extension(), "yaml");
assert_eq!(ConfigFormat::Toml.extension(), "toml");
assert_eq!(ConfigFormat::Json.extension(), "json");
}
#[test]
fn test_config_format_detection() {
assert_eq!(
ConfigFormat::from_extension("yaml"),
Some(ConfigFormat::Yaml)
);
assert_eq!(
ConfigFormat::from_extension("yml"),
Some(ConfigFormat::Yaml)
);
assert_eq!(
ConfigFormat::from_extension("toml"),
Some(ConfigFormat::Toml)
);
assert_eq!(
ConfigFormat::from_extension("json"),
Some(ConfigFormat::Json)
);
assert_eq!(ConfigFormat::from_extension("txt"), None);
}
#[test]
fn test_document_format_detection() {
assert_eq!(
DocumentFormat::from_extension("yaml"),
Some(DocumentFormat::Yaml)
);
assert_eq!(
DocumentFormat::from_extension("md"),
Some(DocumentFormat::Markdown)
);
assert_eq!(
DocumentFormat::from_extension("markdown"),
Some(DocumentFormat::Markdown)
);
assert_eq!(DocumentFormat::from_extension("txt"), None);
}
}