From 029a3c448ed02048d479c20a92d9b6ece4550615 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 17 Oct 2018 14:43:21 +0200 Subject: [PATCH] Change WikiStore::create_wiki() interface to also return the index page. This way a user of the library can use the index page entry right away. Signed-off-by: Matthias Beyer --- lib/domain/libimagwiki/src/store.rs | 18 +++++++++++------- lib/domain/libimagwiki/src/wiki.rs | 11 +++++++++++ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/lib/domain/libimagwiki/src/store.rs b/lib/domain/libimagwiki/src/store.rs index 89b7093b..5ee5bd32 100644 --- a/lib/domain/libimagwiki/src/store.rs +++ b/lib/domain/libimagwiki/src/store.rs @@ -17,6 +17,7 @@ // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // +use libimagstore::store::FileLockEntry; use libimagstore::store::Store; use libimagstore::storeid::StoreId; use libimagstore::storeid::IntoStoreId; @@ -30,10 +31,10 @@ pub trait WikiStore { fn get_wiki<'a, 'b>(&'a self, name: &'b str) -> Result>>; fn create_wiki<'a, 'b>(&'a self, name: &'b str) - -> Result>; + -> Result<(Wiki<'a, 'b>, FileLockEntry<'a>)>; fn retrieve_wiki<'a, 'b>(&'a self, name: &'b str) - -> Result>; + -> Result<(Wiki<'a, 'b>, FileLockEntry<'a>)>; } @@ -59,20 +60,23 @@ impl WikiStore for Store { /// Ob success, an empty Wiki entry with the name `index` is created inside the wiki. Later, new /// entries are automatically linked to this entry. /// - fn create_wiki<'a, 'b>(&'a self, name: &'b str) -> Result> { + fn create_wiki<'a, 'b>(&'a self, name: &'b str) -> Result<(Wiki<'a, 'b>, FileLockEntry<'a>)> { debug!("Trying to get wiki '{}'", name); let wiki = Wiki::new(self, name); - let _ = wiki.create_index_page()?; - Ok(wiki) + let index = wiki.create_index_page()?; + Ok((wiki, index)) } fn retrieve_wiki<'a, 'b>(&'a self, name: &'b str) - -> Result> + -> Result<(Wiki<'a, 'b>, FileLockEntry<'a>)> { match self.get_wiki(name)? { None => self.create_wiki(name), - Some(wiki) => Ok(wiki), + Some(wiki) => { + let index = wiki.get_index_page()?; + Ok((wiki, index)) + }, } } diff --git a/lib/domain/libimagwiki/src/wiki.rs b/lib/domain/libimagwiki/src/wiki.rs index c3888710..5e87c150 100644 --- a/lib/domain/libimagwiki/src/wiki.rs +++ b/lib/domain/libimagwiki/src/wiki.rs @@ -30,6 +30,7 @@ use libimagstore::storeid::StoreIdIteratorWithStore; use libimagentrylink::internal::InternalLinker; use failure::Fallible as Result; +use failure::Error; use failure::err_msg; pub struct Wiki<'a, 'b>(&'a Store, &'b str); @@ -56,6 +57,16 @@ impl<'a, 'b> Wiki<'a, 'b> { self.0.create(sid) } + pub(crate) fn get_index_page(&self) -> Result> { + let path = PathBuf::from(format!("{}/index", self.1)); + let sid = ::module_path::ModuleEntryPath::new(path).into_storeid()?; + + self.0 + .get(sid) + .map_err(Error::from)? + .ok_or_else(|| Error::from(err_msg("Missing index"))) + } + pub fn get_entry>(&self, entry_name: EN) -> Result>> { let path = PathBuf::from(format!("{}/{}", self.1, entry_name.as_ref())); let sid = ::module_path::ModuleEntryPath::new(path).into_storeid()?;