vii/
textprop.rs

1#![allow(dead_code)]
2#![allow(unused_variables)]
3//! Text Properties
4//!
5//! Text properties are properties that can be optionally added to text to give them special meaning.
6//!
7//! The real power behind text properties is that they move with the text, regardless of position.
8//!
9//! Text properties do not have persistence outside of Vim. Once your instance closes, the text properties are removed as well.
10//!
11//! https://vimhelp.org/textprop.txt.html
12//!
13//! ```
14//! use vii::textprop::{
15//!     TextProperty,
16//!     PropertyType,
17//! };
18//! use vii::textprop::PropertyTypeBuilder;
19//!
20//! // Create New Property
21//!
22//! let prop = TextProperty { id: 0, r#type: "number".to_string() };
23//!
24//! // Create New Property Type
25//!
26//! let prop_type = PropertyTypeBuilder::default().highlight("Constant".to_string()).build();
27//! ```
28
29use std::str::FromStr;
30
31use serde::{Deserialize, Serialize};
32
33type HighlightGroup = String;
34
35/// Text Property (Base)
36#[derive(Serialize, Deserialize)]
37pub struct TextProperty {
38    pub id: i32,
39    pub r#type: String,
40}
41
42/// Text Property Type
43#[derive(Default, Serialize, Deserialize)]
44pub struct PropertyType {
45    highlight: Option<HighlightGroup>,
46    combine: Option<bool>,
47    priority: Option<i32>,
48    start_include: Option<bool>,
49    end_include: Option<bool>,
50}
51
52/// Parse JSON
53impl FromStr for PropertyType {
54    type Err = serde_json::Error;
55
56    fn from_str(s: &str) -> Result<Self, Self::Err> {
57        serde_json::from_str(s)
58    }
59}
60
61#[derive(Default)]
62pub struct PropertyTypeBuilder {
63    highlight: Option<HighlightGroup>,
64    combine: Option<bool>,
65    priority: Option<i32>,
66    start_include: Option<bool>,
67    end_include: Option<bool>,
68}
69
70impl PropertyTypeBuilder {
71    pub fn build(self) -> PropertyType {
72        PropertyType {
73            highlight: self.highlight,
74            combine: self.combine,
75            priority: self.priority,
76            start_include: self.start_include,
77            end_include: self.end_include,
78        }
79    }
80}
81
82impl PropertyTypeBuilder {
83    pub fn highlight(mut self, highlight: HighlightGroup) -> Self {
84        self.highlight = Some(highlight);
85        self
86    }
87    pub fn combine(mut self, combine: bool) -> Self {
88        self.combine = Some(combine);
89        self
90    }
91    pub fn priority(mut self, priority: i32) -> Self {
92        self.priority = Some(priority);
93        self
94    }
95    pub fn start_include(mut self, start_include: bool) -> Self {
96        self.start_include = Some(start_include);
97        self
98    }
99    pub fn end_include(mut self, end_include: bool) -> Self {
100        self.end_include = Some(end_include);
101        self
102    }
103}
104
105mod tests {
106    use super::*;
107
108    #[test]
109    fn test_property_type() {
110        let pt = PropertyType {
111            highlight: Some("Constant".to_string()),
112            combine: Some(true),
113            priority: Some(-10),
114            start_include: Some(true),
115            end_include: Some(true),
116        };
117        assert_eq!(pt.highlight, Some("Constant".to_string()));
118        assert_eq!(pt.combine, Some(true));
119        assert_eq!(pt.priority, Some(-10));
120        assert_eq!(pt.start_include, Some(true));
121        assert_eq!(pt.end_include, Some(true));
122    }
123    #[test]
124    fn test_property_type_empty() {
125        let pt: PropertyType = Default::default();
126        assert_eq!(pt.highlight, None);
127        assert_eq!(pt.combine, None);
128        assert_eq!(pt.priority, None);
129        assert_eq!(pt.start_include, None);
130        assert_eq!(pt.end_include, None);
131    }
132    #[test]
133    fn test_property_type_builder() {
134        let pt = PropertyTypeBuilder::default()
135            .highlight("Constant".to_string())
136            .build();
137        assert_eq!(pt.highlight, Some("Constant".to_string()));
138    }
139}