Skip to content

Commit

Permalink
refactor: use try_into_blob()
Browse files Browse the repository at this point in the history
  • Loading branch information
jpgrayson committed Oct 29, 2023
1 parent 005fbeb commit a2a50bc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 31 deletions.
4 changes: 2 additions & 2 deletions src/stack/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ impl<'repo> StackState<'repo> {
let mut tree = tree;
let stack_json = tree.peel_to_entry_by_path("stack.json")?;
if let Some(stack_json) = stack_json {
let stack_json_blob = stack_json.object()?.peel_to_kind(gix::objs::Kind::Blob)?;
let raw_state = RawStackState::from_stack_json(&stack_json_blob.data)?;
let stack_json_data = stack_json.object()?.try_into_blob()?.take_data();
let raw_state = RawStackState::from_stack_json(&stack_json_data)?;
Self::from_raw_state(repo, raw_state)
} else {
Err(anyhow!("stack metadata not found"))
Expand Down
44 changes: 15 additions & 29 deletions src/stack/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::{
};

use anyhow::{anyhow, Context, Result};
use bstr::ByteSlice;
use bstr::{ByteSlice, ByteVec};

use super::serde::{RawPatchState, RawStackState};
use crate::{ext::RepositoryExtended, patch::PatchName, stack::state::StackState};
Expand Down Expand Up @@ -57,21 +57,14 @@ fn get_format_version(repo: &gix::Repository, branch_name: &str) -> Result<i64>
.tree()
.context("finding version 4 state tree")?;
if let Some(meta_entry) = state_tree.peel_to_entry_by_path("meta")? {
let meta_blob = meta_entry
let meta_content = meta_entry
.object()
.context("finding old stack `meta` blob")
.and_then(|obj| {
if matches!(obj.kind, gix::objs::Kind::Blob) {
Ok(obj)
} else {
Err(anyhow!(
"`meta` object `{}` in stack state is not a blob",
obj.id
))
}
})?;
let meta_content =
std::str::from_utf8(&meta_blob.data).context("decoding version 4 meta")?;
.context("finding old stack `meta` blob")?
.try_into_blob()
.context("interpret `meta` object `{meta_obj.id}` as blob")?
.take_data()
.into_string()
.context("decoding version 4 meta")?;
let mut meta_lines = meta_content.lines();
let first_line = meta_lines.next();
if first_line == Some("Version: 4") {
Expand Down Expand Up @@ -104,21 +97,14 @@ fn stack_upgrade_from_4(repo: &gix::Repository, branch_name: &str) -> Result<()>
.tree()
.context("finding version 4 state tree")?;
if let Some(meta_entry) = state_tree.peel_to_entry_by_path("meta")? {
let meta_blob = meta_entry
let meta_content = meta_entry
.object()
.context("finding old stack `meta` blob")
.and_then(|obj| {
if matches!(obj.kind, gix::objs::Kind::Blob) {
Ok(obj)
} else {
Err(anyhow!(
"`meta` object `{}` in stack state is not a blob",
obj.id
))
}
})?;
let meta_content =
std::str::from_utf8(&meta_blob.data).context("decoding version 4 meta")?;
.context("finding old stack `meta` blob")?
.try_into_blob()
.context("interpret `meta` object `{meta_obj.id}` as blob")?
.take_data()
.into_string()
.context("decoding version 4 meta")?;
let mut meta_lines = meta_content.lines();
let first_line = meta_lines.next();
if first_line != Some("Version: 4") {
Expand Down

0 comments on commit a2a50bc

Please sign in to comment.