Skip to main content

bynk_project/
environment.rs

1//! The names a deploy gives a project's resources in a named environment (back-half paydown
2//! 1C.2, Decision E of #4039).
3//!
4//! Pure functions of `(environment, name)`. The plan prints the names, the push writes them into
5//! the `[env.<name>]` block `bynk-emit` renders, and the two must agree by construction: they
6//! are the one set of functions, in one crate below both (#2028).
7
8/// `<name>` in the `default` environment, `<name>-<environment>` in any other. The suffix shape
9/// Cloudflare gives a Worker's *own* deployed name under `--env`, so a Service Binding to it
10/// must match exactly to resolve; queues, buckets, indexes and databases follow the same shape,
11/// which keeps the resource kinds, and the mental model, consistent.
12pub fn env_qualify(environment: &str, name: &str) -> String {
13    if environment == "default" {
14        name.to_string()
15    } else {
16        format!("{name}-{environment}")
17    }
18}
19
20/// The name project Worker `worker` is deployed under in `environment`, and so the name every
21/// Service Binding to it must carry (#2028): [`env_qualify`]. Under `--env`, Wrangler names an
22/// environment that sets no `name` `<top-level name>-<env>` and uses an environment's own `name`
23/// as given, so stating it changes nothing Wrangler would have done on its own; it makes Bynk,
24/// not a Wrangler default, the owner of the name the plan promises.
25pub fn deployed_worker_name(environment: &str, worker: &str) -> String {
26    env_qualify(environment, worker)
27}
28
29/// P5 (#2323): the name an `R2` instance's bucket has on the account in `environment`. Bucket
30/// names are scoped to the account, so (like a Worker's) it is a function of the project's names
31/// alone.
32pub fn r2_bucket_name(environment: &str, name: &str) -> String {
33    env_qualify(environment, name)
34}
35
36/// #3479: the name a `Vectorize` instance's index has in `environment`.
37pub fn vectorize_index_name(environment: &str, name: &str) -> String {
38    env_qualify(environment, name)
39}
40
41/// P3b (#2582): the name an `Analytics` instance's dataset has in `environment`: qualified as
42/// [`env_qualify`] does but with `_` (`shop_cart_signups_staging`), since the dataset is a SQL
43/// table name.
44pub fn analytics_dataset_name(environment: &str, name: &str) -> String {
45    if environment == "default" {
46        name.to_string()
47    } else {
48        format!(
49            "{name}_{}",
50            environment.to_ascii_lowercase().replace('-', "_")
51        )
52    }
53}
54
55/// P4a (#2602): the name a `D1` instance's database has on the account in `environment`.
56pub fn d1_database_name(environment: &str, name: &str) -> String {
57    env_qualify(environment, name)
58}
59
60#[cfg(test)]
61mod tests {
62    use super::*;
63
64    #[test]
65    fn the_default_environment_is_the_bare_name_and_any_other_is_suffixed() {
66        assert_eq!(env_qualify("default", "orders"), "orders");
67        assert_eq!(env_qualify("staging", "orders"), "orders-staging");
68        assert_eq!(
69            deployed_worker_name("staging", "shop-orders"),
70            "shop-orders-staging"
71        );
72        assert_eq!(r2_bucket_name("eu-staging", "media"), "media-eu-staging");
73        assert_eq!(vectorize_index_name("default", "docs"), "docs");
74        assert_eq!(d1_database_name("staging", "db"), "db-staging");
75    }
76
77    #[test]
78    fn a_dataset_is_qualified_with_an_underscore_in_lower_case() {
79        assert_eq!(analytics_dataset_name("default", "signups"), "signups");
80        assert_eq!(
81            analytics_dataset_name("EU-Staging", "signups"),
82            "signups_eu_staging"
83        );
84    }
85}