Skip to main content

photon_core/
stub_identity.rs

1//! Minimal JSON identity factory for tests, docs, and dev and test runs.
2
3use std::any::Any;
4
5use serde_json::Value;
6
7use crate::error::IdentityError;
8use crate::identity::{Actor, IdentityFactory};
9
10/// Actor label derived from publish-time `actor_json` (`System.operation` when present).
11#[derive(Debug, Clone)]
12pub struct JsonActor {
13    label: String,
14}
15
16impl Actor for JsonActor {
17    fn label(&self) -> &str {
18        &self.label
19    }
20
21    fn as_any(&self) -> &dyn Any {
22        self
23    }
24
25    fn as_any_mut(&mut self) -> &mut dyn Any {
26        self
27    }
28
29    fn into_any(self: Box<Self>) -> Box<dyn Any> {
30        self
31    }
32}
33
34/// Reconstructs [`JsonActor`] from JSON captured at publish time.
35///
36/// Sufficient for README examples, integration tests, and local Getting started walkthroughs that
37/// only need a debug label at the handler boundary. Production hosts usually wire a custom
38/// [`IdentityFactory`] that maps to real principal types.
39///
40/// # Example
41///
42/// ```rust,ignore
43/// use std::sync::Arc;
44///
45/// use photon_core::JsonIdentityFactory;
46/// use photon_runtime::Photon;
47///
48/// let photon = Photon::builder().auto_registry().build()?;
49/// photon.start_executor(Arc::new(JsonIdentityFactory))?;
50/// ```
51#[derive(Debug, Default, Clone, Copy)]
52pub struct JsonIdentityFactory;
53
54impl IdentityFactory for JsonIdentityFactory {
55    fn reconstruct(&self, actor_json: &str) -> Result<Box<dyn Actor>, IdentityError> {
56        let value: Value = serde_json::from_str(actor_json)
57            .map_err(|e| IdentityError::InvalidActor(e.to_string()))?;
58        let label = value
59            .get("System")
60            .and_then(|s| s.get("operation"))
61            .and_then(|v| v.as_str())
62            .unwrap_or("json-actor")
63            .to_string();
64        Ok(Box::new(JsonActor { label }))
65    }
66}