Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 45 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,60 @@
on: [ push, pull_request ]
name: Linter and unit tests

name: build
on:
push:
branches: [ master ]
paths:
- '.github/workflows/build.yml'
- "**/Cargo.*"
- "src/**"
- "tests/**"
pull_request:
branches: [ master ]
paths:
- '.github/workflows/build.yml'
- "**/Cargo.*"
- "src/**"
- "tests/**"

jobs:
test:
name: Rust project
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install latest nightly
- name: "Checkout"
uses: actions/checkout@v2

- name: "Install latest nightly"
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly
override: true
components: rustfmt, clippy
- name: Run cargo test

- name: "Format check"
uses: actions-rs/cargo@v1
with:
command: fmt
args: -- --check

- name: "Run cargo test"
uses: actions-rs/cargo@v1
with:
command: test
args: --all-features --all-targets

# Uncomment when supported:
# - name: "Linter checks"
# uses: actions-rs/cargo@v1
# with:
# command: clippy
# args: --all-features --all-targets -- --deny "clippy::all"
#
# - name: "Check"
# uses: actions-rs/cargo@v1
# with:
# command: check
# args: --all-features --all-targets
#
#
16 changes: 15 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# global .gitignore
*.swp

# IDE
.idea/
.vscode/

# Rust
target/
book/

# Mac files
**/.DS_STore

# Test files
tests/*/export/**
tests/*/export_wasm/**
target/**
4 changes: 2 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use clap::{Parser};
use clap::Parser;
use once_cell::sync::Lazy;
// TODO: make non-annotation generate different DeserializeError that is simpler
// and works with From<cbor_event:Error> only
Expand Down Expand Up @@ -51,4 +51,4 @@ pub struct Cli {
pub package_json: bool,
}

pub static CLI_ARGS: Lazy<Cli> = Lazy::new(|| Cli::parse());
pub static CLI_ARGS: Lazy<Cli> = Lazy::new(|| Cli::parse());
152 changes: 91 additions & 61 deletions src/comment_ast.rs
Original file line number Diff line number Diff line change
@@ -1,83 +1,89 @@
extern crate nom;
use nom::{
IResult,
bytes::complete::{tag, take_while1, take_while}, branch::alt, multi::many0,
branch::alt,
bytes::complete::{tag, take_while, take_while1},
multi::many0,
IResult,
};

#[derive(Default, Debug, PartialEq)]
pub struct RuleMetadata {
pub name: Option<String>,
pub is_newtype: bool,
pub name: Option<String>,
pub is_newtype: bool,
}

fn merge_metadata(r1: &RuleMetadata, r2: &RuleMetadata) -> RuleMetadata {
RuleMetadata {
name: match (r1.name.as_ref(), r2.name.as_ref()) {
(Some(val1), Some(val2)) => panic!("Key \"name\" specified twice: {:?} {:?}", val1, val2),
(val@Some(_), _) => val.cloned(),
(_, val) => val.cloned()
(Some(val1), Some(val2)) => {
panic!("Key \"name\" specified twice: {:?} {:?}", val1, val2)
}
(val @ Some(_), _) => val.cloned(),
(_, val) => val.cloned(),
},
is_newtype: r1.is_newtype || r2.is_newtype
is_newtype: r1.is_newtype || r2.is_newtype,
}
}

enum ParseResult {
NewType,
Name(String)
NewType,
Name(String),
}

impl RuleMetadata {
fn from_parse_results(results: &[ParseResult]) -> RuleMetadata {
let mut base = RuleMetadata::default();
for result in results {
match result {
ParseResult::Name(name) => {
match base.name.as_ref() {
Some(old_name) => panic!("Key \"name\" specified twice: {:?} {:?}", old_name, name),
None => { base.name = Some(name.to_string()); }
}
},
ParseResult::NewType => { base.is_newtype = true; }
}
fn from_parse_results(results: &[ParseResult]) -> RuleMetadata {
let mut base = RuleMetadata::default();
for result in results {
match result {
ParseResult::Name(name) => match base.name.as_ref() {
Some(old_name) => {
panic!("Key \"name\" specified twice: {:?} {:?}", old_name, name)
}
None => {
base.name = Some(name.to_string());
}
},
ParseResult::NewType => {
base.is_newtype = true;
}
}
}
base
}
base
}
}

fn tag_name(input: &str) -> IResult<&str, ParseResult> {
let (input, _) = tag("@name")(input)?;
let (input, _) = take_while(char::is_whitespace)(input)?;
let (input, name) = take_while1(|ch| !char::is_whitespace(ch))(input)?;
let (input, _) = tag("@name")(input)?;
let (input, _) = take_while(char::is_whitespace)(input)?;
let (input, name) = take_while1(|ch| !char::is_whitespace(ch))(input)?;

Ok((input, ParseResult::Name(name.to_string())))
Ok((input, ParseResult::Name(name.to_string())))
}
fn tag_newtype(input: &str) -> IResult<&str, ParseResult> {
let (input, _) = tag("@newtype")(input)?;
let (input, _) = tag("@newtype")(input)?;

Ok((input, ParseResult::NewType))
Ok((input, ParseResult::NewType))
}
fn whitespace_then_tag(input: &str) -> IResult<&str, ParseResult> {
let (input, _) = take_while(char::is_whitespace)(input)?;
let (input, result) = alt((tag_name, tag_newtype))(input)?;
let (input, _) = take_while(char::is_whitespace)(input)?;
let (input, result) = alt((tag_name, tag_newtype))(input)?;

Ok((input, result))
Ok((input, result))
}

fn rule_metadata(input: &str) -> IResult<&str, RuleMetadata> {
let (input, parse_results) = many0(whitespace_then_tag)(input)?;

let (input, parse_results) = many0(whitespace_then_tag)(input)?;

Ok((input, RuleMetadata::from_parse_results(&parse_results)))
Ok((input, RuleMetadata::from_parse_results(&parse_results)))
}


impl <'a> From<Option<&'a cddl::ast::Comments<'a>>> for RuleMetadata {
fn from(comments: Option<&'a cddl::ast::Comments<'a>>) -> RuleMetadata {
match comments {
None => RuleMetadata::default(),
Some(c) => metadata_from_comments(&c.0)
impl<'a> From<Option<&'a cddl::ast::Comments<'a>>> for RuleMetadata {
fn from(comments: Option<&'a cddl::ast::Comments<'a>>) -> RuleMetadata {
match comments {
None => RuleMetadata::default(),
Some(c) => metadata_from_comments(&c.0),
}
}
}
}

pub fn metadata_from_comments(comments: &[&str]) -> RuleMetadata {
Expand All @@ -86,38 +92,62 @@ pub fn metadata_from_comments(comments: &[&str]) -> RuleMetadata {
if let Ok(comment_metadata) = rule_metadata(comment) {
result = merge_metadata(&result, &comment_metadata.1);
}
};
}
result
}

#[test]
fn parse_comment_name() {
assert_eq!(rule_metadata("@name foo"), Ok(("", RuleMetadata {
name: Some("foo".to_string()),
is_newtype: false,
})));
assert_eq!(
rule_metadata("@name foo"),
Ok((
"",
RuleMetadata {
name: Some("foo".to_string()),
is_newtype: false,
}
))
);
}

#[test]
fn parse_comment_newtype() {
assert_eq!(rule_metadata("@newtype"), Ok(("", RuleMetadata {
name: None,
is_newtype: true
})));
assert_eq!(
rule_metadata("@newtype"),
Ok((
"",
RuleMetadata {
name: None,
is_newtype: true
}
))
);
}

#[test]
fn parse_comment_newtype_and_name() {
assert_eq!(rule_metadata("@newtype @name foo"), Ok(("", RuleMetadata {
name: Some("foo".to_string()),
is_newtype: true
})));
assert_eq!(
rule_metadata("@newtype @name foo"),
Ok((
"",
RuleMetadata {
name: Some("foo".to_string()),
is_newtype: true
}
))
);
}

#[test]
fn parse_comment_newtype_and_name_inverse() {
assert_eq!(rule_metadata("@name foo @newtype"), Ok(("", RuleMetadata {
name: Some("foo".to_string()),
is_newtype: true
})));
}
assert_eq!(
rule_metadata("@name foo @newtype"),
Ok((
"",
RuleMetadata {
name: Some("foo".to_string()),
is_newtype: true
}
))
);
}
Loading