git-bug 0.2.4

A rust library for interfacing with git-bug repositories
Documentation
// git-bug-rs - A rust library for interfacing with git-bug repositories
//
// Copyright (C) 2025 Benedikt Peetz <benedikt.peetz@b-peetz.de>
// SPDX-License-Identifier: GPL-3.0-or-later
//
// This file is part of git-bug-rs/git-gub.
//
// You should have received a copy of the License along with this program.
// If not, see <https://www.gnu.org/licenses/agpl.txt>.

use simd_json::{derived::ValueTryIntoString, owned};

use crate::{
    entities::issue::issue_operation::IssueOperationData,
    replica::entity::{id::Id, operation::operation_data::get},
};

struct SetMetadata {
    target: String,
    new_metadata: Vec<(String, String)>,
}

pub(crate) fn set_metadata(
    mut value: owned::Object,
) -> Result<IssueOperationData, super::decode::Error> {
    let base: SetMetadata = SetMetadata {
        target: get! {value, "target", try_into_string, super::decode::Error},
        new_metadata: get! {@map[preserve-order] value,
        "new_metadata", try_into_string, super::decode::Error},
    };

    Ok(IssueOperationData::SetMetadata {
        target: Id::from_hex(base.target.as_bytes())?,
        new_metadata: base.new_metadata,
    })
}

pub(crate) fn set_metadata_value<'a>(
    target: &'a Id,
    new_metadata: &'a Vec<(String, String)>,
) -> simd_json::borrowed::Object<'a> {
    let mut object = simd_json::borrowed::Object::new();

    let mut newer_metadata = simd_json::borrowed::Object::new();
    for (key, value) in new_metadata {
        // Safety:
        // Is a new object.
        unsafe {
            newer_metadata.insert_nocheck(key.into(), value.as_str().into());
        }
    }

    // Safety:
    // We just created this object. As such, it is empty.
    unsafe {
        object.insert_nocheck("target".into(), target.to_string().into());
        object.insert_nocheck("new_metadata".into(), newer_metadata.into());
    }

    object
}