Skip to content

Commit

Permalink
add example and new command
Browse files Browse the repository at this point in the history
  • Loading branch information
SkymanOne committed Mar 24, 2024
1 parent e9298e4 commit aeedb7f
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 4 deletions.
7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@ num-bigint = "0.4"
num-rational = "0.4"
num-traits = "0.2"
algonaut_core = "0.4"
hex = "0.4"
hex = "0.4"
regex = "1.10"
clap = { version ="4.5", features = ["derive"]}
ariadne = { version = "0.4", features = ["auto-color"] }
anyhow = "1.0"
walkdir = "2.5"
7 changes: 6 additions & 1 deletion crates/folidity/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@ version.workspace = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
folidity-parser = { workspace = true }
folidity-parser = { workspace = true }
folidity-semantics = { workspace = true }
clap = { workspace = true }
ariadne = { workspace = true }
anyhow = { workspace = true }
walkdir = { workspace = true }
19 changes: 19 additions & 0 deletions crates/folidity/src/cmd/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use anyhow::Result;
use clap::Subcommand;

use self::new::NewCommand;

mod new;

#[derive(Subcommand)]
pub enum Commands {
New(NewCommand),
}

impl Commands {
pub fn run(&self) -> Result<()> {
match self {
Commands::New(cmd) => cmd.run(),
}
}
}
61 changes: 61 additions & 0 deletions crates/folidity/src/cmd/new.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use anyhow::Result;
use std::{
ffi::OsString,
fs::{
self,
create_dir,
File,
},
io::Write,
path::Path,
};
use walkdir::WalkDir;

use clap::Args;

/// Creates a new templated `folidity` counter project.
/// with a basic contract, README and approval teal code.
#[derive(Args)]
pub struct NewCommand {
/// Path to the new project.
/// If empty, the project will be created in the current dir.
#[clap(value_parser)]
name: Option<OsString>,
}

impl NewCommand {
pub fn run(&self) -> Result<()> {
let out_dir = self.name.clone().unwrap_or(OsString::from("."));
let out_path = Path::new(&out_dir);
if out_path.exists() {
for entry in WalkDir::new(out_path)
.follow_links(true)
.into_iter()
.filter_map(|e| e.ok())
{
let f_name = entry.file_name().to_string_lossy();
let sec = entry.metadata()?.modified()?;

if f_name.ends_with(".fol") && sec.elapsed()?.as_secs() < 86400 {
anyhow::bail!(
"Project with this name already exist in {}",
out_dir.to_str().unwrap()
);
}
}
} else {
create_dir(&out_dir).map(|_| anyhow::anyhow!("Cannot create project directory."))?;
}

let contract_content = include_str!("../../../../examples/counter/counter.fol");
let readme_content = include_str!("../../../../examples/counter/README.md");

let mut contract_file = File::create(Path::new(&out_dir).join("counter.fol"))?;
contract_file.write_all(contract_content.to_string().as_bytes())?;

let mut readme_file = File::create(Path::new(&out_dir).join("README.md"))?;
readme_file.write_all(readme_content.to_string().as_bytes())?;

Ok(())
}
}
21 changes: 20 additions & 1 deletion crates/folidity/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
use clap::Parser;
use cmd::Commands;

mod cmd;

#[derive(Parser)]
#[command(author = env!("CARGO_PKG_AUTHORS"), version = concat!("version ", env!("CARGO_PKG_VERSION")), about = env!("CARGO_PKG_DESCRIPTION"), subcommand_required = true)]
struct Cli {
#[command(subcommand)]
command: Commands,
}

fn main() {
println!("Hello, world!");
let cli = Cli::parse();
match cli.command.run() {
Ok(()) => {}
Err(err) => {
eprintln!("{err:?}");
std::process::exit(1);
}
}
}
2 changes: 1 addition & 1 deletion crates/parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ lalrpop-util = { workspace = true }
thiserror = { workspace = true }
derive-node = { workspace = true }
folidity-diagnostics = { workspace = true }
regex = "*"
regex = { workspace = true }

[build-dependencies] # <-- We added this and everything after!
lalrpop = { workspace = true }
3 changes: 3 additions & 0 deletions examples/counter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Verifiable counter

This is a simple counter smart contract.
40 changes: 40 additions & 0 deletions examples/counter/counter.fol
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
state CounterState {
counter: int,
} st [
# example bounds
counter < 1000,
counter > -1000
]

# This is an constructor.
@init
# Anyone can call this function.
@(any)
fn () initialise() when () -> CounterState {
move CounterState : { 0 };
}

@(any)
fn () incr_by(value: int) when CounterState s -> CounterState
st [
value > 100,
value < 100
] {
let value = s.counter + value;
move CounterState : { value };
}

@(any)
fn () decr_by(value: int) when CounterState s -> CounterState
st [
value > 100,
value < 100
] {
let value = s.counter - value;
move CounterState : { value };
}

@(any)
view(CounterState s) fn int get_value() {
return s.counter;
}
Empty file added examples/flipper/README.md
Empty file.
Empty file added examples/flipper/flipper.fol
Empty file.

0 comments on commit aeedb7f

Please sign in to comment.