Skip to main content

waveless_compiler/
generator.rs

1// Waveless
2// Copyright (C) 2026 Oscar Alvarez Gonzalez
3
4//!
5//! The Waveless' endpoints generator and schema's checksum calculator.
6//! Connects to the specified the project's databases, scans their schema to produce
7//! endpoints accordingly and produces the schema's checksum.
8//!
9use crate::*;
10
11use databases::*;
12
13#[derive(Clone, Constructor, Getters, Debug)]
14#[getset(get = "pub")]
15pub struct GeneratedEndpoints(CheapVec<(Bytes, (Endpoints, Option<EndpointGeneratorChecksum>))>);
16
17impl GeneratedEndpoints {
18    /// Discovers all endpoints from the project's database and calculate the checksum per database.
19    /// TODO: Maybe the endpoint generation logic should be delegated to the [`AnyEndpointGenerator`](trait@waveless_commons::endpoint::generator::AnyEndpointGenerator) trait.
20    #[instrument(skip_all)]
21    pub async fn generate() -> Result<Self> {
22        let cx = CompilerCx::acquire();
23
24        let project = cx.project();
25
26        let db_conns: DbConns = DatabasesManager::acquire().to_owned().into();
27
28        let mut all_endpoints = CheapVec::<_, 0>::new();
29
30        let generators = project.compiler().endpoint_generators().iter();
31
32        for generator in generators {
33            let (endpoints, checksum) = generator.backend().generate(db_conns.to_owned()).await?;
34
35            all_endpoints.push((
36                generator.backend().id()?,
37                (endpoints, {
38                    if let Some(checksum) = checksum {
39                        Some(EndpointGeneratorChecksum::new(
40                            generator.backend().to_owned(),
41                            checksum,
42                        ))
43                    } else {
44                        None
45                    }
46                }),
47            ));
48        }
49
50        Ok(GeneratedEndpoints::new(all_endpoints))
51    }
52}