Skip to main content

runar_lang/
lib.rs

1//! Rúnar smart contract development crate.
2//!
3//! Provides types, mock crypto functions, and real hash functions for
4//! developing and testing Rúnar contracts in Rust. Import the prelude
5//! to get everything:
6//!
7//! ```ignore
8//! use runar::prelude::*;
9//! ```
10
11pub mod ec;
12pub mod ecdsa;
13pub mod prelude;
14pub mod rabin;
15pub mod sdk;
16pub mod slh_dsa;
17pub mod test_keys;
18pub mod wots;
19
20// Re-export proc-macro attributes so `#[runar::contract]` works.
21pub use runar_lang_macros::{contract, methods, public, stateful_contract};
22
23/// Runs the Rúnar frontend (parse → validate → typecheck) on a `.runar.rs`
24/// source string. Returns `Ok(())` if the contract is valid Rúnar, or an
25/// error describing what failed.
26///
27/// ```ignore
28/// #[test]
29/// fn test_compile() {
30///     let source = include_str!("MyContract.runar.rs");
31///     runar::compile_check(source, "MyContract.runar.rs").unwrap();
32/// }
33/// ```
34pub fn compile_check(source: &str, file_name: &str) -> Result<(), String> {
35    let parse_result = runar_compiler_rust::frontend::parser::parse_source(source, Some(file_name));
36    if !parse_result.errors.is_empty() {
37        let msgs: Vec<String> = parse_result.errors.iter().map(|e| e.to_string()).collect();
38        return Err(format!("parse errors: {}", msgs.join("; ")));
39    }
40
41    let contract = parse_result
42        .contract
43        .ok_or_else(|| format!("no contract found in {}", file_name))?;
44
45    let validation = runar_compiler_rust::frontend::validator::validate(&contract);
46    if !validation.errors.is_empty() {
47        return Err(format!("validation errors: {}", validation.error_strings().join("; ")));
48    }
49
50    let tc = runar_compiler_rust::frontend::typecheck::typecheck(&contract);
51    if !tc.errors.is_empty() {
52        return Err(format!("type check errors: {}", tc.error_strings().join("; ")));
53    }
54
55    Ok(())
56}