Skip to content

Commit

Permalink
Merge pull request #352 from moonbitlang/improve-error-message
Browse files Browse the repository at this point in the history
improve error message
  • Loading branch information
lijunchen authored Sep 29, 2024
2 parents 7d75880 + 85f17ed commit 2c2079e
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 13 deletions.
2 changes: 2 additions & 0 deletions crates/moon/tests/test_cases/general.in/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target/
.mooncakes/
1 change: 1 addition & 0 deletions crates/moon/tests/test_cases/general.in/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# username/hello
10 changes: 10 additions & 0 deletions crates/moon/tests/test_cases/general.in/moon.mod.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "username/hello",
"version": "0.1.0",
"readme": "README.md",
"repository": "",
"license": "",
"keywords": [],
"description": "",
"source": "src"
}
3 changes: 3 additions & 0 deletions crates/moon/tests/test_cases/general.in/src/lib/hello.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn hello() -> String {
"Hello, world!"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
test "hello" {
if @lib.hello() != "Hello, world!" {
fail!("@lib.hello() != \"Hello, world!\"")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
3 changes: 3 additions & 0 deletions crates/moon/tests/test_cases/general.in/src/main/main.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main {
println(@lib.hello())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"is-main": true,
"import": [
"username/hello/lib"
]
}
28 changes: 26 additions & 2 deletions crates/moon/tests/test_cases/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ use std::io::Write;

use super::*;
use expect_test::expect;
use moonutil::common::{
get_cargo_pkg_version, CargoPathExt, TargetBackend, DEP_PATH, MOON_MOD_JSON,
use moonutil::{
common::{get_cargo_pkg_version, CargoPathExt, TargetBackend, DEP_PATH, MOON_MOD_JSON},
module::MoonModJSON,
};
use serde::{Deserialize, Serialize};
use walkdir::WalkDir;
Expand Down Expand Up @@ -6747,3 +6748,26 @@ fn test_moon_coverage() {
expect!["Total: 3/6"],
);
}

#[test]
fn test_bad_version() {
let dir = TestDir::new("general.in");
let content = std::fs::read_to_string(dir.join("moon.mod.json")).unwrap();
let mut moon_mod: MoonModJSON = serde_json::from_str(&content).unwrap();
moon_mod.version = Some("0.0".to_string());
std::fs::write(
dir.join("moon.mod.json"),
serde_json::to_string(&moon_mod).unwrap(),
)
.unwrap();
check(
&get_err_stderr(&dir, ["check"]),
expect![[r#"
error: failed to load `$ROOT/moon.mod.json`
Caused by:
0: `version` bad format
1: unexpected end of input while parsing minor version number
"#]],
);
}
4 changes: 2 additions & 2 deletions crates/mooncake/src/registry/online.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl super::Registry for OnlineRegistry {
continue;
}
};
let module: MoonMod = module.into();
let module: MoonMod = module.try_into()?;
if let Some(v) = &module.version {
res.insert(v.clone(), Rc::new(module));
}
Expand Down Expand Up @@ -144,7 +144,7 @@ impl OnlineRegistry {
let lines = reader.lines().collect::<std::io::Result<Vec<String>>>()?;
for line in lines.iter().rev() {
let j: MoonModJSON = serde_json_lenient::from_str(line)?;
if j.version.as_ref() == Some(version) {
if j.version.as_ref() == Some(&version.to_string()) {
if let Some(checksum) = j.checksum {
return Ok(checksum);
} else {
Expand Down
7 changes: 6 additions & 1 deletion crates/moonutil/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ pub enum MoonModJSONFormatErrorKind {
Parse(#[from] serde_json_lenient::Error),
#[error("`source` bad format")]
Source(#[from] SourceError),
#[error("`version` bad format")]
Version(#[from] semver::Error),
}

pub fn read_module_from_json(path: &Path) -> Result<MoonMod, MoonModJSONFormatError> {
Expand All @@ -114,7 +116,10 @@ pub fn read_module_from_json(path: &Path) -> Result<MoonMod, MoonModJSONFormatEr
});
}
}
Ok(j.into())
j.try_into().map_err(|e| MoonModJSONFormatError {
path: path.into(),
kind: e,
})
}

fn read_package_from_json(path: &Path) -> anyhow::Result<MoonPkg> {
Expand Down
24 changes: 16 additions & 8 deletions crates/moonutil/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
//
// For inquiries, you can contact us via e-mail at jichuruanjian@idea.edu.cn.

use crate::common::MoonModJSONFormatErrorKind;
use crate::common::MooncOpt;
use crate::common::MOON_PKG_JSON;
use crate::dependency::{DependencyInfo, DependencyInfoJson};
Expand Down Expand Up @@ -410,8 +411,7 @@ pub struct MoonModJSON {

/// version of the module
#[serde(skip_serializing_if = "Option::is_none")]
#[schemars(with = "Option<String>")]
pub version: Option<Version>,
pub version: Option<String>,

/// third-party dependencies of the module
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -471,18 +471,26 @@ pub struct MoonModJSON {
pub alert_list: Option<String>,
}

impl From<MoonModJSON> for MoonMod {
fn from(j: MoonModJSON) -> Self {
impl TryFrom<MoonModJSON> for MoonMod {
type Error = MoonModJSONFormatErrorKind;
fn try_from(j: MoonModJSON) -> Result<Self, Self::Error> {
let version = match &j.version {
None => None,
Some(v) => {
Some(Version::parse(v.as_str()).map_err(MoonModJSONFormatErrorKind::Version)?)
}
};

let deps = match j.deps {
None => IndexMap::new(),
Some(d) => d.into_iter().map(|(k, v)| (k, v.into())).collect(),
};

let source = j.source.map(|s| if s.is_empty() { ".".into() } else { s });

MoonMod {
Ok(MoonMod {
name: j.name,
version: j.version,
version,
deps,
readme: j.readme,
repository: j.repository,
Expand All @@ -498,14 +506,14 @@ impl From<MoonModJSON> for MoonMod {

alert_list: j.alert_list,
warn_list: j.warn_list,
}
})
}
}

pub fn convert_module_to_mod_json(m: MoonMod) -> MoonModJSON {
MoonModJSON {
name: m.name,
version: m.version,
version: m.version.map(|v| v.to_string()),
deps: Some(m.deps.into_iter().map(|(k, v)| (k, v.into())).collect()),
readme: m.readme,
repository: m.repository,
Expand Down

0 comments on commit 2c2079e

Please sign in to comment.