Skip to content

Commit

Permalink
failed attempt at integrating fastn-compiler with fastn 0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
amitu committed Dec 6, 2024
1 parent 9900b67 commit 9238a86
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 20 deletions.
5 changes: 2 additions & 3 deletions fastn-core/src/package/package_doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,11 +397,10 @@ pub(crate) async fn read_ftd_(
}
}

async fn read_ftd_2024(_config: &mut fastn_core::RequestConfig) -> fastn_core::Result<FTDResult> {
async fn read_ftd_2024(config: &fastn_core::RequestConfig) -> fastn_core::Result<FTDResult> {
Ok(FTDResult::Html(
fastn_runtime::render_2024_document(
// Box::new(&mut config.config.ds),
todo!(),
&config.config.ds,
Default::default(),
"index.ftd",
serde_json::Value::Null,
Expand Down
6 changes: 3 additions & 3 deletions fastn-ds/src/symbols.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
impl fastn_ds::DocumentStore {
async fn find_all_definitions_in_a_module(
&mut self,
&self,
arena: &mut fastn_unresolved::Arena,
global_aliases: &fastn_unresolved::AliasesSimple,
(file, module): (String, fastn_unresolved::Module),
Expand Down Expand Up @@ -35,9 +35,9 @@ impl fastn_ds::DocumentStore {
}

#[async_trait::async_trait]
impl fastn_compiler::SymbolStore for &mut fastn_ds::DocumentStore {
impl fastn_compiler::SymbolStore for &fastn_ds::DocumentStore {
async fn lookup(
&mut self,
&self,
arena: &mut fastn_unresolved::Arena,
global_aliases: &fastn_unresolved::AliasesSimple,
symbols: &std::collections::HashSet<fastn_unresolved::Symbol>,
Expand Down
2 changes: 1 addition & 1 deletion fastn-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ pub(crate) fn external_css_files(
}

pub async fn render_2024_document(
symbols: Box<dyn fastn_compiler::SymbolStore + Send>,
symbols: dyn fastn_compiler::SymbolStore,
global_aliases: fastn_unresolved::AliasesSimple,
path: &str,
_data: serde_json::Value,
Expand Down
24 changes: 13 additions & 11 deletions v0.5/fastn-compiler/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const ITERATION_THRESHOLD: usize = 100;
// exposing: y as x
//
pub(crate) struct Compiler {
symbols: Box<dyn fastn_compiler::SymbolStore>,
pub(crate) definitions_used: std::collections::HashSet<fastn_unresolved::Symbol>,
pub(crate) arena: fastn_unresolved::Arena,
pub(crate) definitions: std::collections::HashMap<String, fastn_unresolved::URD>,
Expand All @@ -21,7 +20,6 @@ pub(crate) struct Compiler {

impl Compiler {
fn new(
symbols: Box<dyn fastn_compiler::SymbolStore>,
source: &str,
package: &str,
module: Option<&str>,
Expand All @@ -38,7 +36,6 @@ impl Compiler {
document.content = vec![];

Self {
symbols,
arena,
definitions: std::collections::HashMap::new(),
modules: std::collections::HashMap::new(),
Expand All @@ -52,11 +49,11 @@ impl Compiler {
async fn fetch_unresolved_symbols(
&mut self,
symbols_to_fetch: &std::collections::HashSet<fastn_unresolved::Symbol>,
symbols: &impl fastn_compiler::SymbolStore,
) {
self.definitions_used
.extend(symbols_to_fetch.iter().cloned());
let definitions = self
.symbols
let definitions = symbols
.lookup(&mut self.arena, &self.global_aliases, symbols_to_fetch)
.await;
for definition in definitions {
Expand Down Expand Up @@ -170,7 +167,10 @@ impl Compiler {
stuck_on_symbols
}

async fn compile(mut self) -> Result<fastn_resolved::CompiledDocument, fastn_compiler::Error> {
async fn compile(
mut self,
symbols: &impl fastn_compiler::SymbolStore,
) -> Result<fastn_resolved::CompiledDocument, fastn_compiler::Error> {
// we only make 10 attempts to resolve the document: we need a warning if we are not able to
// resolve the document in 10 attempts.
let mut unresolvable = std::collections::HashSet::new();
Expand All @@ -184,7 +184,8 @@ impl Compiler {
break;
}
// ever_used.extend(&unresolved_symbols);
self.fetch_unresolved_symbols(&unresolved_symbols).await;
self.fetch_unresolved_symbols(&unresolved_symbols, &symbols)
.await;
// this itself has to happen in a loop. we need a warning if we are not able to resolve all
// symbols in 10 attempts.
let mut r = ResolveSymbolsResult::default();
Expand All @@ -199,7 +200,8 @@ impl Compiler {
break;
}
// ever_used.extend(r.need_more_symbols);
self.fetch_unresolved_symbols(&r.need_more_symbols).await;
self.fetch_unresolved_symbols(&r.need_more_symbols, &symbols)
.await;
iterations += 1;
}

Expand Down Expand Up @@ -243,14 +245,14 @@ impl Compiler {
/// earlier we had strict mode here, but to simplify things, now we let the caller convert non-empty
/// warnings from OK part as error, and discard the generated JS.
pub async fn compile(
symbols: Box<dyn fastn_compiler::SymbolStore + Send>,
symbols: impl fastn_compiler::SymbolStore,
source: &str,
package: &str,
module: Option<&str>,
global_aliases: fastn_unresolved::AliasesSimple,
) -> Result<fastn_resolved::CompiledDocument, fastn_compiler::Error> {
Compiler::new(symbols, source, package, module, global_aliases)
.compile()
Compiler::new(source, package, module, global_aliases)
.compile(&symbols)
.await
}

Expand Down
4 changes: 2 additions & 2 deletions v0.5/fastn-compiler/src/symbols.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#[async_trait::async_trait]
pub trait SymbolStore: Send {
pub trait SymbolStore {
/// it is okay / acceptable to return more symbols than asked.
///
/// this is because if we are fetching symbols by parsing a ftd file, it makes sense to store
Expand All @@ -11,7 +11,7 @@ pub trait SymbolStore: Send {
/// for some related symbols soon.
// TODO: should we make it async?
async fn lookup(
&mut self,
&self,
arena: &mut fastn_unresolved::Arena,
global_aliases: &fastn_unresolved::AliasesSimple,
symbols: &std::collections::HashSet<fastn_unresolved::Symbol>,
Expand Down

0 comments on commit 9238a86

Please sign in to comment.