sonic_callreq/builder.rs
1// SONIC: Toolchain for formally-verifiable distributed contracts
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5// Designed in 2019-2025 by Dr Maxim Orlovsky <orlovsky@ubideco.org>
6// Written in 2024-2025 by Dr Maxim Orlovsky <orlovsky@ubideco.org>
7//
8// Copyright (C) 2019-2024 LNP/BP Standards Association, Switzerland.
9// Copyright (C) 2024-2025 Laboratories for Ubiquitous Deterministic Computing (UBIDECO),
10// Institute for Distributed and Cognitive Systems (InDCS), Switzerland.
11// Copyright (C) 2019-2025 Dr Maxim Orlovsky.
12// All rights under the above copyrights are reserved.
13//
14// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
15// in compliance with the License. You may obtain a copy of the License at
16//
17// http://www.apache.org/licenses/LICENSE-2.0
18//
19// Unless required by applicable law or agreed to in writing, software distributed under the License
20// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
21// or implied. See the License for the specific language governing permissions and limitations under
22// the License.
23
24use amplify::confinement;
25use chrono::{DateTime, Utc};
26use strict_types::{StrictVal, TypeName};
27
28use crate::{CallRequest, CallState, Endpoint, MethodName, StateName};
29
30impl<T, A> CallRequest<T, A> {
31 pub fn new(scope: T, auth: A, data: Option<StrictVal>) -> Self {
32 Self {
33 scope,
34 api: None,
35 call: None,
36 auth,
37 data,
38 lock: None,
39 expiry: None,
40 endpoints: Default::default(),
41 unknown_query: Default::default(),
42 }
43 }
44
45 pub fn use_api(mut self, api: impl Into<TypeName>) -> Self {
46 self.api = Some(api.into());
47 self
48 }
49
50 pub fn use_method(mut self, method: MethodName) -> Self {
51 if let Some(call) = &mut self.call {
52 call.method = method;
53 } else {
54 self.call = Some(CallState::new(method));
55 }
56 self
57 }
58
59 pub fn use_state(mut self, state: StateName) -> Self {
60 let mut call = self
61 .call
62 .expect("use_method must be called before use_state");
63 call.destructible = Some(state);
64 self.call = Some(call);
65 self
66 }
67
68 pub fn use_expiry(mut self, expiry: DateTime<Utc>) -> Self {
69 self.expiry = Some(expiry);
70 self
71 }
72
73 pub fn add_endpoint(mut self, endpoint: Endpoint) -> Result<Self, confinement::Error> {
74 self.endpoints.push(endpoint)?;
75 Ok(self)
76 }
77}