Skip to content

Commit

Permalink
Reduce size of Rust library
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Sep 11, 2024
1 parent e4318f9 commit 1cfac23
Show file tree
Hide file tree
Showing 14 changed files with 1,981 additions and 2,006 deletions.
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 1cfac23

Please sign in to comment.