feat: add plugin schema system, tokenizer cache, and config validation
- Add plugin schema types and runtime discovery for meta/filter plugins - Rewrite --generate-config to use schema system instead of hardcoded types - Add Settings::validate_config() for startup validation - Cache tokenizer instances via static Lazy to avoid repeated BPE loading - Add split_by_token_iter() and count_bounded() to Tokenizer - Fix double-counting bug in TokensMetaPlugin when buffer < max_buffer_size - Eliminate unnecessary allocations in token count methods - Refactor token filters: remove Option<Tokenizer>, use iterator API - Fix TailTokensFilter correctness: unbounded buffer instead of ring buffer - Add encoding option to all token filters - Add description() to MetaPlugin and FilterPlugin traits - Fix unused_mut warning in compression engine (feature-gated code) Co-Authored-By: code-review-bot <noreply@anthropic.com>
This commit is contained in:
@@ -1,81 +1,17 @@
|
||||
use crate::meta_plugin::MetaPlugin;
|
||||
use anyhow::Result;
|
||||
use clap::Command;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_yaml;
|
||||
use std::collections::HashMap;
|
||||
use strum::IntoEnumIterator;
|
||||
|
||||
/// Mode for generating a default configuration file.
|
||||
///
|
||||
/// This module creates a commented YAML template with default values for settings,
|
||||
/// including list format, server config, compression, and meta plugins.
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
/// Default configuration structure for the generated template.
|
||||
///
|
||||
/// Includes core settings, list formatting, server options, compression, and meta plugins.
|
||||
struct DefaultConfig {
|
||||
dir: Option<String>,
|
||||
list_format: Vec<ColumnConfig>,
|
||||
human_readable: bool,
|
||||
output_format: Option<String>,
|
||||
quiet: bool,
|
||||
force: bool,
|
||||
server: Option<ServerConfig>,
|
||||
compression_plugin: Option<CompressionPluginConfig>,
|
||||
meta_plugins: Option<Vec<MetaPluginConfig>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
/// Configuration for a column in the list format.
|
||||
struct ColumnConfig {
|
||||
name: String,
|
||||
label: Option<String>,
|
||||
#[serde(default)]
|
||||
align: ColumnAlignment,
|
||||
#[serde(default)]
|
||||
max_len: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Default)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
/// Alignment options for table columns.
|
||||
enum ColumnAlignment {
|
||||
#[default]
|
||||
Left,
|
||||
Right,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
/// Server configuration options.
|
||||
struct ServerConfig {
|
||||
address: Option<String>,
|
||||
port: Option<u16>,
|
||||
password_file: Option<String>,
|
||||
password: Option<String>,
|
||||
password_hash: Option<String>,
|
||||
cors_origin: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
/// Configuration for the compression plugin.
|
||||
struct CompressionPluginConfig {
|
||||
name: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
/// Configuration for a meta plugin.
|
||||
struct MetaPluginConfig {
|
||||
name: String,
|
||||
#[serde(default)]
|
||||
options: std::collections::HashMap<String, serde_yaml::Value>,
|
||||
#[serde(default)]
|
||||
outputs: std::collections::HashMap<String, String>,
|
||||
}
|
||||
use crate::common::schema::{gather_filter_plugin_schemas, gather_meta_plugin_schemas};
|
||||
use crate::compression_engine::CompressionType;
|
||||
use crate::config;
|
||||
|
||||
/// Generates and prints a default commented YAML configuration template.
|
||||
///
|
||||
/// Creates instances of available meta plugins to populate default options and outputs,
|
||||
/// then serializes the config to YAML with all lines commented for easy editing.
|
||||
/// Discovers all registered meta plugins, filter plugins, and compression engines
|
||||
/// at runtime via the plugin schema system. Outputs a commented YAML template
|
||||
/// with all available plugins and their default options/outputs.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
@@ -85,153 +21,244 @@ struct MetaPluginConfig {
|
||||
/// # Returns
|
||||
///
|
||||
/// `Ok(())` on success.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```ignore
|
||||
/// // Example usage requires Command and Settings instances
|
||||
/// mode_generate_config(&mut cmd, &settings)?;
|
||||
/// ```
|
||||
pub fn mode_generate_config(_cmd: &mut Command, _settings: &crate::config::Settings) -> Result<()> {
|
||||
// Create instances of each meta plugin to get their default options and outputs
|
||||
let cwd_plugin = crate::meta_plugin::cwd::CwdMetaPlugin::new(None, None);
|
||||
let digest_plugin = crate::meta_plugin::digest::DigestMetaPlugin::new(None, None);
|
||||
let hostname_plugin = crate::meta_plugin::hostname::HostnameMetaPlugin::new(None, None);
|
||||
#[cfg(feature = "magic")]
|
||||
let magic_file_plugin = crate::meta_plugin::magic_file::MagicFileMetaPlugin::new(None, None);
|
||||
let env_plugin = crate::meta_plugin::env::EnvMetaPlugin::new(None, None);
|
||||
let meta_schemas = gather_meta_plugin_schemas();
|
||||
let filter_schemas = gather_filter_plugin_schemas();
|
||||
|
||||
// Create a default configuration
|
||||
let default_config = DefaultConfig {
|
||||
dir: Some("~/.local/share/keep".to_string()),
|
||||
list_format: vec![
|
||||
ColumnConfig {
|
||||
name: "id".to_string(),
|
||||
label: Some("Item".to_string()),
|
||||
align: ColumnAlignment::Right,
|
||||
max_len: None,
|
||||
},
|
||||
ColumnConfig {
|
||||
name: "time".to_string(),
|
||||
label: Some("Time".to_string()),
|
||||
align: ColumnAlignment::Right,
|
||||
max_len: None,
|
||||
},
|
||||
ColumnConfig {
|
||||
name: "size".to_string(),
|
||||
label: Some("Size".to_string()),
|
||||
align: ColumnAlignment::Right,
|
||||
max_len: None,
|
||||
},
|
||||
ColumnConfig {
|
||||
name: "tags".to_string(),
|
||||
label: Some("Tags".to_string()),
|
||||
align: ColumnAlignment::Left,
|
||||
max_len: Some("40".to_string()),
|
||||
},
|
||||
ColumnConfig {
|
||||
name: "meta:hostname_full".to_string(),
|
||||
label: Some("Hostname".to_string()),
|
||||
align: ColumnAlignment::Left,
|
||||
max_len: Some("28".to_string()),
|
||||
},
|
||||
],
|
||||
human_readable: false,
|
||||
output_format: Some("table".to_string()),
|
||||
quiet: false,
|
||||
force: false,
|
||||
server: Some(ServerConfig {
|
||||
address: Some("127.0.0.1".to_string()),
|
||||
port: Some(8080),
|
||||
password_file: None,
|
||||
password: None,
|
||||
password_hash: None,
|
||||
cors_origin: None,
|
||||
}),
|
||||
compression_plugin: None,
|
||||
meta_plugins: Some(vec![
|
||||
MetaPluginConfig {
|
||||
name: "cwd".to_string(),
|
||||
options: cwd_plugin.options().clone(),
|
||||
outputs: convert_outputs_to_string_map(cwd_plugin.outputs()),
|
||||
},
|
||||
MetaPluginConfig {
|
||||
name: "digest".to_string(),
|
||||
options: digest_plugin.options().clone(),
|
||||
outputs: convert_outputs_to_string_map(digest_plugin.outputs()),
|
||||
},
|
||||
MetaPluginConfig {
|
||||
name: "hostname".to_string(),
|
||||
options: hostname_plugin.options().clone(),
|
||||
outputs: convert_outputs_to_string_map(hostname_plugin.outputs()),
|
||||
},
|
||||
#[cfg(feature = "magic")]
|
||||
MetaPluginConfig {
|
||||
name: "magic_file".to_string(),
|
||||
options: magic_file_plugin.options().clone(),
|
||||
outputs: convert_outputs_to_string_map(magic_file_plugin.outputs()),
|
||||
},
|
||||
MetaPluginConfig {
|
||||
name: "env".to_string(),
|
||||
options: env_plugin.options().clone(),
|
||||
outputs: convert_outputs_to_string_map(env_plugin.outputs()),
|
||||
},
|
||||
]),
|
||||
};
|
||||
// Build list_format defaults matching config.rs
|
||||
let list_format = default_list_format();
|
||||
|
||||
// Serialize to YAML and comment out all lines
|
||||
let yaml = serde_yaml::to_string(&default_config)?;
|
||||
// Build meta_plugins with env as the default (active), rest commented
|
||||
let meta_plugins = build_meta_plugins_section(&meta_schemas);
|
||||
|
||||
// Comment out every line
|
||||
let commented_yaml = yaml
|
||||
.lines()
|
||||
.map(|line| {
|
||||
if line.trim().is_empty() {
|
||||
line.to_string()
|
||||
} else {
|
||||
format!("# {line}")
|
||||
// Build the full YAML
|
||||
let mut lines = Vec::with_capacity(128);
|
||||
|
||||
lines.push("# Keep configuration file".to_string());
|
||||
lines.push("# Uncomment and modify the settings you need.".to_string());
|
||||
lines.push(String::new());
|
||||
|
||||
// Core settings
|
||||
lines.push("# Data directory for storing items".to_string());
|
||||
lines.push("dir: ~/.local/share/keep".to_string());
|
||||
lines.push(String::new());
|
||||
|
||||
// List format
|
||||
lines.push("# Column configuration for --list output".to_string());
|
||||
lines.push("list_format:".to_string());
|
||||
for col in &list_format {
|
||||
lines.push(format!(" - name: {}", col.name));
|
||||
lines.push(format!(" label: {}", col.label));
|
||||
lines.push(format!(" align: {}", col.align));
|
||||
}
|
||||
lines.push(String::new());
|
||||
|
||||
// Table config
|
||||
lines.push("# Table display configuration".to_string());
|
||||
lines.push("#table_config:".to_string());
|
||||
lines.push("# style: nothing".to_string());
|
||||
lines.push("# modifiers: []".to_string());
|
||||
lines.push("# content_arrangement: dynamic".to_string());
|
||||
lines.push("# truncination_indicator: \"\"".to_string());
|
||||
lines.push(String::new());
|
||||
|
||||
// Other settings
|
||||
lines.push("human_readable: false".to_string());
|
||||
lines.push("output_format: table".to_string());
|
||||
lines.push("quiet: false".to_string());
|
||||
lines.push("force: false".to_string());
|
||||
lines.push(String::new());
|
||||
|
||||
// Server config
|
||||
lines.push("# Server configuration (only used with --server)".to_string());
|
||||
lines.push("server:".to_string());
|
||||
lines.push(" address: 127.0.0.1".to_string());
|
||||
lines.push(" port: 8080".to_string());
|
||||
lines.push("# username: keep".to_string());
|
||||
lines.push("# password: null".to_string());
|
||||
lines.push("# password_file: null".to_string());
|
||||
lines.push("# password_hash: null".to_string());
|
||||
lines.push("# jwt_secret: null".to_string());
|
||||
lines.push("# jwt_secret_file: null".to_string());
|
||||
lines.push("# cert_file: null".to_string());
|
||||
lines.push("# key_file: null".to_string());
|
||||
lines.push("# cors_origin: null".to_string());
|
||||
lines.push(String::new());
|
||||
|
||||
// Compression plugin
|
||||
lines.push("# Compression plugin to use".to_string());
|
||||
lines.push("#compression_plugin:".to_string());
|
||||
let mut comp_types: Vec<String> = CompressionType::iter().map(|ct| ct.to_string()).collect();
|
||||
comp_types.sort();
|
||||
for ct in &comp_types {
|
||||
lines.push(format!("# name: {ct} # {}", compression_description(ct)));
|
||||
}
|
||||
lines.push(String::new());
|
||||
|
||||
// Meta plugins
|
||||
lines.push("# Meta plugins to run when saving items".to_string());
|
||||
lines.push("meta_plugins:".to_string());
|
||||
for line in &meta_plugins {
|
||||
lines.push(line.clone());
|
||||
}
|
||||
lines.push(String::new());
|
||||
|
||||
// Filter plugins reference
|
||||
if !filter_schemas.is_empty() {
|
||||
lines.push("# Available filter plugins (use with --filter)".to_string());
|
||||
for schema in &filter_schemas {
|
||||
lines.push(format!("# {}", schema.name));
|
||||
if !schema.description.is_empty() {
|
||||
lines.push(format!("# {}", schema.description));
|
||||
}
|
||||
})
|
||||
.collect::<Vec<String>>()
|
||||
.join("\n");
|
||||
for opt in &schema.options {
|
||||
let req = if opt.required { "required" } else { "optional" };
|
||||
lines.push(format!(
|
||||
"# {} ({:?}, {})",
|
||||
opt.name, opt.option_type, req
|
||||
));
|
||||
}
|
||||
}
|
||||
lines.push(String::new());
|
||||
}
|
||||
|
||||
println!("{commented_yaml}");
|
||||
// Client config
|
||||
lines.push("# Client configuration (requires client feature)".to_string());
|
||||
lines.push("#client:".to_string());
|
||||
lines.push("# url: null".to_string());
|
||||
lines.push("# username: null".to_string());
|
||||
lines.push("# password: null".to_string());
|
||||
lines.push("# jwt: null".to_string());
|
||||
|
||||
// Print
|
||||
for line in &lines {
|
||||
println!("{line}");
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Helper function to convert outputs from serde_yaml::Value to String.
|
||||
///
|
||||
/// Handles null (uses key), strings, and other values by serializing to YAML string.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `outputs` - Reference to the outputs HashMap.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// A HashMap with string keys and values.
|
||||
fn convert_outputs_to_string_map(
|
||||
outputs: &std::collections::HashMap<String, serde_yaml::Value>,
|
||||
) -> std::collections::HashMap<String, String> {
|
||||
let mut result = std::collections::HashMap::new();
|
||||
for (key, value) in outputs {
|
||||
match value {
|
||||
serde_yaml::Value::Null => {
|
||||
// For null, use the key as the value
|
||||
result.insert(key.clone(), key.clone());
|
||||
struct ListColumn {
|
||||
name: String,
|
||||
label: String,
|
||||
align: String,
|
||||
}
|
||||
|
||||
fn default_list_format() -> Vec<ListColumn> {
|
||||
vec![
|
||||
ListColumn {
|
||||
name: "id".into(),
|
||||
label: "Item".into(),
|
||||
align: "right".into(),
|
||||
},
|
||||
ListColumn {
|
||||
name: "time".into(),
|
||||
label: "Time".into(),
|
||||
align: "right".into(),
|
||||
},
|
||||
ListColumn {
|
||||
name: "size".into(),
|
||||
label: "Size".into(),
|
||||
align: "right".into(),
|
||||
},
|
||||
ListColumn {
|
||||
name: "meta:text_line_count".into(),
|
||||
label: "Lines".into(),
|
||||
align: "right".into(),
|
||||
},
|
||||
ListColumn {
|
||||
name: "tags".into(),
|
||||
label: "Tags".into(),
|
||||
align: "left".into(),
|
||||
},
|
||||
ListColumn {
|
||||
name: "meta:hostname_short".into(),
|
||||
label: "Host".into(),
|
||||
align: "left".into(),
|
||||
},
|
||||
ListColumn {
|
||||
name: "meta:command".into(),
|
||||
label: "Command".into(),
|
||||
align: "left".into(),
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
fn build_meta_plugins_section(schemas: &[crate::common::schema::PluginSchema]) -> Vec<String> {
|
||||
let mut lines = Vec::new();
|
||||
|
||||
for (i, schema) in schemas.iter().enumerate() {
|
||||
let is_default = schema.name == "env";
|
||||
let prefix = if is_default { "" } else { "# " };
|
||||
|
||||
if i > 0 {
|
||||
lines.push(format!("{prefix}# --- {name} ---", name = schema.name));
|
||||
}
|
||||
|
||||
lines.push(format!("{prefix}- name: {}", schema.name));
|
||||
|
||||
// Options
|
||||
if !schema.options.is_empty() {
|
||||
lines.push(format!("{prefix} options:"));
|
||||
for opt in &schema.options {
|
||||
if let Some(ref default) = opt.default {
|
||||
let default_str = format_yaml_value(default);
|
||||
lines.push(format!("{prefix} {}: {}", opt.name, default_str));
|
||||
} else if opt.required {
|
||||
lines.push(format!("{prefix} {}: null # required", opt.name));
|
||||
}
|
||||
}
|
||||
serde_yaml::Value::String(s) => {
|
||||
result.insert(key.clone(), s.clone());
|
||||
}
|
||||
_ => {
|
||||
// Convert other values to their YAML string representation
|
||||
result.insert(
|
||||
key.clone(),
|
||||
serde_yaml::to_string(value).unwrap_or_default(),
|
||||
);
|
||||
} else {
|
||||
lines.push(format!("{prefix} options: {{}}"));
|
||||
}
|
||||
|
||||
// Outputs
|
||||
if !schema.outputs.is_empty() {
|
||||
lines.push(format!("{prefix} outputs:"));
|
||||
for output in &schema.outputs {
|
||||
lines.push(format!("{prefix} {}: {}", output.name, output.name));
|
||||
}
|
||||
} else {
|
||||
lines.push(format!("{prefix} outputs: {{}}"));
|
||||
}
|
||||
}
|
||||
result
|
||||
|
||||
lines
|
||||
}
|
||||
|
||||
fn format_yaml_value(value: &serde_yaml::Value) -> String {
|
||||
match value {
|
||||
serde_yaml::Value::Null => "null".into(),
|
||||
serde_yaml::Value::Bool(b) => b.to_string(),
|
||||
serde_yaml::Value::Number(n) => n.to_string(),
|
||||
serde_yaml::Value::String(s) => {
|
||||
if s.contains(' ') || s.contains(':') || s.contains('#') {
|
||||
format!("\"{s}\"")
|
||||
} else {
|
||||
s.clone()
|
||||
}
|
||||
}
|
||||
serde_yaml::Value::Sequence(_) | serde_yaml::Value::Mapping(_) => {
|
||||
serde_yaml::to_string(value)
|
||||
.unwrap_or_default()
|
||||
.trim()
|
||||
.to_string()
|
||||
}
|
||||
serde_yaml::Value::Tagged(_) => serde_yaml::to_string(value)
|
||||
.unwrap_or_default()
|
||||
.trim()
|
||||
.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
fn compression_description(name: &str) -> &str {
|
||||
match name {
|
||||
"lz4" => "Fast compression (native)",
|
||||
"gzip" => "Good compression ratio (native)",
|
||||
"bzip2" => "High compression (requires bzip2 binary)",
|
||||
"xz" => "Very high compression (requires xz binary)",
|
||||
"zstd" => "Modern fast compression (requires zstd binary)",
|
||||
"none" => "No compression",
|
||||
_ => "",
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user