Skip to content

Commit

Permalink
few import tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
amitu committed Nov 12, 2024
1 parent 7cadd45 commit a88cbae
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
10 changes: 8 additions & 2 deletions v0.5/fastn-unresolved/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@ impl fastn_section::JDebug for fastn_unresolved::Import {
fn debug(&self, _source: &str) -> serde_json::Value {
let mut o = serde_json::Map::new();

let name = if self.package.0.is_empty() {
self.module.0.to_string()
} else {
format!("{}/{}", self.package.0, self.module.0)
};

o.insert(
"import".into(),
match self.alias {
Some(ref v) => format!("{}{}=>{}", self.package.0, self.module.0, v.0),
None => format!("{}{}", self.package.0, self.module.0),
Some(ref v) => format!("{name}=>{}", v.0),
None => name,
}
.into(),
);
Expand Down
26 changes: 21 additions & 5 deletions v0.5/fastn-unresolved/src/parser/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,28 @@ pub(super) fn import(
}

fn parse_module_name(
_source: &str,
caption: &str,
_document: &mut fastn_unresolved::Document,
) -> Option<fastn_unresolved::Import> {
// section.caption must be single text block, parsable as a module-name.
// module-name must be internally able to handle aliasing.
todo!()
let (module, alias) = match caption.split_once(" as ") {
Some((module, alias)) => (module, Some(alias)),
None => (caption, None),
};

let (package, module) = match module.split_once("/") {
Some((package, module)) => (package, module),
None => ("", module),
};

Some(fastn_unresolved::Import {
package: fastn_unresolved::PackageName(package.to_string()),
module: fastn_unresolved::ModuleName(module.to_string()),
alias: alias.map(|v| fastn_unresolved::Identifier(v.to_string())),
exports: None,
exposing: None,
})
}

fn parse_export(
Expand All @@ -74,7 +90,6 @@ fn parse_export(
_document: &mut fastn_unresolved::Document,
_import: &mut fastn_unresolved::Import,
) {
todo!()
}

fn parse_exposing(
Expand All @@ -83,13 +98,14 @@ fn parse_exposing(
_document: &mut fastn_unresolved::Document,
_import: &mut fastn_unresolved::Import,
) {
todo!()
}

#[cfg(test)]
mod tests {
#[track_caller]
fn t1(source: &str, expected: serde_json::Value) {
use fastn_section::JDebug;

let (mut document, sections) =
fastn_unresolved::Document::new(fastn_section::Document::parse(source));

Expand All @@ -99,7 +115,7 @@ mod tests {
};

super::import(source, section, &mut document);
assert_eq!(serde_json::to_value(document.imports).unwrap(), expected);
assert_eq!(document.imports.get(0).unwrap().debug(source), expected);
}

macro_rules! t {
Expand Down

0 comments on commit a88cbae

Please sign in to comment.