use serde::{Deserialize, Serialize};
use crate::{error::Result, lexicon::ExternalLexicons, util::normalize::lookup_key, Error};
pub const PLUGIN_SCHEMA_VERSION: u32 = 1;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct LexiconPlugin {
pub metadata: PluginMetadata,
pub payload: PluginPayload,
}
impl LexiconPlugin {
#[must_use]
pub fn payload_kind(&self) -> PluginPayloadKind {
self.payload.kind()
}
pub fn validate(&self) -> Result<()> {
self.metadata.validate()?;
self.payload.validate()
}
pub fn register_into(&self, external: &mut ExternalLexicons) -> Result<()> {
self.validate()?;
external.register_plugin(self)
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct PluginMetadata {
pub schema_version: u32,
pub plugin_version: u32,
pub source_id: String,
pub source_version: Option<String>,
pub upstream_url: Option<String>,
pub prepared_at: String,
pub checksum: Option<String>,
pub license_summary: String,
pub notice: Option<String>,
}
impl PluginMetadata {
#[must_use]
pub fn new(source_id: impl Into<String>, license_summary: impl Into<String>) -> Self {
Self {
schema_version: PLUGIN_SCHEMA_VERSION,
plugin_version: 1,
source_id: source_id.into(),
source_version: None,
upstream_url: None,
prepared_at: "unknown".to_string(),
checksum: None,
license_summary: license_summary.into(),
notice: None,
}
}
pub fn validate(&self) -> Result<()> {
if self.schema_version != PLUGIN_SCHEMA_VERSION {
return Err(Error::UnsupportedVersion {
found: self.schema_version,
expected: PLUGIN_SCHEMA_VERSION,
});
}
if self.source_id.trim().is_empty() {
return Err(Error::InvalidData("plugin source_id cannot be empty".to_string()));
}
if self.license_summary.trim().is_empty() {
return Err(Error::InvalidData("plugin license_summary cannot be empty".to_string()));
}
Ok(())
}
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "kebab-case")]
pub enum PluginPayloadKind {
WordSet,
CanonicalMap,
MultiwordMap,
RankedWords,
ProtectedSpellings,
}
impl PluginPayloadKind {
pub(crate) const fn tag(self) -> u8 {
match self {
Self::WordSet => 1,
Self::CanonicalMap => 2,
Self::MultiwordMap => 3,
Self::RankedWords => 4,
Self::ProtectedSpellings => 5,
}
}
pub(crate) fn from_tag(tag: u8) -> Result<Self> {
match tag {
1 => Ok(Self::WordSet),
2 => Ok(Self::CanonicalMap),
3 => Ok(Self::MultiwordMap),
4 => Ok(Self::RankedWords),
5 => Ok(Self::ProtectedSpellings),
_ => Err(Error::InvalidData(format!("unknown plugin payload tag {tag}"))),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct MapEntry {
pub key: String,
pub value: String,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct RankedEntry {
pub word: String,
pub rank: u64,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(tag = "kind", rename_all = "kebab-case")]
pub enum PluginPayload {
WordSet {
words: Vec<String>,
},
CanonicalMap {
entries: Vec<MapEntry>,
},
MultiwordMap {
entries: Vec<MapEntry>,
},
RankedWords {
entries: Vec<RankedEntry>,
},
ProtectedSpellings {
entries: Vec<MapEntry>,
},
}
impl PluginPayload {
#[must_use]
pub fn kind(&self) -> PluginPayloadKind {
match self {
Self::WordSet { .. } => PluginPayloadKind::WordSet,
Self::CanonicalMap { .. } => PluginPayloadKind::CanonicalMap,
Self::MultiwordMap { .. } => PluginPayloadKind::MultiwordMap,
Self::RankedWords { .. } => PluginPayloadKind::RankedWords,
Self::ProtectedSpellings { .. } => PluginPayloadKind::ProtectedSpellings,
}
}
#[must_use]
pub fn len(&self) -> usize {
match self {
Self::WordSet { words } => words.len(),
Self::CanonicalMap { entries }
| Self::MultiwordMap { entries }
| Self::ProtectedSpellings { entries } => entries.len(),
Self::RankedWords { entries } => entries.len(),
}
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub(crate) fn validate(&self) -> Result<()> {
match self {
Self::WordSet { words } => validate_words(words),
Self::CanonicalMap { entries } | Self::ProtectedSpellings { entries } => {
validate_map_entries(entries)
}
Self::MultiwordMap { entries } => validate_multiword_entries(entries),
Self::RankedWords { entries } => validate_ranked_entries(entries),
}
}
}
fn validate_words(words: &[String]) -> Result<()> {
let mut seen = std::collections::BTreeSet::new();
for word in words {
let key = lookup_key(word);
if key.is_empty() {
return Err(Error::InvalidData("word-set entries cannot be empty".to_string()));
}
if !seen.insert(key) {
return Err(Error::InvalidData("word-set entries must be unique".to_string()));
}
}
Ok(())
}
fn validate_map_entries(entries: &[MapEntry]) -> Result<()> {
let mut seen = std::collections::BTreeSet::new();
for entry in entries {
let key = lookup_key(&entry.key);
if key.is_empty() || entry.value.trim().is_empty() {
return Err(Error::InvalidData(
"map entries require non-empty keys and values".to_string(),
));
}
if !seen.insert(key) {
return Err(Error::InvalidData("map entries must be unique".to_string()));
}
}
Ok(())
}
fn validate_multiword_entries(entries: &[MapEntry]) -> Result<()> {
validate_map_entries(entries)?;
for entry in entries {
if !entry.key.contains(' ') {
return Err(Error::InvalidData(
"multiword-map entries require keys containing at least one space".to_string(),
));
}
}
Ok(())
}
fn validate_ranked_entries(entries: &[RankedEntry]) -> Result<()> {
let mut seen = std::collections::BTreeSet::new();
for entry in entries {
let key = lookup_key(&entry.word);
if key.is_empty() {
return Err(Error::InvalidData(
"ranked-word entries require a non-empty word".to_string(),
));
}
if !seen.insert(key) {
return Err(Error::InvalidData("ranked-word entries must be unique".to_string()));
}
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::{
LexiconPlugin, MapEntry, PluginMetadata, PluginPayload, PluginPayloadKind, RankedEntry,
};
use crate::ExternalLexicons;
#[test]
fn validates_payloads() {
let plugin = LexiconPlugin {
metadata: PluginMetadata::new("fixture", "MIT"),
payload: PluginPayload::CanonicalMap {
entries: vec![MapEntry { key: "github".to_string(), value: "GitHub".to_string() }],
},
};
assert_eq!(plugin.payload_kind(), PluginPayloadKind::CanonicalMap);
assert!(plugin.validate().is_ok());
}
#[test]
fn rejects_duplicate_words() {
let plugin = LexiconPlugin {
metadata: PluginMetadata::new("fixture", "MIT"),
payload: PluginPayload::WordSet { words: vec!["and".to_string(), "AND".to_string()] },
};
assert!(plugin.validate().is_err());
}
#[test]
fn validates_ranked_entries() {
let plugin = LexiconPlugin {
metadata: PluginMetadata::new("fixture", "MIT"),
payload: PluginPayload::RankedWords {
entries: vec![RankedEntry { word: "common".to_string(), rank: 7 }],
},
};
assert!(plugin.validate().is_ok());
}
#[test]
fn validates_multiword_entries() {
let plugin = LexiconPlugin {
metadata: PluginMetadata::new("fixture", "MIT"),
payload: PluginPayload::MultiwordMap {
entries: vec![MapEntry {
key: "new york city".to_string(),
value: "New York City".to_string(),
}],
},
};
assert!(plugin.validate().is_ok());
}
#[test]
fn registers_plugins_into_external_lexicons() {
let plugin = LexiconPlugin {
metadata: PluginMetadata::new("fixture", "MIT"),
payload: PluginPayload::ProtectedSpellings {
entries: vec![MapEntry { key: "github".to_string(), value: "GitHub".to_string() }],
},
};
let mut lexicons = ExternalLexicons::default();
plugin.register_into(&mut lexicons).unwrap();
assert_eq!(lexicons.protected_spelling("github"), Some("GitHub"));
}
}