Skip to content

Commit

Permalink
Reduce size of Rust library (#33)
Browse files Browse the repository at this point in the history
* Reduce size of Rust library

* simpler

* fix tests

* ci: cache Rust

* ci: cache rust/ dir
  • Loading branch information
DaniPopes authored Sep 17, 2024
1 parent 1ee7e21 commit 0b3150b
Show file tree
Hide file tree
Showing 15 changed files with 1,995 additions and 2,022 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/rust-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ jobs:
- uses: actions/checkout@v2

- name: Install Rust
uses: actions-rs/toolchain@v1
uses: dtolnay/rust-toolchain@stable

- name: Cache Rust artifacts
uses: Swatinem/rust-cache@v2
with:
profile: minimal
toolchain: stable
override: true
cache-on-failure: true
workspaces: rust

- name: Build and Install Rust Project
run: |
cd rust
cargo install --path crates/mesc_cli
run: cargo install --path rust/crates/mesc_cli --profile dev

- name: Set up Python
uses: actions/setup-python@v2
Expand Down
24 changes: 11 additions & 13 deletions python/generate_network_names.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
- ./python/mesc/network_names.py
- ./rust/crates/mesc/src/network_names.rs
"""

from __future__ import annotations

import os
import requests
from typing import Mapping, MutableMapping

import requests

# specialcase the standard name for certain chains
special_cases: Mapping[str, str] = {
Expand Down Expand Up @@ -119,28 +120,25 @@ def generate_python_content(network_names: Mapping[str, str]) -> str:
rust_path = '../rust/crates/mesc/src/network_names.rs'
rust_template = """{preamble}
use crate::{{ChainId, TryIntoChainId}};
use std::collections::HashMap;
use crate::ChainId;
use std::{{collections::HashMap, sync::OnceLock}};
/// get default mapping between chain_id's and network names
pub fn get_network_names() -> HashMap<ChainId, String> {{
{name_data}
pub fn get_network_names() -> &'static HashMap<ChainId, &'static str> {{
static CACHE: OnceLock<HashMap<ChainId, &'static str>> = OnceLock::new();
CACHE.get_or_init(|| NETWORKS.iter().map(|&(chain_id, name)| (chain_id.into(), name)).collect())
}}
pub(crate) const NETWORKS: &[(u64, &str)] = &{name_data};
"""


def generate_rust_content(network_names: Mapping[str, str]) -> str:
rust_preamble = '\n'.join(('//! ' + line).strip() for line in preamble.split('\n'))
name_data = '['
for chain_id, network_name in network_names.items():
name_data += '\n ("' + chain_id + '", "' + network_name + '"),'
name_data += """\n ]
.into_iter()
.filter_map(|(chain_id, name)| match chain_id.try_into_chain_id() {
Ok(chain_id) => Some((chain_id, name.to_string())),
Err(_) => None,
})
.collect()"""
name_data += '\n (' + chain_id + ', "' + network_name + '"),'
name_data += '\n]'
return rust_template.format(preamble=rust_preamble, name_data=name_data)


Expand Down
Loading

0 comments on commit 0b3150b

Please sign in to comment.