harmos_protocol/schema/
mod.rs1mod callers;
15mod graph;
16mod identity;
17#[cfg(test)]
18mod tests_lanes;
19
20pub use callers::{method_callers, with_method_callers};
23pub use graph::{merge_message_types, message_types, operation_types};
24pub use identity::{Executor, operation_identity, plugin_operation_identity};
25
26use prost::Message;
32use prost_types::FileDescriptorSet;
33use sha2::{Digest, Sha256};
34
35use crate::model::{Declarations, Protobuf, lane_declaration};
37use std::collections::{BTreeMap, BTreeSet};
38
39#[derive(Debug, thiserror::Error)]
48#[error("Invalid Protobuf Schema: {0}")]
49pub struct Error(String);
50
51pub fn fingerprint(id: &str, types: &FileDescriptorSet, request: &str, response: &str) -> Result<Vec<u8>, Error> {
62 let request = message_types(types, request)?;
63 let response = message_types(types, response)?;
64 let mut hash = Sha256::new();
65 hash.update(id.as_bytes());
66 hash.update([0]);
67 hash.update(request.encode_to_vec());
68 hash.update(response.encode_to_vec());
69 Ok(hash.finalize().to_vec())
70}
71
72pub fn host_fingerprint(id: &str, contract: &Protobuf) -> Result<Vec<u8>, Error> {
74 let descriptors = FileDescriptorSet::decode(contract.descriptors.as_slice()).map_err(|error| Error(error.to_string()))?;
75 let fingerprint = fingerprint(id, &descriptors, &contract.request, &contract.response)?;
76 if contract.input.is_none() && contract.output.is_none() {
77 return Ok(fingerprint);
78 }
79 let mut hash = Sha256::new();
80 hash.update(b"harmos.protobuf.stream.v1\0");
81 hash.update(fingerprint);
82 for message in [&contract.input, &contract.output] {
83 match message {
84 None => hash.update([0]),
85 Some(message) => {
86 hash.update([1]);
87 hash.update(self::fingerprint(id, &descriptors, message, message)?);
88 }
89 }
90 }
91 Ok(hash.finalize().to_vec())
92}
93
94pub fn declarations_digest(declarations: &Declarations) -> Vec<u8> {
96 let mut declarations = declarations.clone();
97 declarations.digest.clear();
98 Sha256::digest(declarations.encode_to_vec()).to_vec()
99}
100
101pub fn plugin_lanes(plugin: &str, declarations: &Declarations) -> Result<(), Error> {
103 let mut names = BTreeSet::new();
104 for lane in &declarations.lanes {
105 let operation = declarations.operations.iter().find(|operation| operation.lane == lane.name);
106 let operation = operation.map_or("<unassigned>", |operation| operation.id.as_str());
107 let context = format!("Declare Operation `{operation}` Lane `{}`", lane.name);
108 if lane.name.is_empty() || lane.name == "Open" || lane.name == "harmos.resident" || lane.name.chars().any(char::is_control) {
109 return Err(Error(format!("{context}: Choose a Nonempty, Nonreserved Name Without Control Characters")));
110 }
111 if !names.insert(lane.name.as_str()) {
112 return Err(Error(format!("{context}: Declare Each Lane Name Once")));
113 }
114 match lane.capacity {
115 Some(lane_declaration::Capacity::Serial(true) | lane_declaration::Capacity::Unbounded(true)) => {}
116 Some(lane_declaration::Capacity::Concurrent(width)) if width > 0 => {}
117 _ => return Err(Error(format!("{context}: Declare Serial, a Positive Concurrent Width or Unbounded Capacity"))),
118 }
119 }
120 for operation in &declarations.operations {
121 if operation.lane.is_empty() {
122 continue;
123 }
124 let context = format!("Declare Operation `{}` Lane `{}`", operation.id, operation.lane);
125 if operation.id == format!("{plugin}.initialize") || operation.id == format!("{plugin}.finalize") {
126 return Err(Error(format!("{context}: Remove the Lifecycle Lane Assignment")));
127 }
128 if !names.contains(operation.lane.as_str()) {
129 return Err(Error(format!("{context}: Declare This Lane in the Plugin")));
130 }
131 }
132 Ok(())
133}
134
135pub fn plugin_collections(declarations: &Declarations) -> Result<(), Error> {
137 let mut groups = BTreeMap::<&str, Vec<_>>::new();
138 for stream in &declarations.streams {
139 if !stream.collection.is_empty() {
140 groups.entry(&stream.collection).or_default().push(stream);
141 }
142 }
143 for (id, members) in groups {
144 let first = members.iter().map(|stream| stream.id.as_str()).min();
145 if first != Some(id) {
146 return Err(Error(format!("Declare Collection `{id}`: use its lexicographically first member stream as its identity")));
147 }
148 for stream in &members {
149 if stream.interval.is_none() && stream.interval_setting.is_empty() {
150 return Err(Error(format!(
151 "Declare Collection `{id}`: give member `{}` a positive interval or duration setting",
152 stream.id
153 )));
154 }
155 }
156 }
157 Ok(())
158}
159
160pub fn descriptor_options() -> FileDescriptorSet {
162 FileDescriptorSet::decode(include_bytes!("../message-options-descriptors.bin").as_slice())
163 .expect("generated message option descriptors")
164}
165
166