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 std::str::FromStr;

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

use crate::{
    entities::identity::identity_operation::IdentityOperationData,
    replica::entity::operation::operation_data::get,
};

struct SetAvatarUrl {
    url: String,
}

pub(crate) fn set_avatar_url(
    mut value: owned::Object,
) -> Result<IdentityOperationData, super::decode::Error> {
    let base: SetAvatarUrl = SetAvatarUrl {
        url: get! {value, "url", try_into_string, super::decode::Error},
    };

    Ok(IdentityOperationData::SetAvatarUrl {
        url: Url::from_str(&base.url)?,
    })
}

pub(crate) fn set_avatar_url_value(url: &Url) -> simd_json::borrowed::Object<'_> {
    let mut object = simd_json::borrowed::Object::new();

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

    object
}