libimagwiki/
wiki.rs

1//
2// imag - the personal information management suite for the commandline
3// Copyright (C) 2015-2020 Matthias Beyer <mail@beyermatthias.de> and contributors
4//
5// This library is free software; you can redistribute it and/or
6// modify it under the terms of the GNU Lesser General Public
7// License as published by the Free Software Foundation; version
8// 2.1 of the License.
9//
10// This library is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13// Lesser General Public License for more details.
14//
15// You should have received a copy of the GNU Lesser General Public
16// License along with this library; if not, write to the Free Software
17// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18//
19
20use std::path::PathBuf;
21
22use libimagstore::store::Store;
23use libimagstore::store::FileLockEntry;
24use libimagstore::iter::Entries;
25use libimagentrylink::linkable::Linkable;
26
27use failure::Fallible as Result;
28use failure::Error;
29use failure::err_msg;
30use failure::ResultExt;
31
32pub struct Wiki<'a, 'b>(&'a Store, &'b str);
33
34/// An interface for accessing, creating and deleting "wiki pages"
35///
36/// Wiki pages are normal entries with some details added.
37///
38///
39/// # Details
40///
41/// Entries are automatically linked to the "index" page when created and retrieved.
42///
43impl<'a, 'b> Wiki<'a, 'b> {
44
45    pub(crate) fn new(store: &'a Store, name: &'b str) -> Wiki<'a, 'b> {
46        Wiki(store, name)
47    }
48
49    pub(crate) fn create_index_page(&self) -> Result<FileLockEntry<'a>> {
50        let path = PathBuf::from(format!("{}/index", self.1));
51        let sid  = crate::module_path::new_id(path)?;
52
53        self.0.create(sid)
54    }
55
56    pub(crate) fn get_index_page(&self) -> Result<FileLockEntry<'a>> {
57        let path = PathBuf::from(format!("{}/index", self.1));
58        let sid  = crate::module_path::new_id(path)?;
59
60        self.0
61            .get(sid)
62            .context("Cannot get ID from store")
63            .map_err(Error::from)?
64            .ok_or_else(|| err_msg("Missing index"))
65    }
66
67    pub fn get_entry<EN: AsRef<str>>(&self, entry_name: EN) -> Result<Option<FileLockEntry<'a>>> {
68        let path = PathBuf::from(format!("{}/{}", self.1, entry_name.as_ref()));
69        let sid  = crate::module_path::new_id(path)?;
70        self.0.get(sid)
71    }
72
73    pub fn create_entry<EN: AsRef<str>>(&self, entry_name: EN) -> Result<FileLockEntry<'a>> {
74        let path = PathBuf::from(format!("{}/{}", self.1, entry_name.as_ref()));
75        let sid  = crate::module_path::new_id(path)?;
76        let mut index = self
77            .get_entry("index")?
78            .ok_or_else(|| err_msg("Missing index page"))?;
79        let mut entry = self.0.create(sid)?;
80
81        entry.add_link(&mut index).map(|_| entry)
82    }
83
84    pub fn retrieve_entry<EN: AsRef<str>>(&self, entry_name: EN) -> Result<FileLockEntry<'a>> {
85        let path = PathBuf::from(format!("{}/{}", self.1, entry_name.as_ref()));
86        let sid  = crate::module_path::new_id(path)?;
87        let mut index = self
88            .get_entry("index")?
89            .ok_or_else(|| err_msg("Missing index page"))?;
90        let mut entry = self.0.retrieve(sid)?;
91
92        entry.add_link(&mut index).map(|_| entry)
93    }
94
95    pub fn all_ids(&self) -> Result<Entries<'a>> {
96        self.0.entries()?.in_collection("wiki")
97    }
98
99    pub fn delete_entry<EN: AsRef<str>>(&self, entry_name: EN) -> Result<()> {
100        let path = PathBuf::from(format!("{}/{}", self.1, entry_name.as_ref()));
101        let sid  = crate::module_path::new_id(path)?;
102        self.0.delete(sid)
103    }
104}
105