Skip to content

Commit

Permalink
Deduplicate definition of constants
Browse files Browse the repository at this point in the history
Ref. eng/recordflux/RecordFlux#1672
  • Loading branch information
treiher committed Aug 29, 2024
1 parent 120d701 commit 4a1f854
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 10 deletions.
15 changes: 15 additions & 0 deletions rapidflux/src/consts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use librapidflux::consts as lib;
use pyo3::prelude::*;

use crate::register_submodule_declarations;

register_submodule_declarations!(
consts,
[
("BUILTINS_PACKAGE", lib::BUILTINS_PACKAGE),
("INTERNAL_PACKAGE", lib::INTERNAL_PACKAGE),
("MAX_SCALAR_SIZE", lib::MAX_SCALAR_SIZE),
],
[],
[]
);
4 changes: 4 additions & 0 deletions rapidflux/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use pyo3::prelude::*;

mod consts;
mod diagnostics;
mod identifier;
mod logging;
Expand All @@ -28,6 +29,9 @@ fn rapidflux(py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<diagnostics::RapidFluxError>()?;
m.add("FatalError", py.get_type_bound::<diagnostics::FatalError>())?;

// Constants module
register_submodule!(consts, py, m);

// Logging module
register_submodule!(logging, py, m);

Expand Down
7 changes: 6 additions & 1 deletion rapidflux/src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,9 @@ fn set_quiet(value: bool) {
lib::diagnostics::logging::set_quiet(value);
}

register_submodule_declarations!(logging, [], [error, help, info, note, set_quiet, warning]);
register_submodule_declarations!(
logging,
[],
[],
[error, help, info, note, set_quiet, warning]
);
2 changes: 1 addition & 1 deletion rapidflux/src/source_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ fn clear() {
lib::clear();
}

register_submodule_declarations!(source_code, [], [clear, register, retrieve]);
register_submodule_declarations!(source_code, [], [], [clear, register, retrieve]);
1 change: 1 addition & 0 deletions rapidflux/src/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1142,6 +1142,7 @@ impl_states!(
);
register_submodule_declarations!(
ty,
[],
[
Bounds,
Builtins,
Expand Down
10 changes: 7 additions & 3 deletions rapidflux/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ macro_rules! impl_states {
};
}

/// Register classes and functions in a submodule.
/// Register attributes, classes and functions in a submodule.
///
/// This macro generate a `register_<module name>_module` function that is used by the
/// `register_submodule` function later to add a submodule in `rapidflux`.
Expand All @@ -50,18 +50,22 @@ macro_rules! impl_states {
/// fn bar() {}
/// fn baz() {}
///
/// register_submodule_declarations!(foo, [A, B], [bar, baz]);
/// register_submodule_declarations!(foo, [("c", 299_792_458)], [A, B], [bar, baz]);
/// ```
#[macro_export]
macro_rules! register_submodule_declarations {
($module_name:ident, [$($class_name:ident),* $(,)?], [$($fn_name:ident),* $(,)?] $(,)?) => {
($module_name:ident, [$(($attr_name:literal, $attr:expr)),* $(,)?], [$($class_name:ident),* $(,)?], [$($fn_name:ident),* $(,)?] $(,)?) => {
::paste::paste! {
pub fn [<register_ $module_name _module>]<'py>(
py: Python<'py>,
m: &Bound<'py, PyModule>
) -> PyResult<()> {
const PY_MODULE_PATH: &str = concat!("rflx.rapidflux.", stringify!($module_name));

$(
m.add($attr_name, $attr)?;
)*

$(
m.add_class::<$class_name>()?;
)*
Expand Down
9 changes: 4 additions & 5 deletions rflx/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
from pathlib import Path
from typing import Final

from rflx.identifier import ID
from rflx.rapidflux import ID, consts

BUILTINS_PACKAGE: Final = ID("__BUILTINS__")
INTERNAL_PACKAGE: Final = ID("__INTERNAL__")
BUILTINS_PACKAGE: Final = ID(consts.BUILTINS_PACKAGE)
INTERNAL_PACKAGE: Final = ID(consts.INTERNAL_PACKAGE)

# TODO(eng/recordflux/RecordFlux#1077): Size of integers is limited to 63 bits
MAX_SCALAR_SIZE: Final = 63
MAX_SCALAR_SIZE: Final = consts.MAX_SCALAR_SIZE

CACHE_PATH: Final = Path.cwd() / ".rflx_cache"

Expand Down
5 changes: 5 additions & 0 deletions rflx/rapidflux/consts.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from typing import Final

BUILTINS_PACKAGE: Final[str]
INTERNAL_PACKAGE: Final[str]
MAX_SCALAR_SIZE: Final[int]
2 changes: 2 additions & 0 deletions tests/unit/rapidflux/consts_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def test_dummy() -> None:
pass

0 comments on commit 4a1f854

Please sign in to comment.