Skip to main content

casper_contract_sdk/
schema.rs

1pub trait CasperSchema {
2    fn schema() -> Schema;
3}
4
5use std::fmt::LowerHex;
6
7use bitflags::Flags;
8use casper_executor_wasm_common::flags::EntryPointFlags;
9use serde::{Deserialize, Deserializer, Serialize, Serializer};
10
11use crate::abi::{Declaration, Definitions};
12
13pub fn serialize_bits<T, S>(data: &T, serializer: S) -> Result<S::Ok, S::Error>
14where
15    S: Serializer,
16    T: Flags,
17    T::Bits: Serialize,
18{
19    data.bits().serialize(serializer)
20}
21
22pub fn deserialize_bits<'de, D, F>(deserializer: D) -> Result<F, D::Error>
23where
24    D: Deserializer<'de>,
25    F: Flags,
26    F::Bits: Deserialize<'de> + LowerHex,
27{
28    let raw: F::Bits = F::Bits::deserialize(deserializer)?;
29    F::from_bits(raw).ok_or(serde::de::Error::custom(format!(
30        "Unexpected flags value 0x{raw:#08x}"
31    )))
32}
33
34#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Clone)]
35pub struct SchemaArgument {
36    pub name: String,
37    pub decl: Declaration,
38}
39
40#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Clone)]
41pub struct SchemaEntryPoint {
42    pub name: String,
43    pub arguments: Vec<SchemaArgument>,
44    pub result: Declaration,
45    #[serde(
46        serialize_with = "serialize_bits",
47        deserialize_with = "deserialize_bits"
48    )]
49    pub flags: EntryPointFlags,
50}
51
52#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Clone)]
53#[serde(tag = "type")]
54pub enum SchemaType {
55    /// Contract schemas contain a state structure that we want to mark in the schema.
56    Contract { state: Declaration },
57    /// Schemas of interface type does not contain state.
58    Interface,
59}
60
61#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Clone)]
62pub struct SchemaMessage {
63    pub name: String,
64    pub decl: Declaration,
65}
66
67#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Clone)]
68pub struct Schema {
69    pub name: String,
70    pub version: Option<String>,
71    #[serde(rename = "type")]
72    pub type_: SchemaType,
73    pub definitions: Definitions,
74    pub entry_points: Vec<SchemaEntryPoint>,
75    pub messages: Vec<SchemaMessage>,
76}
77
78#[derive(Debug)]
79pub struct EntryPoint<'a, F: Fn()> {
80    pub name: &'a str,
81    pub params: &'a [&'a str],
82    pub func: F,
83}
84
85#[cfg(not(target_family = "wasm"))]
86use std::{cell::RefCell, collections::BTreeMap};
87
88#[cfg(not(target_family = "wasm"))]
89thread_local! {
90    pub static DISPATCHER: RefCell<BTreeMap<String, extern "C" fn()>> = RefCell::default();
91}
92
93// #[cfg(not(target_family = "wasm"))]
94// #[no_mangle]
95// pub unsafe fn register_func(name: &str, f: extern "C" fn() -> ()) {
96//     println!("registering function {}", name);
97//     DISPATCHER.with(|foo| foo.borrow_mut().insert(name.to_string(), f));
98// }