diff options
author | Norbert Preining <norbert@preining.info> | 2020-05-28 03:03:21 +0000 |
---|---|---|
committer | Norbert Preining <norbert@preining.info> | 2020-05-28 03:03:21 +0000 |
commit | 745892fbddea56040139108277e728b53fd8fc11 (patch) | |
tree | 15e55e299b38fccdabce8364f1fbf4f02ef37994 /support/texlab/src/components.rs | |
parent | 195ca7e0c377d83455867bdd8e409d4e1cf024ea (diff) |
CTAN sync 202005280303
Diffstat (limited to 'support/texlab/src/components.rs')
-rw-r--r-- | support/texlab/src/components.rs | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/support/texlab/src/components.rs b/support/texlab/src/components.rs new file mode 100644 index 0000000000..76890c8b15 --- /dev/null +++ b/support/texlab/src/components.rs @@ -0,0 +1,94 @@ +use crate::protocol::{MarkupContent, MarkupKind}; +use once_cell::sync::Lazy; +use serde::{Deserialize, Serialize}; + +#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct Database { + pub components: Vec<Component>, + pub metadata: Vec<Metadata>, +} + +impl Database { + pub fn find(&self, name: &str) -> Option<&Component> { + self.components.iter().find(|component| { + component + .file_names + .iter() + .any(|file_name| file_name == name) + }) + } + + pub fn contains(&self, short_name: &str) -> bool { + let sty = format!("{}.sty", short_name); + let cls = format!("{}.cls", short_name); + self.find(&sty).is_some() || self.find(&cls).is_some() + } + + pub fn kernel(&self) -> &Component { + self.components + .iter() + .find(|component| component.file_names.is_empty()) + .unwrap() + } + + pub fn exists(&self, file_name: &str) -> bool { + self.components + .iter() + .any(|component| component.file_names.iter().any(|f| f == file_name)) + } + + pub fn documentation(&self, name: &str) -> Option<MarkupContent> { + let metadata = self + .metadata + .iter() + .find(|metadata| metadata.name == name)?; + + let desc = metadata.description.to_owned()?; + Some(MarkupContent { + kind: MarkupKind::PlainText, + value: desc, + }) + } +} + +#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct Component { + pub file_names: Vec<String>, + pub references: Vec<String>, + pub commands: Vec<Command>, + pub environments: Vec<String>, +} + +#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct Command { + pub name: String, + pub image: Option<String>, + pub glyph: Option<String>, + pub parameters: Vec<Parameter>, +} + +#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct Parameter(pub Vec<Argument>); + +#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct Argument { + pub name: String, + pub image: Option<String>, +} + +#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct Metadata { + pub name: String, + pub caption: Option<String>, + pub description: Option<String>, +} + +const JSON: &str = include_str!("../data/components.json"); + +pub static COMPONENT_DATABASE: Lazy<Database> = Lazy::new(|| serde_json::from_str(JSON).unwrap()); |