vimwiki-server 0.1.0

Daemon that supports parsing and modifying vimwiki files.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
use crate::data::{
    Element, ElementQuery, FromVimwikiElement, GqlPageFilter,
    GraphqlDatabaseError, InlineElement, InlineElementQuery, Page, PageQuery,
    Region,
};
use entity::*;
use entity_async_graphql::*;
use serde::{Deserialize, Serialize};
use strum::{Display, EnumString};
use vimwiki::{self as v, Located};

/// Represents a single document list
#[gql_ent]
pub struct List {
    /// The segment of the document this list covers
    #[ent(field(graphql(filter_untyped)))]
    region: Region,

    /// The items contained in the list
    #[ent(edge(policy = "deep"))]
    items: Vec<ListItem>,

    /// Page containing this list
    #[ent(edge)]
    page: Page,

    /// Parent element to this list
    #[ent(edge(policy = "shallow", wrap, graphql(filter_untyped)))]
    parent: Option<Element>,
}

impl<'a> FromVimwikiElement<'a> for List {
    type Element = Located<v::List<'a>>;

    fn from_vimwiki_element(
        page_id: Id,
        parent_id: Option<Id>,
        element: Self::Element,
    ) -> Result<Self, GraphqlDatabaseError> {
        let region = Region::from(element.region());

        let mut ent = GraphqlDatabaseError::wrap(
            Self::build()
                .region(region)
                .items(Vec::new())
                .page(page_id)
                .parent(parent_id)
                .finish_and_commit(),
        )?;

        let mut items = Vec::new();
        for item in element.into_inner().items {
            items.push(
                ListItem::from_vimwiki_element(page_id, Some(ent.id()), item)?
                    .id(),
            );
        }

        ent.set_items_ids(items);
        ent.commit().map_err(GraphqlDatabaseError::Database)?;

        Ok(ent)
    }
}

/// Represents a single item within a list in a document
#[gql_ent]
pub struct ListItem {
    /// The segment of the document this list item covers
    #[ent(field(graphql(filter_untyped)))]
    region: Region,

    /// The type of the list item
    #[ent(field(graphql(filter_untyped)))]
    item_type: ListItemType,

    /// The suffix to a list item
    #[ent(field(graphql(filter_untyped)))]
    suffix: ListItemSuffix,

    /// The position of this list item among all items in a list
    position: i32,

    /// The contents contained within the list item
    #[ent(edge(policy = "deep", wrap, graphql(filter_untyped)))]
    contents: Vec<ListItemContent>,

    /// Additional attributes associated with the list item
    #[ent(edge(policy = "deep"))]
    attributes: ListItemAttributes,

    /// Page containing this list item
    #[ent(edge)]
    page: Page,

    /// Parent element to this list item
    #[ent(edge(policy = "shallow", wrap, graphql(filter_untyped)))]
    parent: Option<Element>,
}

impl<'a> FromVimwikiElement<'a> for ListItem {
    type Element = Located<v::ListItem<'a>>;

    fn from_vimwiki_element(
        page_id: Id,
        parent_id: Option<Id>,
        element: Self::Element,
    ) -> Result<Self, GraphqlDatabaseError> {
        let region = Region::from(element.region());
        let item = element.into_inner();

        let item_type = ListItemType::from(item.ty);
        let suffix = ListItemSuffix::from(item.suffix);
        let position = item.pos as i32;

        let mut ent = GraphqlDatabaseError::wrap(
            Self::build()
                .region(region)
                .item_type(item_type)
                .suffix(suffix)
                .position(position)
                .contents(Vec::new())
                .attributes(0)
                .page(page_id)
                .parent(parent_id)
                .finish_and_commit(),
        )?;

        let mut contents = Vec::new();
        for content in item.contents {
            contents.push(
                ListItemContent::from_vimwiki_element(
                    page_id,
                    Some(ent.id()),
                    content,
                )?
                .id(),
            );
        }

        let attributes = ListItemAttributes::from_vimwiki_element(
            page_id,
            Some(ent.id()),
            item.attributes,
        )?
        .id();

        ent.set_contents_ids(contents);
        ent.set_attributes_id(attributes);
        ent.commit().map_err(GraphqlDatabaseError::Database)?;

        Ok(ent)
    }
}

/// Represents the type of prefix used with a list item
#[derive(
    async_graphql::Enum,
    Copy,
    Clone,
    Debug,
    Eq,
    PartialEq,
    Display,
    EnumString,
    Serialize,
    Deserialize,
)]
pub enum ListItemType {
    Number,
    Pound,
    LowercaseAlphabet,
    UppercaseAlphabet,
    LowercaseRoman,
    UppercaseRoman,
    Hyphen,
    Asterisk,
    Other,
}

impl ValueLike for ListItemType {
    fn into_value(self) -> Value {
        Value::from(self.to_string())
    }

    fn try_from_value(value: Value) -> Result<Self, Value> {
        match value {
            Value::Text(x) => x.as_str().parse().map_err(|_| Value::Text(x)),
            x => Err(x),
        }
    }
}

impl<'a> From<v::ListItemType<'a>> for ListItemType {
    fn from(t: v::ListItemType<'a>) -> Self {
        match t {
            v::ListItemType::Ordered(x) => match x {
                v::OrderedListItemType::LowercaseAlphabet => {
                    Self::LowercaseAlphabet
                }
                v::OrderedListItemType::LowercaseRoman => Self::LowercaseRoman,
                v::OrderedListItemType::Number => Self::Number,
                v::OrderedListItemType::Pound => Self::Pound,
                v::OrderedListItemType::UppercaseAlphabet => {
                    Self::UppercaseAlphabet
                }
                v::OrderedListItemType::UppercaseRoman => Self::UppercaseRoman,
            },
            v::ListItemType::Unordered(x) => match x {
                v::UnorderedListItemType::Asterisk => Self::Asterisk,
                v::UnorderedListItemType::Hyphen => Self::Hyphen,
                v::UnorderedListItemType::Other(_) => Self::Other,
            },
        }
    }
}

#[derive(
    async_graphql::Enum,
    Copy,
    Clone,
    Debug,
    Eq,
    PartialEq,
    Display,
    EnumString,
    Serialize,
    Deserialize,
)]
#[graphql(remote = "vimwiki::ListItemSuffix")]
#[strum(serialize_all = "snake_case")]
pub enum ListItemSuffix {
    None,
    Period,
    Paren,
}

impl ValueLike for ListItemSuffix {
    fn into_value(self) -> Value {
        Value::from(self.to_string())
    }

    fn try_from_value(value: Value) -> Result<Self, Value> {
        match value {
            Value::Text(x) => x.as_str().parse().map_err(|_| Value::Text(x)),
            x => Err(x),
        }
    }
}

#[gql_ent]
pub enum ListItemContent {
    InlineContent(InlineContent),
    List(List),
}

impl ListItemContent {
    pub fn page_id(&self) -> Id {
        match self {
            Self::InlineContent(x) => x.page_id(),
            Self::List(x) => x.page_id(),
        }
    }

    pub fn parent_id(&self) -> Option<Id> {
        match self {
            Self::InlineContent(x) => x.parent_id(),
            Self::List(x) => x.parent_id(),
        }
    }
}

impl<'a> FromVimwikiElement<'a> for ListItemContent {
    type Element = Located<v::ListItemContent<'a>>;

    fn from_vimwiki_element(
        page_id: Id,
        parent_id: Option<Id>,
        element: Self::Element,
    ) -> Result<Self, GraphqlDatabaseError> {
        let region = element.region();
        Ok(match element.into_inner() {
            v::ListItemContent::InlineContent(x) => {
                Self::InlineContent(InlineContent::from_vimwiki_element(
                    page_id,
                    parent_id,
                    Located::new(x, region),
                )?)
            }
            v::ListItemContent::List(x) => {
                Self::List(List::from_vimwiki_element(
                    page_id,
                    parent_id,
                    Located::new(x, region),
                )?)
            }
        })
    }
}

#[gql_ent]
pub struct InlineContent {
    #[ent(edge(policy = "deep", wrap, graphql(filter_untyped)))]
    contents: Vec<InlineElement>,

    /// Page containing this inline content
    #[ent(edge)]
    page: Page,

    /// Parent element to this inline content
    #[ent(edge(policy = "shallow", wrap, graphql(filter_untyped)))]
    parent: Option<Element>,
}

impl<'a> FromVimwikiElement<'a> for InlineContent {
    type Element = Located<v::InlineElementContainer<'a>>;

    fn from_vimwiki_element(
        page_id: Id,
        parent_id: Option<Id>,
        element: Self::Element,
    ) -> Result<Self, GraphqlDatabaseError> {
        let mut ent = GraphqlDatabaseError::wrap(
            Self::build()
                .contents(Vec::new())
                .page(page_id)
                .parent(parent_id)
                .finish_and_commit(),
        )?;

        let mut contents = Vec::new();
        for content in element.into_inner() {
            contents.push(
                InlineElement::from_vimwiki_element(
                    page_id,
                    Some(ent.id()),
                    content,
                )?
                .id(),
            );
        }

        ent.set_contents_ids(contents);
        ent.commit().map_err(GraphqlDatabaseError::Database)?;

        Ok(ent)
    }
}

#[gql_ent]
pub struct ListItemAttributes {
    #[ent(field(graphql(filter_untyped)))]
    todo_status: Option<ListItemTodoStatus>,

    /// Page containing this list item attribute set
    #[ent(edge)]
    page: Page,

    /// Parent element to this list item attribute set
    #[ent(edge(policy = "shallow", wrap, graphql(filter_untyped)))]
    parent: Option<Element>,
}

impl<'a> FromVimwikiElement<'a> for ListItemAttributes {
    type Element = v::ListItemAttributes;

    fn from_vimwiki_element(
        page_id: Id,
        parent_id: Option<Id>,
        element: Self::Element,
    ) -> Result<Self, GraphqlDatabaseError> {
        let todo_status = element.todo_status.map(ListItemTodoStatus::from);

        GraphqlDatabaseError::wrap(
            Self::build()
                .todo_status(todo_status)
                .page(page_id)
                .parent(parent_id)
                .finish_and_commit(),
        )
    }
}

#[derive(
    async_graphql::Enum,
    Copy,
    Clone,
    Debug,
    Eq,
    PartialEq,
    Display,
    EnumString,
    Serialize,
    Deserialize,
)]
#[graphql(remote = "vimwiki::ListItemTodoStatus")]
#[strum(serialize_all = "snake_case")]
pub enum ListItemTodoStatus {
    /// Flags list item as a TODO item that has not been completed
    Incomplete,

    /// Flags list item as a TODO item that is partially complete (1-33%)
    PartiallyComplete1,

    /// Flags list item as a TODO item that is partially complete (34-66%)
    PartiallyComplete2,

    /// Flags list item as a TODO item that is partially complete (67-99%)
    PartiallyComplete3,

    /// Flags list item as a TODO item that is complete
    Complete,

    /// Flags list item as a TODO item that has been rejected
    Rejected,
}

impl ValueLike for ListItemTodoStatus {
    fn into_value(self) -> Value {
        Value::from(self.to_string())
    }

    fn try_from_value(value: Value) -> Result<Self, Value> {
        match value {
            Value::Text(x) => x.as_str().parse().map_err(|_| Value::Text(x)),
            x => Err(x),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use entity_inmemory::InmemoryDatabase;
    use vimwiki::macros::*;

    #[test]
    fn should_fully_populate_from_vimwiki_element() {
        global::with_db(InmemoryDatabase::default(), || {
            let element = vimwiki_list! {r#"
            - item 1
            - item 2
                - sub item 1
                - sub item 2
            - [ ] item 3
            "#};
            let region = Region::from(element.region());

            let ent = List::from_vimwiki_element(999, Some(123), element)
                .expect("Failed to convert from element");
            assert_eq!(ent.region(), &region);
            assert_eq!(ent.page_id(), 999);
            assert_eq!(ent.parent_id(), Some(123));

            for item in ent.load_items().expect("Failed to load items") {
                assert_eq!(item.page_id(), 999);
                assert_eq!(item.parent_id(), Some(ent.id()));
            }

            // TODO: Validate rest of content for proper edge trickling and
            //       other info like fields
        });
    }
}