Skip to main content

co_primitives/library/
reducer_action_core.rs

1// SPDX-License-Identifier: AGPL-3.0-only
2// Copyright (C) 2026 1io BRANDGUARDIAN GmbH
3
4use crate::{from_cbor, AnyBlockStorage, MultiCodec};
5use cid::Cid;
6use serde::Deserialize;
7
8/// Read `core` property from a `ReducerAction`.
9pub fn reducer_action_core(cbor: &[u8]) -> Result<String, anyhow::Error> {
10	let core_reducer_action: CoreReducerAction = from_cbor(cbor)?;
11	Ok(core_reducer_action.core)
12}
13
14/// Read `core` property from a `ReducerAction`.
15pub async fn reducer_action_core_from_storage(
16	storage: &impl AnyBlockStorage,
17	reducer_action: Cid,
18) -> Result<String, anyhow::Error> {
19	let block = storage.get(MultiCodec::with_cbor(&reducer_action)?).await?;
20	reducer_action_core(block.data())
21}
22
23/// Only extracts the core of an reducer action.
24/// See: [`co_primitives::ReducerAction`]
25#[derive(Debug, Deserialize)]
26struct CoreReducerAction {
27	#[serde(rename = "c")]
28	core: String,
29}
30
31#[cfg(test)]
32mod tests {
33	use super::CoreReducerAction;
34	use crate::{from_cbor, to_cbor, ReducerAction};
35	use cid::Cid;
36
37	#[test]
38	fn test_core_reducer_action() {
39		let reducer_action: ReducerAction<Option<Cid>> =
40			ReducerAction { core: "test-core".into(), from: "did:test".into(), payload: None, time: 1 };
41		let reducer_action_cbor = to_cbor(&reducer_action).unwrap();
42		let core_reducer_action: CoreReducerAction = from_cbor(&reducer_action_cbor).unwrap();
43		assert_eq!(core_reducer_action.core.as_str(), "test-core");
44	}
45}