Skip to content

Commit

Permalink
api: add ContextBuilder.build() to build Context without opening
Browse files Browse the repository at this point in the history
  • Loading branch information
link2xt committed Mar 2, 2024
1 parent c3d9681 commit ceadd89
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,19 @@ impl ContextBuilder {
self
}

/// Opens the [`Context`].
pub async fn open(self) -> Result<Context> {
/// Builds the [`Context`] without opening it.
pub async fn build(self) -> Result<Context> {
let context =
Context::new_closed(&self.dbfile, self.id, self.events, self.stock_strings).await?;
let password = self.password.unwrap_or_default();
Ok(context)
}

/// Builds the [`Context`] and opens it.
///
/// Returns error if context cannot be opened with the given passphrase.
pub async fn open(self) -> Result<Context> {
let password = self.password.clone().unwrap_or_default();
let context = self.build().await?;
match context.open(password).await? {
true => Ok(context),
false => bail!("database could not be decrypted, incorrect or missing password"),
Expand Down

0 comments on commit ceadd89

Please sign in to comment.