dusk-forge 0.2.2

A smart contract development framework for Dusk
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Copyright (c) DUSK NETWORK. All rights reserved.

//! Schema types for contract metadata.
//!
//! These types are used by the `#[contract]` macro to generate
//! compile-time contract schemas that describe functions and events.

extern crate alloc;

use serde::Serialize;

/// Schema for a contract function.
#[derive(Debug, Clone, Copy, Serialize)]
pub struct Function {
    /// Function name.
    pub name: &'static str,
    /// Documentation string.
    pub doc: &'static str,
    /// Input type name (or "()" for no input).
    pub input: &'static str,
    /// Output type name (or "()" for no output).
    pub output: &'static str,
    /// Whether this function requires custom serialization.
    pub custom: bool,
}

/// Schema for a contract event.
#[derive(Debug, Clone, Copy, Serialize)]
pub struct Event {
    /// Event topic string.
    pub topic: &'static str,
    /// Event data type name.
    pub data: &'static str,
}

/// Schema for an imported type.
#[derive(Debug, Clone, Copy, Serialize)]
pub struct Import {
    /// The short name used in the contract (e.g., `SetU64`).
    pub name: &'static str,
    /// The full path to the type (e.g., `evm_core::standard_bridge::SetU64`).
    pub path: &'static str,
}

/// Complete schema for a contract.
#[derive(Debug, Clone, Copy, Serialize)]
pub struct Contract {
    /// Contract name.
    pub name: &'static str,
    /// List of imported types with their full paths.
    pub imports: &'static [Import],
    /// List of contract functions.
    pub functions: &'static [Function],
    /// List of contract events.
    pub events: &'static [Event],
}

impl Contract {
    /// Returns an iterator over all imports.
    pub fn iter_imports(&self) -> impl Iterator<Item = &Import> {
        self.imports.iter()
    }

    /// Returns an iterator over all functions.
    pub fn iter_functions(&self) -> impl Iterator<Item = &Function> {
        self.functions.iter()
    }

    /// Returns an iterator over all events.
    pub fn iter_events(&self) -> impl Iterator<Item = &Event> {
        self.events.iter()
    }

    /// Find an import by short name.
    #[must_use]
    pub fn get_import(&self, name: &str) -> Option<&Import> {
        self.imports.iter().find(|i| i.name == name)
    }

    /// Find a function by name.
    #[must_use]
    pub fn get_function(&self, name: &str) -> Option<&Function> {
        self.functions.iter().find(|f| f.name == name)
    }

    /// Find an event by topic.
    #[must_use]
    pub fn get_event(&self, topic: &str) -> Option<&Event> {
        self.events.iter().find(|e| e.topic == topic)
    }

    /// Serialize the schema to a JSON string.
    #[must_use]
    pub fn to_json(&self) -> alloc::string::String {
        serde_json::to_string(self).unwrap_or_else(|_| alloc::string::String::from("{}"))
    }
}