use simd_json::{borrowed, derived::ValueTryIntoArray, owned};
use crate::{
entities::issue::{data::label::Label, issue_operation::IssueOperationData},
replica::entity::operation::operation_data::get,
};
struct LabelChange {
added: Option<Vec<owned::Value>>,
removed: Option<Vec<owned::Value>>,
}
pub(crate) fn label_change(
mut value: owned::Object,
) -> Result<IssueOperationData, super::decode::Error> {
Ok({
let base: LabelChange = LabelChange {
added: get! {@option value, "added", try_into_array, super::decode::Error},
removed: get! {@option value, "removed", try_into_array, super::decode::Error},
};
IssueOperationData::LabelChange {
added: base
.added
.unwrap_or_default()
.into_iter()
.map(Label::try_from)
.collect::<Result<_, _>>()?,
removed: base
.removed
.unwrap_or_default()
.into_iter()
.map(Label::try_from)
.collect::<Result<_, _>>()?,
}
})
}
pub(crate) fn label_change_value<'a>(
added: &'a [Label],
removed: &'a [Label],
) -> borrowed::Object<'a> {
let added: Option<Vec<_>> = if added.is_empty() {
None
} else {
Some(
added
.iter()
.map(Into::<borrowed::Value<'_>>::into)
.collect(),
)
};
let removed: Option<Vec<_>> = if removed.is_empty() {
None
} else {
Some(
removed
.iter()
.map(Into::<borrowed::Value<'_>>::into)
.collect(),
)
};
let mut object = borrowed::Object::new();
unsafe {
object.insert_nocheck("added".into(), added.into());
object.insert_nocheck("removed".into(), removed.into());
}
object
}