mirror of
https://github.com/google/bumble.git
synced 2026-04-16 00:25:31 +00:00
Faster company id table
Following up on the [loose end from the initial PR](https://github.com/google/bumble/pull/207#discussion_r1278015116), we can avoid accessing the Python company id map at runtime by doing code gen ahead of time. Using an example to do the code gen avoids even the small build slowdown from invoking the code gen logic in build.rs, but more importantly, means that it's still a totally boring normal build that won't require any IDE setup, etc, to work for everyone. Since the company ID list changes rarely, and there's a test to ensure it always matches, this seems like a good trade.
This commit is contained in:
7
rust/CHANGELOG.md
Normal file
7
rust/CHANGELOG.md
Normal file
@@ -0,0 +1,7 @@
|
||||
# Next
|
||||
|
||||
- Code-gen company ID table
|
||||
|
||||
# 0.1.0
|
||||
|
||||
- Initial release
|
||||
129
rust/Cargo.lock
generated
129
rust/Cargo.lock
generated
@@ -130,7 +130,7 @@ name = "bumble"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"clap 4.3.17",
|
||||
"clap 4.3.19",
|
||||
"env_logger",
|
||||
"hex",
|
||||
"itertools",
|
||||
@@ -158,9 +158,12 @@ checksum = "89b2fd2a0dcf38d7971e2194b6b6eebab45ae01067456a7fd93d5547a61b70be"
|
||||
|
||||
[[package]]
|
||||
name = "cc"
|
||||
version = "1.0.79"
|
||||
version = "1.0.81"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f"
|
||||
checksum = "6c6b2562119bf28c3439f7f02db99faf0aa1a8cdfe5772a2ee155d32227239f0"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
@@ -185,9 +188,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "clap"
|
||||
version = "4.3.17"
|
||||
version = "4.3.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5b0827b011f6f8ab38590295339817b0d26f344aa4932c3ced71b45b0c54b4a9"
|
||||
checksum = "5fd304a20bff958a57f04c4e96a2e7594cc4490a0e809cbd48bb6437edaa452d"
|
||||
dependencies = [
|
||||
"clap_builder",
|
||||
"clap_derive",
|
||||
@@ -196,9 +199,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "clap_builder"
|
||||
version = "4.3.17"
|
||||
version = "4.3.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9441b403be87be858db6a23edb493e7f694761acdc3343d5a0fcaafd304cbc9e"
|
||||
checksum = "01c6a3f08f1fe5662a35cfe393aec09c4df95f60ee93b7556505260f75eee9e1"
|
||||
dependencies = [
|
||||
"anstream",
|
||||
"anstyle",
|
||||
@@ -215,7 +218,7 @@ dependencies = [
|
||||
"heck",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.26",
|
||||
"syn 2.0.28",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -241,9 +244,9 @@ checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7"
|
||||
|
||||
[[package]]
|
||||
name = "either"
|
||||
version = "1.8.1"
|
||||
version = "1.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91"
|
||||
checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07"
|
||||
|
||||
[[package]]
|
||||
name = "env_logger"
|
||||
@@ -260,9 +263,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "errno"
|
||||
version = "0.3.1"
|
||||
version = "0.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4bcfec3a70f97c962c307b2d2c56e358cf1d00b558d74262b5f929ee8cc7e73a"
|
||||
checksum = "6b30f669a7961ef1631673d2766cc92f52d64f7ef354d4fe0ddfd30ed52f0f4f"
|
||||
dependencies = [
|
||||
"errno-dragonfly",
|
||||
"libc",
|
||||
@@ -281,12 +284,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "fastrand"
|
||||
version = "1.9.0"
|
||||
version = "2.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e51093e27b0797c359783294ca4f0a911c270184cb10f85783b118614a1501be"
|
||||
dependencies = [
|
||||
"instant",
|
||||
]
|
||||
checksum = "6999dc1837253364c2ebb0704ba97994bd874e8f195d665c50b7548f6ea92764"
|
||||
|
||||
[[package]]
|
||||
name = "futures"
|
||||
@@ -344,7 +344,7 @@ checksum = "89ca545a94061b6365f2c7355b4b32bd20df3ff95f02da9329b34ccc3bd6ee72"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.26",
|
||||
"syn 2.0.28",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -449,31 +449,11 @@ version = "1.0.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bfa799dd5ed20a7e349f3b4639aa80d74549c81716d9ec4f994c9b5815598306"
|
||||
|
||||
[[package]]
|
||||
name = "instant"
|
||||
version = "0.1.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "inventory"
|
||||
version = "0.3.9"
|
||||
version = "0.3.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "25b1d6b4b9fb75fc419bdef998b689df5080a32931cb3395b86202046b56a9ea"
|
||||
|
||||
[[package]]
|
||||
name = "io-lifetimes"
|
||||
version = "1.0.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "eae7b9aee968036d54dce06cebaefd919e4472e753296daccd6d344e3e2df0c2"
|
||||
dependencies = [
|
||||
"hermit-abi 0.3.2",
|
||||
"libc",
|
||||
"windows-sys",
|
||||
]
|
||||
checksum = "a53088c87cf71c9d4f3372a2cb9eea1e7b8a0b1bf8b7f7d23fe5b76dbb07e63b"
|
||||
|
||||
[[package]]
|
||||
name = "is-terminal"
|
||||
@@ -482,7 +462,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cb0889898416213fab133e1d33a0e5858a48177452750691bde3666d0fdbaf8b"
|
||||
dependencies = [
|
||||
"hermit-abi 0.3.2",
|
||||
"rustix 0.38.4",
|
||||
"rustix",
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
@@ -521,15 +501,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "linux-raw-sys"
|
||||
version = "0.3.8"
|
||||
version = "0.4.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ef53942eb7bf7ff43a617b3e2c1c4a5ecf5944a7c1bc12d7ee39bbb15e5c1519"
|
||||
|
||||
[[package]]
|
||||
name = "linux-raw-sys"
|
||||
version = "0.4.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "09fc20d2ca12cb9f044c93e3bd6d32d523e6e2ec3db4f7b2939cd99026ecd3f0"
|
||||
checksum = "57bcfdad1b858c2db7c38303a6d2ad4dfaf5eb53dfeb0910128b2c26d6158503"
|
||||
|
||||
[[package]]
|
||||
name = "lock_api"
|
||||
@@ -804,9 +778,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.31"
|
||||
version = "1.0.32"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5fe8a65d69dd0808184ebb5f836ab526bb259db23c657efa38711b1072ee47f0"
|
||||
checksum = "50f3b39ccfb720540debaa0164757101c08ecb8d326b15358ce76a62c7e85965"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
@@ -864,9 +838,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "regex-automata"
|
||||
version = "0.3.3"
|
||||
version = "0.3.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "39354c10dd07468c2e73926b23bb9c2caca74c5501e38a35da70406f1d923310"
|
||||
checksum = "b7b6d6190b7594385f61bd3911cd1be99dfddcfc365a4160cc2ab5bff4aed294"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
@@ -897,28 +871,14 @@ checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76"
|
||||
|
||||
[[package]]
|
||||
name = "rustix"
|
||||
version = "0.37.23"
|
||||
version = "0.38.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4d69718bf81c6127a49dc64e44a742e8bb9213c0ff8869a22c308f84c1d4ab06"
|
||||
dependencies = [
|
||||
"bitflags 1.3.2",
|
||||
"errno",
|
||||
"io-lifetimes",
|
||||
"libc",
|
||||
"linux-raw-sys 0.3.8",
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustix"
|
||||
version = "0.38.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0a962918ea88d644592894bc6dc55acc6c0956488adcebbfb6e273506b7fd6e5"
|
||||
checksum = "1ee020b1716f0a80e2ace9b03441a749e402e86712f15f16fe8a8f75afac732f"
|
||||
dependencies = [
|
||||
"bitflags 2.3.3",
|
||||
"errno",
|
||||
"libc",
|
||||
"linux-raw-sys 0.4.3",
|
||||
"linux-raw-sys",
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
@@ -996,7 +956,7 @@ dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"rustversion",
|
||||
"syn 2.0.26",
|
||||
"syn 2.0.28",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -1012,9 +972,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.26"
|
||||
version = "2.0.28"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "45c3457aacde3c65315de5031ec191ce46604304d2446e803d71ade03308d970"
|
||||
checksum = "04361975b3f5e348b2189d8dc55bc942f278b2d482a6a0365de5bdd62d351567"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
@@ -1023,21 +983,20 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "target-lexicon"
|
||||
version = "0.12.10"
|
||||
version = "0.12.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1d2faeef5759ab89935255b1a4cd98e0baf99d1085e37d36599c625dac49ae8e"
|
||||
checksum = "9d0e916b1148c8e263850e1ebcbd046f333e0683c724876bb0da63ea4373dc8a"
|
||||
|
||||
[[package]]
|
||||
name = "tempfile"
|
||||
version = "3.6.0"
|
||||
version = "3.7.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "31c0432476357e58790aaa47a8efb0c5138f137343f3b5f23bd36a27e3b0a6d6"
|
||||
checksum = "5486094ee78b2e5038a6382ed7645bc084dc2ec433426ca4c3cb61e2007b8998"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"cfg-if",
|
||||
"fastrand",
|
||||
"redox_syscall",
|
||||
"rustix 0.37.23",
|
||||
"rustix",
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
@@ -1058,22 +1017,22 @@ checksum = "222a222a5bfe1bba4a77b45ec488a741b3cb8872e5e499451fd7d0129c9c7c3d"
|
||||
|
||||
[[package]]
|
||||
name = "thiserror"
|
||||
version = "1.0.43"
|
||||
version = "1.0.44"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a35fc5b8971143ca348fa6df4f024d4d55264f3468c71ad1c2f365b0a4d58c42"
|
||||
checksum = "611040a08a0439f8248d1990b111c95baa9c704c805fa1f62104b39655fd7f90"
|
||||
dependencies = [
|
||||
"thiserror-impl",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thiserror-impl"
|
||||
version = "1.0.43"
|
||||
version = "1.0.44"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "463fe12d7993d3b327787537ce8dd4dfa058de32fc2b195ef3cde03dc4771e8f"
|
||||
checksum = "090198534930841fab3a5d1bb637cde49e339654e606195f8d9c76eeb081dc96"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.26",
|
||||
"syn 2.0.28",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -1104,7 +1063,7 @@ checksum = "630bdcf245f78637c13ec01ffae6187cca34625e8c63150d424b59e55af2675e"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.26",
|
||||
"syn 2.0.28",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
@@ -23,6 +23,7 @@ hex = "0.4.3"
|
||||
itertools = "0.11.0"
|
||||
lazy_static = "1.4.0"
|
||||
thiserror = "1.0.41"
|
||||
anyhow = { version = "1.0.71", optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
tokio = { version = "1.28.2", features = ["full"] }
|
||||
@@ -38,6 +39,11 @@ env_logger = "0.10.0"
|
||||
rusb = "0.9.2"
|
||||
rand = "0.8.5"
|
||||
|
||||
[[bin]]
|
||||
name = "gen-assigned-numbers"
|
||||
path = "tools/gen_assigned_numbers.rs"
|
||||
required-features = ["bumble-dev-tools"]
|
||||
|
||||
# test entry point that uses pyo3_asyncio's test harness
|
||||
[[test]]
|
||||
name = "pytests"
|
||||
@@ -46,4 +52,5 @@ harness = false
|
||||
|
||||
[features]
|
||||
anyhow = ["pyo3/anyhow"]
|
||||
pyo3-asyncio-attributes = ["pyo3-asyncio/attributes"]
|
||||
pyo3-asyncio-attributes = ["pyo3-asyncio/attributes"]
|
||||
bumble-dev-tools = ["dep:anyhow"]
|
||||
@@ -39,4 +39,18 @@ Check lints:
|
||||
|
||||
```
|
||||
cargo clippy --all-targets
|
||||
```
|
||||
|
||||
## Code gen
|
||||
|
||||
To have the fastest startup while keeping the build simple, code gen for
|
||||
assigned numbers is done with the `gen_assigned_numbers` tool. It should
|
||||
be re-run whenever the Python assigned numbers are changed. To ensure that the
|
||||
generated code is kept up to date, the Rust data is compared to the Python
|
||||
in tests at `pytests/assigned_numbers.rs`.
|
||||
|
||||
To regenerate the assigned number tables based on the Python codebase:
|
||||
|
||||
```
|
||||
PYTHONPATH=.. cargo run --bin gen-assigned-numbers --features bumble-dev-tools
|
||||
```
|
||||
30
rust/pytests/assigned_numbers.rs
Normal file
30
rust/pytests/assigned_numbers.rs
Normal file
@@ -0,0 +1,30 @@
|
||||
use bumble::wrapper::{self, core::Uuid16};
|
||||
use pyo3::{intern, prelude::*, types::PyDict};
|
||||
use std::collections;
|
||||
|
||||
#[pyo3_asyncio::tokio::test]
|
||||
async fn company_ids_matches_python() -> PyResult<()> {
|
||||
let ids_from_python = Python::with_gil(|py| {
|
||||
PyModule::import(py, intern!(py, "bumble.company_ids"))?
|
||||
.getattr(intern!(py, "COMPANY_IDENTIFIERS"))?
|
||||
.downcast::<PyDict>()?
|
||||
.into_iter()
|
||||
.map(|(k, v)| {
|
||||
Ok((
|
||||
Uuid16::from_be_bytes(k.extract::<u16>()?.to_be_bytes()),
|
||||
v.str()?.to_str()?.to_string(),
|
||||
))
|
||||
})
|
||||
.collect::<PyResult<collections::HashMap<_, _>>>()
|
||||
})?;
|
||||
|
||||
assert_eq!(
|
||||
wrapper::assigned_numbers::COMPANY_IDS
|
||||
.iter()
|
||||
.map(|(id, name)| (*id, name.to_string()))
|
||||
.collect::<collections::HashMap<_, _>>(),
|
||||
ids_from_python,
|
||||
"Company ids do not match -- re-run gen_assigned_numbers?"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
@@ -17,4 +17,5 @@ async fn main() -> pyo3::PyResult<()> {
|
||||
pyo3_asyncio::testing::main().await
|
||||
}
|
||||
|
||||
mod assigned_numbers;
|
||||
mod wrapper;
|
||||
|
||||
@@ -12,9 +12,9 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use bumble::{wrapper, wrapper::transport::Transport};
|
||||
use bumble::wrapper::transport::Transport;
|
||||
use nix::sys::stat::Mode;
|
||||
use pyo3::prelude::*;
|
||||
use pyo3::PyResult;
|
||||
|
||||
#[pyo3_asyncio::tokio::test]
|
||||
async fn fifo_transport_can_open() -> PyResult<()> {
|
||||
@@ -29,9 +29,3 @@ async fn fifo_transport_can_open() -> PyResult<()> {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[pyo3_asyncio::tokio::test]
|
||||
async fn company_ids() -> PyResult<()> {
|
||||
assert!(wrapper::assigned_numbers::COMPANY_IDS.len() > 2000);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
2715
rust/src/wrapper/assigned_numbers/company_ids.rs
Normal file
2715
rust/src/wrapper/assigned_numbers/company_ids.rs
Normal file
File diff suppressed because it is too large
Load Diff
@@ -14,40 +14,8 @@
|
||||
|
||||
//! Assigned numbers from the Bluetooth spec.
|
||||
|
||||
use crate::wrapper::core::Uuid16;
|
||||
use lazy_static::lazy_static;
|
||||
use pyo3::{
|
||||
intern,
|
||||
types::{PyDict, PyModule},
|
||||
PyResult, Python,
|
||||
};
|
||||
use std::collections;
|
||||
|
||||
mod company_ids;
|
||||
mod services;
|
||||
|
||||
pub use company_ids::COMPANY_IDS;
|
||||
pub use services::SERVICE_IDS;
|
||||
|
||||
lazy_static! {
|
||||
/// Assigned company IDs
|
||||
pub static ref COMPANY_IDS: collections::HashMap<Uuid16, String> = load_company_ids()
|
||||
.expect("Could not load company ids -- are Bumble's Python sources available?");
|
||||
|
||||
}
|
||||
|
||||
fn load_company_ids() -> PyResult<collections::HashMap<Uuid16, String>> {
|
||||
// this takes about 4ms on a fast machine -- slower than constructing in rust, but not slow
|
||||
// enough to worry about
|
||||
Python::with_gil(|py| {
|
||||
PyModule::import(py, intern!(py, "bumble.company_ids"))?
|
||||
.getattr(intern!(py, "COMPANY_IDENTIFIERS"))?
|
||||
.downcast::<PyDict>()?
|
||||
.into_iter()
|
||||
.map(|(k, v)| {
|
||||
Ok((
|
||||
Uuid16::from_be_bytes(k.extract::<u16>()?.to_be_bytes()),
|
||||
v.str()?.to_str()?.to_string(),
|
||||
))
|
||||
})
|
||||
.collect::<PyResult<collections::HashMap<_, _>>>()
|
||||
})
|
||||
}
|
||||
|
||||
@@ -59,7 +59,7 @@ impl AdvertisingData {
|
||||
}
|
||||
|
||||
/// 16-bit UUID
|
||||
#[derive(PartialEq, Eq, Hash)]
|
||||
#[derive(PartialEq, Eq, Hash, Clone, Copy)]
|
||||
pub struct Uuid16 {
|
||||
/// Big-endian bytes
|
||||
uuid: [u8; 2],
|
||||
|
||||
97
rust/tools/gen_assigned_numbers.rs
Normal file
97
rust/tools/gen_assigned_numbers.rs
Normal file
@@ -0,0 +1,97 @@
|
||||
// Copyright 2023 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! This tool generates Rust code with assigned number tables from the equivalent Python.
|
||||
|
||||
use pyo3::{
|
||||
intern,
|
||||
types::{PyDict, PyModule},
|
||||
PyResult, Python,
|
||||
};
|
||||
use std::{collections, env, fs, path};
|
||||
|
||||
fn main() -> anyhow::Result<()> {
|
||||
pyo3::prepare_freethreaded_python();
|
||||
let mut dir = path::Path::new(&env::var("CARGO_MANIFEST_DIR")?).to_path_buf();
|
||||
dir.push("src/wrapper/assigned_numbers");
|
||||
|
||||
company_ids(&dir)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn company_ids(base_dir: &path::Path) -> anyhow::Result<()> {
|
||||
let mut sorted_ids = load_company_ids()?.into_iter().collect::<Vec<_>>();
|
||||
sorted_ids.sort_by_key(|(id, _name)| *id);
|
||||
|
||||
let mut contents = String::new();
|
||||
contents.push_str(LICENSE_HEADER);
|
||||
contents.push_str("\n\n");
|
||||
contents.push_str(
|
||||
"// auto-generated by gen_assigned_numbers, do not edit
|
||||
|
||||
use crate::wrapper::core::Uuid16;
|
||||
use lazy_static::lazy_static;
|
||||
use std::collections;
|
||||
|
||||
lazy_static! {
|
||||
/// Assigned company IDs
|
||||
pub static ref COMPANY_IDS: collections::HashMap<Uuid16, &'static str> = [
|
||||
",
|
||||
);
|
||||
|
||||
for (id, name) in sorted_ids {
|
||||
contents.push_str(&format!(" ({id}_u16, r#\"{name}\"#),\n"))
|
||||
}
|
||||
|
||||
contents.push_str(
|
||||
" ]
|
||||
.into_iter()
|
||||
.map(|(id, name)| (Uuid16::from_be_bytes(id.to_be_bytes()), name))
|
||||
.collect();
|
||||
}
|
||||
",
|
||||
);
|
||||
|
||||
let mut company_ids = base_dir.to_path_buf();
|
||||
company_ids.push("company_ids.rs");
|
||||
fs::write(&company_ids, contents)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn load_company_ids() -> PyResult<collections::HashMap<u16, String>> {
|
||||
Python::with_gil(|py| {
|
||||
PyModule::import(py, intern!(py, "bumble.company_ids"))?
|
||||
.getattr(intern!(py, "COMPANY_IDENTIFIERS"))?
|
||||
.downcast::<PyDict>()?
|
||||
.into_iter()
|
||||
.map(|(k, v)| Ok((k.extract::<u16>()?, v.str()?.to_str()?.to_string())))
|
||||
.collect::<PyResult<collections::HashMap<_, _>>>()
|
||||
})
|
||||
}
|
||||
|
||||
const LICENSE_HEADER: &str = r#"// Copyright 2023 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License."#;
|
||||
Reference in New Issue
Block a user