use serde::{Deserialize, Serialize};
use crate::Principal;
mod federated;
pub use federated::{FederatedAttributes, FederatedPeerClass, RemoteClaim};
pub const SCOPE_FEDERATED_CAPABILITY: &str = "scope-federated/v1";
#[derive(Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)]
enum ScopePrincipal {
Reserved { module_id: String },
Direct {},
Unverified {},
}
impl From<ScopePrincipal> for Principal {
fn from(value: ScopePrincipal) -> Self {
match value {
ScopePrincipal::Reserved { module_id } => Self::Reserved { module_id },
ScopePrincipal::Direct {} => Self::Direct,
ScopePrincipal::Unverified {} => Self::Unverified,
}
}
}
fn deserialize_scope_principal<'de, D: serde::Deserializer<'de>>(
deserializer: D,
) -> Result<Principal, D::Error> {
ScopePrincipal::deserialize(deserializer).map(Into::into)
}
fn deserialize_scope_principals<'de, D: serde::Deserializer<'de>>(
deserializer: D,
) -> Result<Vec<Principal>, D::Error> {
Vec::<ScopePrincipal>::deserialize(deserializer)
.map(|principals| principals.into_iter().map(Into::into).collect())
}
pub fn validate_bind_proof_sha256(hash: &str) -> Result<(), &'static str> {
if hash.len() == 64
&& hash
.bytes()
.all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))
{
Ok(())
} else {
Err("bind_proof_sha256 must be exactly 64 lowercase hexadecimal characters")
}
}
fn deserialize_bind_proof_sha256<'de, D: serde::Deserializer<'de>>(
deserializer: D,
) -> Result<Option<String>, D::Error> {
let hash = Option::<String>::deserialize(deserializer)?;
if let Some(hash) = &hash {
validate_bind_proof_sha256(hash).map_err(serde::de::Error::custom)?;
}
Ok(hash)
}
pub const CAP_SCOPES_V1: &str = "scopes/v1";
pub const CAP_ROUTE_ROLE_VERSIONS_V1: &str = "route-role-versions/v1";
pub const BIND_NO_PROJECT_CAPABILITY: &str = "bind-no-project/v1";
pub const SCOPE_SYNC_OP: &str = "scope.sync";
pub const SCOPE_APPLY_OP: &str = "scope.apply";
pub const SCOPE_DESCRIBE_OP: &str = "scope.describe";
pub const MAX_LIVE_SCOPES_PER_OWNER: usize = 10_000;
pub const MAX_SCOPE_ATTRIBUTE_BYTES: usize = 4 * 1024;
pub const MAX_SCOPE_TOMBSTONES_PER_OWNER: usize = 1_000;
pub const MAX_CARRIER_TARGETS: usize = 16;
pub const MAX_SCOPE_EXPIRY_AHEAD_MS: u64 = 86_400_000;
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum ScopeKind {
Head,
Worker,
Ephemeral,
Federated,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct ScopeParent {
#[serde(deserialize_with = "deserialize_scope_principal")]
pub owner: Principal,
#[serde(rename = "ref")]
pub scope_ref: String,
pub scope_epoch: u64,
}
impl ScopeParent {
pub fn new(owner: Principal, scope_ref: impl Into<String>, scope_epoch: u64) -> Self {
Self {
owner,
scope_ref: scope_ref.into(),
scope_epoch,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct ScopeCarrier {
#[serde(deserialize_with = "deserialize_scope_principal")]
pub principal: Principal,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub targets: Option<Vec<String>>,
}
impl ScopeCarrier {
pub fn new(principal: Principal) -> Self {
Self {
principal,
targets: None,
}
}
#[must_use]
pub fn with_targets(mut self, targets: Option<Vec<String>>) -> Self {
self.targets = targets;
self
}
}
pub const FLOW_SCOPES_CAPABILITY: &str = "flow-scopes/v1";
pub const FLOW_IDENTITY_SCOPES_CAPABILITY: &str = "flow-identity-scopes/v1";
pub const AGENT_RUN_SCOPES_CAPABILITY: &str = "agent-run-scopes/v1";
pub const SCOPE_ON_BEHALF_OF_CAPABILITY: &str = "scope-on-behalf-of/v1";
pub const WORKER_RUNS_CAPABILITY: &str = "worker-runs/v1";
pub const SCOPE_SERVED_BY_CAPABILITY: &str = "scope-served-by/v1";
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct ScopeAttributes {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub federated: Option<FederatedAttributes>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub agent_id: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub on_behalf_of_agent: Option<String>,
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub delegates: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub flow_id: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub flow_version: Option<u64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub flow_code_hash: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub flow_install_id: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub run_id: Option<String>,
}
impl ScopeAttributes {
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn with_federated(mut self, federated: Option<FederatedAttributes>) -> Self {
self.federated = federated;
self
}
#[must_use]
pub fn with_agent_id(mut self, agent_id: Option<String>) -> Self {
self.agent_id = agent_id;
self
}
#[must_use]
pub fn with_on_behalf_of_agent(mut self, on_behalf_of_agent: Option<String>) -> Self {
self.on_behalf_of_agent = on_behalf_of_agent;
self
}
#[must_use]
pub fn with_delegates(mut self, delegates: bool) -> Self {
self.delegates = delegates;
self
}
#[must_use]
pub fn with_flow_id(mut self, flow_id: Option<String>) -> Self {
self.flow_id = flow_id;
self
}
#[must_use]
pub fn with_flow_version(mut self, flow_version: Option<u64>) -> Self {
self.flow_version = flow_version;
self
}
#[must_use]
pub fn with_flow_code_hash(mut self, flow_code_hash: Option<String>) -> Self {
self.flow_code_hash = flow_code_hash;
self
}
#[must_use]
pub fn with_flow_install_id(mut self, flow_install_id: Option<String>) -> Self {
self.flow_install_id = flow_install_id;
self
}
#[must_use]
pub fn with_run_id(mut self, run_id: Option<String>) -> Self {
self.run_id = run_id;
self
}
pub fn is_empty(&self) -> bool {
self.federated.is_none()
&& self.agent_id.is_none()
&& self.on_behalf_of_agent.is_none()
&& !self.delegates
&& self.flow_id.is_none()
&& self.flow_version.is_none()
&& self.flow_code_hash.is_none()
&& self.flow_install_id.is_none()
&& self.run_id.is_none()
}
}
pub fn validate_flow_id(flow_id: &str) -> Result<(), crate::tool_call::OpaqueFieldError> {
crate::tool_call::validate_opaque_field("flow_id", flow_id)
}
pub fn validate_flow_version(flow_version: u64) -> Result<(), &'static str> {
if !(1..=9_007_199_254_740_991).contains(&flow_version) {
return Err("flow_version must be from 1 to 9007199254740991");
}
Ok(())
}
pub fn validate_flow_code_hash(flow_code_hash: &str) -> Result<(), &'static str> {
if flow_code_hash.len() != 64
|| !flow_code_hash
.bytes()
.all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte))
{
return Err("flow_code_hash must contain exactly 64 lowercase hex characters");
}
Ok(())
}
pub fn validate_flow_install_id(
flow_install_id: &str,
) -> Result<(), crate::tool_call::OpaqueFieldError> {
crate::tool_call::validate_opaque_field("flow_install_id", flow_install_id)
}
pub fn validate_run_id(run_id: &str) -> Result<(), crate::tool_call::OpaqueFieldError> {
crate::tool_call::validate_opaque_field("run_id", run_id)
}
pub fn validate_on_behalf_of_agent(agent: &str) -> Result<(), crate::tool_call::OpaqueFieldError> {
crate::tool_call::validate_opaque_field("on_behalf_of_agent", agent)
}
pub const SCOPE_WORKDIR_CAPABILITY: &str = "scope-workdir/v1";
pub fn validate_project_id(value: &str) -> Result<(), &'static str> {
if value.is_empty() || value.len() > 128 {
return Err("project_id must contain 1 to 128 bytes");
}
if !value.bytes().all(|byte| (0x21..=0x7e).contains(&byte)) {
return Err("project_id must be printable ASCII without whitespace");
}
Ok(())
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct ScopeRecord {
#[serde(rename = "ref")]
pub scope_ref: String,
pub scope_epoch: u64,
pub kind: ScopeKind,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub workdir: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub project_id: Option<String>,
#[serde(
default,
skip_serializing_if = "Option::is_none",
deserialize_with = "deserialize_bind_proof_sha256"
)]
pub bind_proof_sha256: Option<String>,
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub replace_only_if_idle: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub expires_at_ms: Option<u64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub parent: Option<ScopeParent>,
#[serde(
default,
skip_serializing_if = "Vec::is_empty",
deserialize_with = "deserialize_scope_principals"
)]
pub child_owners: Vec<Principal>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub carriers: Vec<ScopeCarrier>,
#[serde(default, skip_serializing_if = "ScopeAttributes::is_empty")]
pub attributes: ScopeAttributes,
}
impl ScopeRecord {
pub fn new(scope_ref: impl Into<String>, scope_epoch: u64, kind: ScopeKind) -> Self {
Self {
scope_ref: scope_ref.into(),
scope_epoch,
kind,
workdir: None,
project_id: None,
bind_proof_sha256: None,
replace_only_if_idle: false,
expires_at_ms: None,
parent: None,
child_owners: Vec::new(),
carriers: Vec::new(),
attributes: ScopeAttributes::default(),
}
}
#[must_use]
pub fn with_workdir(mut self, workdir: Option<String>) -> Self {
self.workdir = workdir;
self
}
#[must_use]
pub fn with_project_id(mut self, project_id: Option<String>) -> Self {
self.project_id = project_id;
self
}
#[must_use]
pub fn with_bind_proof_sha256(mut self, hash: Option<String>) -> Self {
self.bind_proof_sha256 = hash;
self
}
#[must_use]
pub fn with_replace_only_if_idle(mut self, replace_only_if_idle: bool) -> Self {
self.replace_only_if_idle = replace_only_if_idle;
self
}
#[must_use]
pub fn with_expires_at_ms(mut self, expires_at_ms: Option<u64>) -> Self {
self.expires_at_ms = expires_at_ms;
self
}
#[must_use]
pub fn with_parent(mut self, parent: Option<ScopeParent>) -> Self {
self.parent = parent;
self
}
#[must_use]
pub fn with_child_owners(mut self, child_owners: Vec<Principal>) -> Self {
self.child_owners = child_owners;
self
}
#[must_use]
pub fn with_carriers(mut self, carriers: Vec<ScopeCarrier>) -> Self {
self.carriers = carriers;
self
}
#[must_use]
pub fn with_attributes(mut self, attributes: ScopeAttributes) -> Self {
self.attributes = attributes;
self
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct ScopeEnd {
#[serde(rename = "ref")]
pub scope_ref: String,
pub scope_epoch: u64,
}
impl ScopeEnd {
pub fn new(scope_ref: impl Into<String>, scope_epoch: u64) -> Self {
Self {
scope_ref: scope_ref.into(),
scope_epoch,
}
}
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum ScopeEndOutcome {
Ended,
NotLive,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct ScopeEndResult {
#[serde(rename = "ref")]
pub scope_ref: String,
pub scope_epoch: u64,
pub outcome: ScopeEndOutcome,
}
impl ScopeEndResult {
pub fn new(scope_ref: impl Into<String>, scope_epoch: u64, outcome: ScopeEndOutcome) -> Self {
Self {
scope_ref: scope_ref.into(),
scope_epoch,
outcome,
}
}
}
#[derive(Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct ScopeSelector {
#[serde(deserialize_with = "deserialize_scope_principal")]
pub owner: Principal,
#[serde(rename = "ref")]
pub scope_ref: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub scope_epoch: Option<u64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub bind_proof: Option<String>,
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub hold_presence: bool,
}
impl ScopeSelector {
pub fn new(owner: Principal, scope_ref: impl Into<String>, scope_epoch: Option<u64>) -> Self {
Self {
owner,
scope_ref: scope_ref.into(),
scope_epoch,
bind_proof: None,
hold_presence: false,
}
}
#[must_use]
pub fn with_bind_proof(mut self, proof: Option<String>) -> Self {
self.bind_proof = proof;
self
}
#[must_use]
pub fn with_hold_presence(mut self, hold_presence: bool) -> Self {
self.hold_presence = hold_presence;
self
}
}
impl std::fmt::Debug for ScopeSelector {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ScopeSelector")
.field("owner", &self.owner)
.field("scope_ref", &self.scope_ref)
.field("scope_epoch", &self.scope_epoch)
.field(
"bind_proof",
&self.bind_proof.as_ref().map(|_| "<redacted>"),
)
.finish()
}
}
impl std::fmt::Display for ScopeSelector {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Debug::fmt(self, f)
}
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[serde(rename_all = "snake_case")]
pub enum ParentState {
Linked,
Pending,
Ended,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum ScopeRecordOutcome {
Created,
Replaced,
Updated,
Unchanged,
Refused,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct ScopeRecordResult {
#[serde(rename = "ref")]
pub scope_ref: String,
pub scope_epoch: u64,
pub outcome: ScopeRecordOutcome,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub code: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub message: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub version: Option<u64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub parent_state: Option<ParentState>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct ScopeEnded {
#[serde(rename = "ref")]
pub scope_ref: String,
pub scope_epoch: u64,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum ScopeStatus {
Live,
Ended,
NotLive,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[non_exhaustive]
pub struct ScopeStamp {
pub owner: Principal,
#[serde(rename = "ref")]
pub scope_ref: String,
pub scope_epoch: u64,
pub kind: ScopeKind,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub workdir: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub project_id: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub parent: Option<ScopeParent>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub parent_state: Option<ParentState>,
#[serde(default, skip_serializing_if = "ScopeAttributes::is_empty")]
pub attributes: ScopeAttributes,
pub owner_authorized: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub expires_at_ms: Option<u64>,
}
impl ScopeStamp {
pub fn new(
owner: Principal,
scope_ref: impl Into<String>,
scope_epoch: u64,
kind: ScopeKind,
) -> Self {
Self {
owner,
scope_ref: scope_ref.into(),
scope_epoch,
kind,
workdir: None,
project_id: None,
parent: None,
parent_state: None,
attributes: ScopeAttributes::new(),
owner_authorized: false,
expires_at_ms: None,
}
}
#[must_use]
pub fn with_workdir(mut self, workdir: Option<String>) -> Self {
self.workdir = workdir;
self
}
#[must_use]
pub fn with_project_id(mut self, project_id: Option<String>) -> Self {
self.project_id = project_id;
self
}
#[must_use]
pub fn with_parent(mut self, parent: Option<ScopeParent>) -> Self {
self.parent = parent;
self
}
#[must_use]
pub fn with_parent_state(mut self, parent_state: Option<ParentState>) -> Self {
self.parent_state = parent_state;
self
}
#[must_use]
pub fn with_attributes(mut self, attributes: ScopeAttributes) -> Self {
self.attributes = attributes;
self
}
#[must_use]
pub fn with_owner_authorized(mut self, owner_authorized: bool) -> Self {
self.owner_authorized = owner_authorized;
self
}
#[must_use]
pub fn with_expires_at_ms(mut self, expires_at_ms: Option<u64>) -> Self {
self.expires_at_ms = expires_at_ms;
self
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::tool_call::OpaqueFieldError;
#[test]
fn project_id_is_an_opaque_non_space_ascii_token_of_at_most_128_bytes() {
for valid in ["p", "!~Project:7/path", &"p".repeat(128)] {
assert_eq!(validate_project_id(valid), Ok(()));
}
for invalid in ["", "p p", "p\t", "pƩ", "p\u{7f}", &"p".repeat(129)] {
assert!(validate_project_id(invalid).is_err(), "{invalid:?}");
}
}
#[test]
fn scope_project_fields_round_trip_and_absence_keeps_exact_bytes() {
let record = ScopeRecord::new("session", 1, ScopeKind::Head);
assert_eq!(
serde_json::to_string(&record).unwrap(),
r#"{"ref":"session","scope_epoch":1,"kind":"head"}"#
);
let record = record
.with_workdir(Some("/folder".into()))
.with_project_id(Some("project:1".into()));
let encoded = serde_json::to_string(&record).unwrap();
assert_eq!(
serde_json::from_str::<ScopeRecord>(&encoded).unwrap(),
record
);
let stamp = ScopeStamp::new(Principal::Direct, "session", 1, ScopeKind::Head);
assert_eq!(
serde_json::to_string(&stamp).unwrap(),
r#"{"owner":{"kind":"direct"},"ref":"session","scope_epoch":1,"kind":"head","owner_authorized":false}"#
);
let stamp = stamp
.with_workdir(record.workdir)
.with_project_id(record.project_id);
let encoded = serde_json::to_string(&stamp).unwrap();
assert_eq!(serde_json::from_str::<ScopeStamp>(&encoded).unwrap(), stamp);
}
#[test]
fn flow_id_uses_the_shared_opaque_token_bounds_and_names_its_field() {
let field = "flow_id";
assert_eq!(validate_flow_id(""), Err(OpaqueFieldError::Empty { field }));
assert_eq!(validate_flow_id("f"), Ok(()));
assert_eq!(validate_flow_id(&"f".repeat(256)), Ok(()));
assert_eq!(
validate_flow_id(&"f".repeat(257)),
Err(OpaqueFieldError::TooLong { field, length: 257 })
);
assert_eq!(validate_flow_id("!~Flow:7/step"), Ok(()));
for bad in ["f Ʃ", "f\t", "fƩ", "f\u{7f}"] {
let error = validate_flow_id(bad).unwrap_err();
assert_eq!(
error,
OpaqueFieldError::InvalidCharacter { field, index: 1 }
);
assert_eq!(error.field(), "flow_id");
}
}
#[test]
fn flow_identity_validators_enforce_boundaries_and_name_fields() {
for valid in [1, 9_007_199_254_740_991] {
assert_eq!(validate_flow_version(valid), Ok(()));
}
for invalid in [0, 9_007_199_254_740_992, u64::MAX] {
assert!(validate_flow_version(invalid)
.unwrap_err()
.contains("flow_version"));
}
assert_eq!(
validate_flow_code_hash(&"0123456789abcdef".repeat(4)),
Ok(())
);
for invalid in [
"a".repeat(63),
"a".repeat(65),
"A".repeat(64),
"g".repeat(64),
] {
assert!(validate_flow_code_hash(&invalid)
.unwrap_err()
.contains("flow_code_hash"));
}
for valid in ["i", "!~Install:7/path", &"i".repeat(256)] {
assert_eq!(validate_flow_install_id(valid), Ok(()));
}
for invalid in ["", "i i", "i\t", "iƩ", "i\u{7f}", &"i".repeat(257)] {
assert_eq!(
validate_flow_install_id(invalid).unwrap_err().field(),
"flow_install_id"
);
}
}
#[test]
fn flow_identity_round_trips_and_legacy_stamp_keeps_exact_bytes() {
let attributes = ScopeAttributes::new().with_flow_id(Some("flow:7".into()));
let stamp = ScopeStamp::new(Principal::Direct, "session", 1, ScopeKind::Head)
.with_attributes(attributes.clone());
assert_eq!(
serde_json::to_string(&stamp).unwrap(),
r#"{"owner":{"kind":"direct"},"ref":"session","scope_epoch":1,"kind":"head","attributes":{"flow_id":"flow:7"},"owner_authorized":false}"#
);
let attributes = attributes
.with_flow_version(Some(9_007_199_254_740_991))
.with_flow_code_hash(Some("a".repeat(64)))
.with_flow_install_id(Some("install:7".into()));
let stamp = stamp.with_attributes(attributes.clone());
let encoded = serde_json::to_string(&stamp).unwrap();
assert_eq!(serde_json::from_str::<ScopeStamp>(&encoded).unwrap(), stamp);
assert_eq!(
serde_json::from_str::<ScopeAttributes>(&serde_json::to_string(&attributes).unwrap())
.unwrap(),
attributes
);
assert!(
serde_json::from_str::<ScopeAttributes>(r#"{"flow_version":1,"unknown":true}"#)
.is_err()
);
assert!(!ScopeAttributes::new().with_flow_version(Some(1)).is_empty());
assert!(!ScopeAttributes::new()
.with_flow_code_hash(Some("a".repeat(64)))
.is_empty());
assert!(!ScopeAttributes::new()
.with_flow_install_id(Some("i".into()))
.is_empty());
}
#[test]
fn flow_only_attributes_round_trip_and_absence_keeps_the_bytes() {
let attributes = ScopeAttributes::default();
assert!(attributes.is_empty());
assert_eq!(serde_json::to_string(&attributes).unwrap(), "{}");
assert_eq!(
serde_json::from_str::<ScopeAttributes>("{}").unwrap(),
attributes
);
let attributes = ScopeAttributes {
flow_id: Some("flow:7".to_string()),
..ScopeAttributes::default()
};
assert!(!attributes.is_empty());
let encoded = serde_json::to_string(&attributes).unwrap();
assert_eq!(encoded, r#"{"flow_id":"flow:7"}"#);
assert_eq!(
serde_json::from_str::<ScopeAttributes>(&encoded).unwrap(),
attributes
);
assert!(
serde_json::from_str::<ScopeAttributes>(r#"{"flow_id":"flow:7","unknown":true}"#)
.is_err()
);
}
}