mesc/rust
History 2024-01-28 13:29:00 -08:00
..
crates stop using local toolstr 2024-01-28 13:29:00 -08:00
Cargo.lock bump cli dependency to v0.1.4 2024-01-14 02:08:08 -08:00
Cargo.toml bump to rust v0.1.4 2024-01-14 02:06:32 -08:00
README.md update tests (#15) 2024-01-28 13:06:14 -08:00
rustfmt.toml apply unified clippy+fmt rules 2023-12-24 01:09:41 -08:00

Rust MESC Implementation

This is a reference rust implementation of the MESC standard.

The crates/mesc crate contains rust libraries for reading, writing, and validating MESC configurations.

The crates/mesc_cli crate is a tool for reading, writing, and validating MESC from the command line. See CLI for details.

Contents

Installation

Inside a cargo project: cargo add mesc

Inside a Cargo.toml: mesc = "1.0"

Example Usage

Basic data structures

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct RpcConfig {
    pub mesc_version: String,
    pub default_endpoint: Option<String>,
    pub endpoints: HashMap<String, Endpoint>,
    pub network_defaults: HashMap<ChainId, String>,
    pub network_names: HashMap<String, ChainId>,
    pub profiles: HashMap<String, Profile>,
    pub global_metadata: HashMap<String, serde_json::Value>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Endpoint {
    pub name: String,
    pub url: String,
    pub chain_id: Option<ChainId>,
    pub endpoint_metadata: HashMap<String, serde_json::Value>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Profile {
    pub name: String,
    pub default_endpoint: Option<String>,
    pub network_defaults: HashMap<ChainId, String>,
    pub profile_metadata: HashMap<String, serde_json::Value>,
    pub use_mesc: bool,
}

#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq, Hash)]
pub struct ChainId(String);

Basic read functions

use mesc;
use mesc::MescError;

type OptionalResult = Result<Option<Endpoint>, MescError>;
type MultiResult = Result<Vec<Endpoint>, MescError>;

// check whether mesc is enabled
let enabled: bool = mesc::is_mesc_enabled();

// get the default endpoint
let endpoint: OptionalResult = mesc::get_default_endpoint(None);

// get the default endpoint of a network
let endpoint: OptionalResult = mesc::get_endpoint_by_network(5, None);

// get the default network for a particular tool
let chain_id: OptionalResult = mesc::get_default_endpoint(Some("xyz_tool"));

// get the default endpoint of a network for a particular tool
let endpoint: OptionalResult = mesc::get_endpoint_by_network(5, Some("xyz_tool"));

// get an endpoint by name
let endpoint: OptionalResult = mesc::get_endpoint_by_name("local_goerli");

// parse a user-provided string into a matching endpoint
// (first try 1. endpoint name, then 2. chain id, then 3. network name)
let endpoint: OptionalResult = mesc::get_endpoint_by_query(user_str, Some("xyz_tool"));

// find all endpoints matching given criteria
let query = mesc::MultiEndpointQuery::new().chain_id(5);
let endpoints: MultiResult = mesc::find_endpoints(query);