Enforce newline after assignment / clause.

This leads to more consistent formatting across entire Aiken programs.
  Before that commit, only long expressions would be formatted on a
  newline, causing non-consistent formatting and additional reading
  barrier when looking at source code.

  Programs also now take more vertical space, which is better for more
  friendly diffing in version control systems (especially git).
This commit is contained in:
KtorZ
2023-03-16 19:42:57 +01:00
parent bf9297efcf
commit 20f5baffa7
60 changed files with 998 additions and 548 deletions

View File

@@ -25,9 +25,12 @@ pub type ProofItem {
// Function returning a hash of a given Merkle Tree element
pub fn root_hash(self: MerkleTree<a>) -> Hash<Sha2_256, ByteArray> {
when self is {
Empty -> #""
Leaf { hash } -> hash
Node { hash, .. } -> hash
Empty ->
#""
Leaf { hash } ->
hash
Node { hash, .. } ->
hash
}
}
@@ -39,9 +42,12 @@ pub fn is_equal(left: MerkleTree<a>, right: MerkleTree<a>) -> Bool {
/// Function returns a total numbers of leaves in the tree.
pub fn size(self: MerkleTree<a>) -> Int {
when self is {
Empty -> 0
Leaf{..} -> 1
Node { left, right, .. } -> size(left) + size(right)
Empty ->
0
Leaf{..} ->
1
Node { left, right, .. } ->
size(left) + size(right)
}
}
@@ -55,8 +61,10 @@ fn combine_hash(
/// Function that returns whether merkle tree has any elements
pub fn is_empty(self: MerkleTree<a>) -> Bool {
when self is {
Empty -> True
_ -> False
Empty ->
True
_ ->
False
}
}
@@ -67,7 +75,8 @@ fn do_proof(
serialise_fn: fn(a) -> ByteArray,
) -> Option<Proof> {
when self is {
Empty -> None
Empty ->
None
Leaf { hash } ->
if hash == item_hash {
Some(proof)
@@ -75,8 +84,10 @@ fn do_proof(
None
}
Node { left, right, .. } -> {
let rh = root_hash(right)
let lh = root_hash(left)
let rh =
root_hash(right)
let lh =
root_hash(left)
let go_left: Option<Proof> =
do_proof(
left,
@@ -104,7 +115,8 @@ pub fn get_proof(
item: a,
serialise_fn: fn(a) -> ByteArray,
) -> Option<Proof> {
let empty: Proof = []
let empty: Proof =
[]
do_proof(self, sha2_256(serialise_fn(item)), empty, serialise_fn)
}
@@ -115,22 +127,26 @@ fn do_from_list(
serialise_fn: fn(a) -> ByteArray,
) -> MerkleTree<a> {
when items is {
[] -> Empty
[] ->
Empty
[item] -> {
let hashed_item = sha2_256(serialise_fn(item))
let hashed_item =
sha2_256(serialise_fn(item))
Leaf { hash: hashed_item }
}
all -> {
let cutoff: Int = len / 2
let cutoff: Int =
len / 2
let left =
all
|> list.take(cutoff)
|> do_from_list(cutoff, serialise_fn)
|> list.take(cutoff)
|> do_from_list(cutoff, serialise_fn)
let right =
all
|> list.drop(cutoff)
|> do_from_list(len - cutoff, serialise_fn)
let hash = combine_hash(root_hash(left), root_hash(right))
|> list.drop(cutoff)
|> do_from_list(len - cutoff, serialise_fn)
let hash =
combine_hash(root_hash(left), root_hash(right))
Node { hash, left, right }
}
}
@@ -151,19 +167,23 @@ fn do_from_hashes_list(
len: Int,
) -> MerkleTree<a> {
when items is {
[] -> Empty
[hashed_item] -> Leaf { hash: hashed_item }
[] ->
Empty
[hashed_item] ->
Leaf { hash: hashed_item }
all -> {
let cutoff: Int = len / 2
let cutoff: Int =
len / 2
let left =
all
|> list.take(cutoff)
|> do_from_hashes_list(cutoff)
|> list.take(cutoff)
|> do_from_hashes_list(cutoff)
let right =
all
|> list.drop(cutoff)
|> do_from_hashes_list(len - cutoff)
let hash = combine_hash(root_hash(left), root_hash(right))
|> list.drop(cutoff)
|> do_from_hashes_list(len - cutoff)
let hash =
combine_hash(root_hash(left), root_hash(right))
Node { hash, left, right }
}
}
@@ -187,7 +207,8 @@ pub fn member_from_hash(
serialise_fn: fn(a) -> ByteArray,
) -> Bool {
when proof is {
[] -> root_hash == item_hash
[] ->
root_hash == item_hash
[head, ..tail] ->
when head is {
Left { hash: l } ->
@@ -216,7 +237,8 @@ pub fn member(
proof: Proof,
serialise_fn: fn(a) -> ByteArray,
) -> Bool {
let item_hash = sha2_256(serialise_fn(item))
let item_hash =
sha2_256(serialise_fn(item))
member_from_hash(item_hash, root_hash, proof, serialise_fn)
}
@@ -225,12 +247,16 @@ pub fn member_from_tree(
item: a,
serialise_fn: fn(a) -> ByteArray,
) -> Bool {
let proof: Option<Proof> = get_proof(tree, item, serialise_fn)
let rh = root_hash(tree)
let proof: Option<Proof> =
get_proof(tree, item, serialise_fn)
let rh =
root_hash(tree)
when proof is {
Some(p) -> member(item, rh, p, serialise_fn)
None -> False
Some(p) ->
member(item, rh, p, serialise_fn)
None ->
False
}
}
@@ -240,17 +266,25 @@ fn create_string_item_serialise_fn() -> fn(String) -> ByteArray {
}
test from_hashes_list_5() {
let dog = @"dog"
let cat = @"cat"
let mouse = @"mouse"
let horse = @"horse"
let dog =
@"dog"
let cat =
@"cat"
let mouse =
@"mouse"
let horse =
@"horse"
let serialise_fn = create_string_item_serialise_fn()
let serialise_fn =
create_string_item_serialise_fn()
let items = [dog, cat, mouse, horse]
let hashes_items = list.map(items, fn(item) { sha2_256(serialise_fn(item)) })
let items =
[dog, cat, mouse, horse]
let hashes_items =
list.map(items, fn(item) { sha2_256(serialise_fn(item)) })
let mt = from_hashes_list(hashes_items)
let mt =
from_hashes_list(hashes_items)
let left_node_hash =
sha2_256(
@@ -264,7 +298,8 @@ test from_hashes_list_5() {
),
)
let root_hash = sha2_256(bytearray.concat(left_node_hash, right_node_hash))
let root_hash =
sha2_256(bytearray.concat(left_node_hash, right_node_hash))
Node {
hash: root_hash,