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::{ValueTryIntoArray, ValueTryIntoString},
    owned,
};

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

struct AddComment {
    message: String,
    files: Option<Vec<owned::Value>>,
}

pub(crate) fn add_comment(
    mut value: owned::Object,
) -> Result<IssueOperationData, super::decode::Error> {
    let base: AddComment = AddComment {
        message: get! {value, "message", try_into_string, super::decode::Error},
        files: get! {@option value, "files", try_into_array, super::decode::Error},
    };

    Ok(IssueOperationData::AddComment {
        message: base.message,
        files: {
            base.files
                .unwrap_or_default()
                .into_iter()
                .map(|file_id| File::try_from(&file_id))
                .collect::<Result<_, _>>()?
        },
    })
}

pub(crate) fn add_comment_value<'a>(
    message: &'a str,
    files: &'a [File],
) -> simd_json::borrowed::Object<'a> {
    let files: Option<Vec<String>> = if files.is_empty() {
        None
    } else {
        Some(files.iter().map(File::to_string).collect())
    };

    let mut object = simd_json::borrowed::Object::new();

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

    object
}