Skip to content

Commit

Permalink
Integrate dscp-lang into dscp-node
Browse files Browse the repository at this point in the history
Adds dscp-lang as a subcommand of dscp-node under lang. This involved a number of changes:

- made `dscp-lang` both a lib and a bin
- update `dscp-node` to include `lang` subcommand
- update exports from dscp-lang to have necessary visibility and trait impls

In addition this includes:

- update output from `lang build` command when passing `-v` to actually be verbose and more useful
  • Loading branch information
mattdean-digicatapult committed Nov 28, 2023
1 parent 23555bf commit d120911
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 35 deletions.
14 changes: 8 additions & 6 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = '2021'
license = 'Apache-2.0'
repository = 'https://github.com/digicatapult/dscp-node/'
name = 'dscp-node'
version = '9.1.2'
version = '9.1.3'

[[bin]]
name = 'dscp-node'
Expand Down Expand Up @@ -59,6 +59,7 @@ frame-benchmarking-cli = { version = "4.0.0-dev", git = "https://github.com/pari
# Local Dependencies
dscp-node-runtime = { path = '../runtime' }
pallet-transaction-payment-free = { default-features = false, path = '../pallets/transaction-payment-free' }
dscp-lang = { path = '../tools/lang' }

# CLI-specific dependencies
try-runtime-cli = { version = "0.10.0-dev", optional = true, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42" }
Expand Down
4 changes: 4 additions & 0 deletions node/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use dscp_lang::cli::Cli as LangCli;
use sc_cli::RunCmd;

#[derive(Debug, clap::Parser)]
Expand Down Expand Up @@ -43,4 +44,7 @@ pub enum Subcommand {

/// Db meta columns information.
ChainInfo(sc_cli::ChainInfoCmd),

/// DSCP language tool for parsing and compiling dscp files.
Lang(LangCli),
}
3 changes: 3 additions & 0 deletions node/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@ pub fn run() -> sc_cli::Result<()> {
let runner = cli.create_runner(cmd)?;
runner.sync_run(|config| cmd.run::<Block>(&config))
}
Some(Subcommand::Lang(cmd)) => cmd
.run()
.map_err(|lang_err| sc_cli::Error::Application(Box::new(lang_err))),
None => {
let runner = cli.create_runner(&cli.run)?;
runner
Expand Down
11 changes: 10 additions & 1 deletion tools/lang/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
[package]
name = "dscp-lang"
authors = ['Digital Catapult <https://www.digicatapult.org.uk>']
version = "0.1.1"
version = "0.2.0"
edition = "2021"

[lib]
name = "dscp_lang"
path = "src/lib.rs"

[[bin]]
name = "dscp-lang"
path = "src/main.rs"

[dependencies]
clap = { version = "4.4.6", features = ["derive"] }
exitcode = "1.1.2"
Expand All @@ -13,3 +21,4 @@ pest_derive = "2.6"
dscp-runtime-types = { path = '../../runtime/types' }
serde = { version = "1.0.189" }
serde_json = { version = "1.0.107" }
thiserror = { version = "1.0.50" }
26 changes: 14 additions & 12 deletions tools/lang/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@ use clap::{Parser, Subcommand};
use crate::{
ast::{parse_str_to_ast, types::AstRoot},
compiler::compile_ast_to_restrictions,
convert::make_pretty_processes,
convert::transform_to_json,
errors::CompilationError,
};

/// A fictional versioning CLI
#[derive(Debug, Parser)] // requires `derive` feature
#[command(name = "dscp-lang", version, author)]
#[command(about = "Tool for checking and compiling dscp token specifications", long_about = None)]
pub(crate) struct Cli {
pub struct Cli {
#[command(subcommand)]
command: Commands,
}

#[derive(Debug, Subcommand)]
enum Commands {
pub enum Commands {
#[command(arg_required_else_help = true)]
Parse {
#[arg(help = "Path to dscp token specification file")]
Expand Down Expand Up @@ -52,11 +52,11 @@ enum Commands {
}

impl Cli {
pub(crate) fn new() -> Self {
pub fn new() -> Self {
Cli::parse()
}

pub(crate) fn run(&self) -> Result<(), CompilationError> {
pub fn run(&self) -> Result<(), CompilationError> {
match &self.command {
Commands::Parse { file_path, verbose } => {
println!("Loading file {}", file_path.to_str().unwrap());
Expand Down Expand Up @@ -119,17 +119,19 @@ impl Cli {
let ast = parse_str_to_ast(&contents)?;
let programs = compile_ast_to_restrictions(ast)?;

println!("Successfully parsed the following programs:");
if *verbose {
for program in &programs {
println!("{}", String::from_utf8(program.name.to_vec()).unwrap());
let program_str = serde_json::to_string(program).unwrap();
println!("JSON: {}", program_str);
println!("Successfully compiled the following programs:");
for program in &programs {
let program_name = String::from_utf8(program.name.to_vec()).unwrap();
if *verbose {
let program_str = transform_to_json(program, false).unwrap();
println!("\n{}:\n{}", program_name, program_str);
} else {
println!("\t{}", program_name);
}
}

if let Some(path) = output_file {
fs::write(path, make_pretty_processes(&programs).unwrap()).unwrap()
fs::write(path, transform_to_json(&programs, true).unwrap()).unwrap()
}

Ok(())
Expand Down
43 changes: 34 additions & 9 deletions tools/lang/src/convert.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::error::Error;

use serde::Serialize;
use serde_json::Value;

use crate::compiler::Process;

fn transform_value(val: Value) -> Value {
match val {
Value::Array(arr) => {
Expand All @@ -23,17 +22,23 @@ fn transform_value(val: Value) -> Value {
}
}

pub fn make_pretty_processes(processes: &Vec<Process>) -> Result<String, Box<dyn Error>> {
let serialised = serde_json::to_value(processes)?;
pub fn transform_to_json<T>(val: &T, pretty: bool) -> Result<String, Box<dyn Error>>
where
T: Serialize,
{
let serialised = serde_json::to_value(val)?;
let transformed = transform_value(serialised);
Ok(serde_json::to_string_pretty(&transformed)?)
Ok(match pretty {
true => serde_json::to_string_pretty(&transformed),
false => serde_json::to_string(&transformed),
}?)
}

#[cfg(test)]
mod tests {
use dscp_runtime_types::BooleanExpressionSymbol;

use super::make_pretty_processes;
use super::transform_to_json;
use crate::compiler::Process;

#[test]
Expand All @@ -47,7 +52,7 @@ mod tests {
.try_into()
.unwrap(),
}];
let result = make_pretty_processes(&processes);
let result = transform_to_json(&processes, true);

assert!(result.is_ok());
assert_eq!(
Expand All @@ -67,6 +72,26 @@ mod tests {
);
}

#[test]
fn transforms_not_pretty() {
let processes = vec![Process {
name: vec![116u8, 101u8, 115u8, 116u8].try_into().unwrap(),
version: 1u32,
program: vec![BooleanExpressionSymbol::Restriction(
dscp_runtime_types::Restriction::None,
)]
.try_into()
.unwrap(),
}];
let result = transform_to_json(&processes, false);

assert!(result.is_ok());
assert_eq!(
result.unwrap(),
r#"[{"name":"test","program":[{"Restriction":"None"}],"version":1}]"#.to_owned()
);
}

#[test]
fn transforms_name_multiple_process() {
let processes = vec![
Expand All @@ -89,7 +114,7 @@ mod tests {
.unwrap(),
},
];
let result = make_pretty_processes(&processes);
let result = transform_to_json(&processes, true);

assert!(result.is_ok());
assert_eq!(
Expand Down Expand Up @@ -132,7 +157,7 @@ mod tests {
.try_into()
.unwrap(),
}];
let result = make_pretty_processes(&processes);
let result = transform_to_json(&processes, true);

assert!(result.is_ok());
assert_eq!(
Expand Down
2 changes: 1 addition & 1 deletion tools/lang/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl fmt::Display for CompilationStage {
}
}

#[derive(PartialEq)]
#[derive(PartialEq, thiserror::Error)]
pub struct CompilationError {
pub(crate) stage: CompilationStage,
pub(crate) exit_code: i32,
Expand Down
7 changes: 7 additions & 0 deletions tools/lang/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pub mod cli;

mod ast;
mod compiler;
mod convert;
mod errors;
mod parser;
9 changes: 4 additions & 5 deletions tools/lang/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
pub mod ast;
pub mod compiler;
pub mod errors;
pub mod parser;

mod ast;
mod cli;
mod compiler;
mod convert;
mod errors;
mod parser;

fn main() -> ! {
let result = cli::Cli::new().run();
Expand Down

0 comments on commit d120911

Please sign in to comment.