Skip to content

Commit

Permalink
fmt + clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
prozacchiwawa committed Jul 15, 2024
1 parent ae754b8 commit 71de5f8
Show file tree
Hide file tree
Showing 11 changed files with 199 additions and 225 deletions.
40 changes: 25 additions & 15 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,35 @@ use clvmr::Allocator;
use toml::{Table, Value};

use clvm_tools_rs::classic::clvm_tools::clvmc::CompileError;
use clvm_tools_rs::classic::platform::argparse::ArgumentValue;
use clvm_tools_rs::classic::clvm_tools::comp_input::RunAndCompileInputData;
use clvm_tools_rs::classic::platform::argparse::ArgumentValue;
use clvm_tools_rs::compiler::comptypes::CompileErr;
use clvm_tools_rs::compiler::srcloc::Srcloc;

fn do_compile(title: &str, filename: &str) -> Result<(), CompileError> {
let mut allocator = Allocator::new();
let mut arguments: HashMap<String, ArgumentValue> = HashMap::new();
arguments.insert("include".to_string(), ArgumentValue::ArgArray(vec![ArgumentValue::ArgString(None, ".".to_string())]));
arguments.insert(
"include".to_string(),
ArgumentValue::ArgArray(vec![ArgumentValue::ArgString(None, ".".to_string())]),
);

let file_content = fs::read_to_string(filename).map_err(|e| {
CompileErr(Srcloc::start(filename), format!("failed to read {filename}: {e:?}"))
CompileErr(
Srcloc::start(filename),
format!("failed to read {filename}: {e:?}"),
)
})?;

arguments.insert("path_or_code".to_string(), ArgumentValue::ArgString(Some(filename.to_string()), file_content));
arguments.insert(
"path_or_code".to_string(),
ArgumentValue::ArgString(Some(filename.to_string()), file_content),
);

let parsed = RunAndCompileInputData::new(&mut allocator, &arguments).map_err(|e| {
CompileError::Modern(
Srcloc::start("*error*"),
format!("error building chialisp {title}: {e}")
format!("error building chialisp {title}: {e}"),
)
})?;
let mut symbol_table = HashMap::new();
Expand All @@ -37,19 +46,20 @@ fn do_compile(title: &str, filename: &str) -> Result<(), CompileError> {
fn compile_chialisp() -> Result<(), CompileError> {
let srcloc = Srcloc::start("chialisp.toml");
let chialisp_toml_text = fs::read_to_string("chialisp.toml").map_err(|e| {
CompileError::Modern(srcloc.clone(), format!("Error reading chialisp.toml: {e:?}"))
CompileError::Modern(
srcloc.clone(),
format!("Error reading chialisp.toml: {e:?}"),
)
})?;

let chialisp_toml = chialisp_toml_text.parse::<Table>().map_err(|e| {
CompileError::Modern(srcloc, format!("Error parsing chialisp.toml: {e:?}"))
})?;
let chialisp_toml = chialisp_toml_text
.parse::<Table>()
.map_err(|e| CompileError::Modern(srcloc, format!("Error parsing chialisp.toml: {e:?}")))?;

if let Some(compiles) = chialisp_toml.get("compile") {
if let Value::Table(t) = compiles {
for (k,v) in t.iter() {
if let Value::String(s) = v {
do_compile(k, &s)?;
}
if let Some(Value::Table(t)) = chialisp_toml.get("compile") {
for (k, v) in t.iter() {
if let Value::String(s) = v {
do_compile(k, s)?;
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/channel_handler/game.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use clvm_traits::{ClvmEncoder, ToClvm};
use clvmr::{NodePtr, run_program};
use clvmr::{run_program, NodePtr};

use clvm_tools_rs::classic::clvm::sexp::proper_list;

Expand Down Expand Up @@ -65,11 +65,11 @@ impl Game {
.expect("should be an atom");
let required_size_factor = Amount::new(
atom_from_clvm(allocator, template_list[3])
.and_then(|a| u64_from_atom(a))
.and_then(u64_from_atom)
.expect("should be an atom"),
);
let initial_max_move_size = atom_from_clvm(allocator, template_list[4])
.and_then(|a| usize_from_atom(a))
.and_then(usize_from_atom)
.expect("should be an atom");
let initial_validation_program = ValidationProgram::new(allocator, template_list[5]);
let initial_validation_program_hash =
Expand All @@ -82,7 +82,7 @@ impl Game {
};
let initial_state = template_list[7];
let initial_mover_share_proportion = atom_from_clvm(allocator, template_list[8])
.and_then(|a| usize_from_atom(a))
.and_then(usize_from_atom)
.expect("should be an atom");
Ok(Game {
id: game_id,
Expand Down
39 changes: 18 additions & 21 deletions src/channel_handler/game_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ fn run_code(
pub enum TheirTurnResult {
FinalMove(NodePtr),
MakeMove(NodePtr, GameHandler, Vec<u8>),
Slash(Evidence, Aggsig),
Slash(Evidence, Box<Aggsig>),
}

impl GameHandler {
Expand Down Expand Up @@ -206,10 +206,7 @@ impl GameHandler {
inputs.amount.clone(),
(
inputs.last_mover_share.clone(),
(
inputs.last_max_move_size.clone(),
(inputs.entropy.clone(), ()),
),
(inputs.last_max_move_size, (inputs.entropy.clone(), ())),
),
),
)
Expand Down Expand Up @@ -249,27 +246,27 @@ impl GameHandler {
}

let max_move_size =
if let Some(mm) = atom_from_clvm(allocator, pl[4]).and_then(|a| usize_from_atom(a)) {
if let Some(mm) = atom_from_clvm(allocator, pl[4]).and_then(usize_from_atom) {
mm
} else {
return Err(Error::StrErr("bad max move size".to_string()));
};
let mover_share =
if let Some(ms) = atom_from_clvm(allocator, pl[5]).and_then(|a| u64_from_atom(a)) {
Amount::new(ms)
} else {
return Err(Error::StrErr(format!(
"bad share {}",
disassemble(allocator.allocator(), pl[5], None)
)));
};
let mover_share = if let Some(ms) = atom_from_clvm(allocator, pl[5]).and_then(u64_from_atom)
{
Amount::new(ms)
} else {
return Err(Error::StrErr(format!(
"bad share {}",
disassemble(allocator.allocator(), pl[5], None)
)));
};
let message_parser = if pl[7] == allocator.allocator().null() {
None
} else {
Some(MessageHandler::from_nodeptr(pl[7]))
};
let validation_program_hash =
if let Some(h) = atom_from_clvm(allocator, pl[2]).map(|a| Hash::from_slice(a)) {
if let Some(h) = atom_from_clvm(allocator, pl[2]).map(Hash::from_slice) {
h
} else {
return Err(Error::StrErr("bad hash".to_string()));
Expand Down Expand Up @@ -312,7 +309,7 @@ impl GameHandler {
let driver_args = (
inputs.amount.clone(),
(
Node(inputs.last_state.clone()),
Node(inputs.last_state),
(
Node(
allocator
Expand All @@ -322,7 +319,7 @@ impl GameHandler {
(
inputs.new_move.validation_info_hash.clone(),
(
inputs.new_move.basic.max_move_size.clone(),
inputs.new_move.basic.max_move_size,
(inputs.new_move.basic.mover_share.clone(), ()),
),
),
Expand Down Expand Up @@ -362,10 +359,10 @@ impl GameHandler {

if move_type == 0 {
if pl.len() < 2 {
return Err(Error::StrErr(format!(
Err(Error::StrErr(format!(
"bad length for move result {}",
disassemble(allocator.allocator(), run_result, None)
)));
)))
} else if pl.len() < 3 {
Ok(TheirTurnResult::FinalMove(pl[1]))
} else {
Expand All @@ -390,7 +387,7 @@ impl GameHandler {
let sig_bytes = allocator.allocator().atom(pl[2]).to_vec();
Ok(TheirTurnResult::Slash(
Evidence::from_nodeptr(pl[1]),
Aggsig::from_slice(&sig_bytes)?,
Box::new(Aggsig::from_slice(&sig_bytes)?),
))
} else {
Err(Error::StrErr("unknown move result type".to_string()))
Expand Down
Loading

0 comments on commit 71de5f8

Please sign in to comment.