Skip to content

Commit

Permalink
Add metadata check #91
Browse files Browse the repository at this point in the history
  • Loading branch information
dalance committed Jan 20, 2023
1 parent b9a6c45 commit f351a94
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ support/vscode/.vscode-test/
support/vscode/*.vsix
support/vscode/bin
support/highlightjs/dist/
veryl-testcase.*
veryl_testcase.*
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## [Unreleased](https://github.com/dalance/veryl/compare/v0.2.1...Unreleased) - ReleaseDate

* [Added] attribute arguments check [#90](https://github.com/dalance/veryl/issues/90)
* [Added] metadata check [#91](https://github.com/dalance/veryl/issues/91)

## [v0.2.1](https://github.com/dalance/veryl/compare/v0.2.0...v0.2.1) - 2023-01-19

Expand Down
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Veryl.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
[project]
name = "veryl-testcase"
name = "veryl_testcase"
version = "0.1.0"
#license = "MIT"
license = "MI OR Apache-2.0"

[build]
clock_type = "posedge"
Expand Down
2 changes: 2 additions & 0 deletions crates/metadata/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ edition = "2021"

[dependencies]
miette = {workspace = true}
regex = {workspace = true}
semver = {workspace = true}
serde = {workspace = true}
spdx = "0.10.0"
thiserror = {workspace = true}
toml = "0.5.10"
veryl-parser = {version = "0.2.1", path = "../parser"}
16 changes: 16 additions & 0 deletions crates/metadata/src/metadata.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::MetadataError;
use regex::Regex;
use semver::Version;
use serde::{Deserialize, Serialize};
use spdx::Expression;
use std::env;
use std::path::{Path, PathBuf};
use std::str::FromStr;
Expand Down Expand Up @@ -37,8 +39,22 @@ impl Metadata {
let text = std::fs::read_to_string(&path)?;
let mut metadata: Metadata = Self::from_str(&text)?;
metadata.metadata_path = path;
metadata.check()?;
Ok(metadata)
}

pub fn check(&self) -> Result<(), MetadataError> {
let valid_project_name = Regex::new(r"^[a-zA-Z_][0-9a-zA-Z_]*$").unwrap();
if !valid_project_name.is_match(&self.project.name) {
return Err(MetadataError::InvalidProjectName(self.project.name.clone()));
}

if let Some(ref license) = self.project.license {
let _ = Expression::parse(license)?;
}

Ok(())
}
}

impl FromStr for Metadata {
Expand Down
11 changes: 11 additions & 0 deletions crates/metadata/src/metadata_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,15 @@ pub enum MetadataError {
#[diagnostic(code(MetadataError::Deserialize), help(""))]
#[error("toml load failed")]
Deserialize(#[from] toml::de::Error),

#[diagnostic(
code(MetadataError::InvalidProjectName),
help("\"[a-zA-Z_][0-9a-zA-Z_]*\" can be used as project name")
)]
#[error("project name \"{0}\" is invalid")]
InvalidProjectName(String),

#[diagnostic(code(MetadataError::InvalidLicense), help(""))]
#[error("license parse failed")]
InvalidLicense(#[from] spdx::ParseError),
}

0 comments on commit f351a94

Please sign in to comment.