use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum SmartSelectionPrecision {
VeryLow,
Low,
#[default]
Normal,
High,
VeryHigh,
}
impl SmartSelectionPrecision {
pub fn value(&self) -> f64 {
match self {
SmartSelectionPrecision::VeryLow => 0.00001,
SmartSelectionPrecision::Low => 0.001,
SmartSelectionPrecision::Normal => 1.0,
SmartSelectionPrecision::High => 1000.0,
SmartSelectionPrecision::VeryHigh => 1_000_000.0,
}
}
pub fn display_name(&self) -> &'static str {
match self {
SmartSelectionPrecision::VeryLow => "Very Low",
SmartSelectionPrecision::Low => "Low",
SmartSelectionPrecision::Normal => "Normal",
SmartSelectionPrecision::High => "High",
SmartSelectionPrecision::VeryHigh => "Very High",
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SmartSelectionRule {
pub name: String,
pub regex: String,
#[serde(default)]
pub precision: SmartSelectionPrecision,
#[serde(default = "default_enabled")]
pub enabled: bool,
}
fn default_enabled() -> bool {
true
}
impl SmartSelectionRule {
pub fn new(
name: impl Into<String>,
regex: impl Into<String>,
precision: SmartSelectionPrecision,
) -> Self {
Self {
name: name.into(),
regex: regex.into(),
precision,
enabled: true,
}
}
}
pub fn default_smart_selection_rules() -> Vec<SmartSelectionRule> {
vec![
SmartSelectionRule::new(
"HTTP URL",
r"https?://[^\s<>\[\]{}|\\^`\x00-\x1f]+",
SmartSelectionPrecision::VeryHigh,
),
SmartSelectionRule::new(
"SSH URL",
r"\bssh://([a-zA-Z0-9_]+@)?([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+(/[^\s]*)?",
SmartSelectionPrecision::VeryHigh,
),
SmartSelectionRule::new(
"Git URL",
r"\bgit://([a-zA-Z0-9_]+@)?([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+(/[^\s]*)?",
SmartSelectionPrecision::VeryHigh,
),
SmartSelectionRule::new(
"File URL",
r"file://[^\s]+",
SmartSelectionPrecision::VeryHigh,
),
SmartSelectionRule::new(
"Email address",
r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b",
SmartSelectionPrecision::High,
),
SmartSelectionRule::new(
"IPv4 address",
r"\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b",
SmartSelectionPrecision::High,
),
SmartSelectionRule::new(
"File path",
r"~?/?(?:[a-zA-Z0-9._-]+/)+[a-zA-Z0-9._-]+/?",
SmartSelectionPrecision::Normal,
),
SmartSelectionRule::new(
"Java/Python import",
r"(?:[a-zA-Z_][a-zA-Z0-9_]*\.){2,}[a-zA-Z_][a-zA-Z0-9_]*",
SmartSelectionPrecision::Normal,
),
SmartSelectionRule::new(
"C++ namespace",
r"(?:[a-zA-Z_][a-zA-Z0-9_]*::)+[a-zA-Z_][a-zA-Z0-9_]*",
SmartSelectionPrecision::Normal,
),
SmartSelectionRule::new(
"Quoted string",
r#""(?:[^"\\]|\\.)*""#,
SmartSelectionPrecision::Normal,
),
SmartSelectionRule::new(
"UUID",
r"\b[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\b",
SmartSelectionPrecision::Normal,
),
]
}