Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rust format rules #19

Merged
merged 1 commit into from
Feb 2, 2024
Merged
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
2 changes: 2 additions & 0 deletions tests/omni_lock_rust/rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
max_width = 120
use_small_heuristics = "Max"
12 changes: 3 additions & 9 deletions tests/omni_lock_rust/src/blake2b.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,15 @@ pub const PERSONALIZATION_OTX: &[u8] = b"ckb-tcob-otxhash";

/// return a blake2b instance with personalization for SighashAll
pub fn new_sighash_all_blake2b() -> Blake2b {
Blake2bBuilder::new(32)
.personal(PERSONALIZATION_SIGHASH_ALL)
.build()
Blake2bBuilder::new(32).personal(PERSONALIZATION_SIGHASH_ALL).build()
}

/// return a blake2b instance with personalization for SighashAllOnly
pub fn new_sighash_all_only_blake2b() -> Blake2b {
Blake2bBuilder::new(32)
.personal(PERSONALIZATION_SIGHASH_ALL_ONLY)
.build()
Blake2bBuilder::new(32).personal(PERSONALIZATION_SIGHASH_ALL_ONLY).build()
}

/// return a blake2b instance with personalization for OTX
pub fn new_otx_blake2b() -> Blake2b {
Blake2bBuilder::new(32)
.personal(PERSONALIZATION_OTX)
.build()
Blake2bBuilder::new(32).personal(PERSONALIZATION_OTX).build()
}
43 changes: 12 additions & 31 deletions tests/omni_lock_rust/src/omni_lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ impl ::core::fmt::Display for Auth {
}
impl ::core::default::Default for Auth {
fn default() -> Self {
let v: Vec<u8> = vec![
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
];
let v: Vec<u8> = vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
Auth::new_unchecked(v.into())
}
}
Expand Down Expand Up @@ -436,8 +434,7 @@ impl molecule::prelude::Builder for AuthBuilder {
}
fn build(&self) -> Self::Entity {
let mut inner = Vec::with_capacity(self.expected_length());
self.write(&mut inner)
.unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
self.write(&mut inner).unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
Auth::new_unchecked(inner.into())
}
}
Expand Down Expand Up @@ -472,8 +469,8 @@ impl ::core::fmt::Display for Identity {
impl ::core::default::Default for Identity {
fn default() -> Self {
let v: Vec<u8> = vec![
37, 0, 0, 0, 12, 0, 0, 0, 33, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 4, 0, 0, 0,
37, 0, 0, 0, 12, 0, 0, 0, 33, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0,
0, 0,
];
Identity::new_unchecked(v.into())
}
Expand Down Expand Up @@ -538,9 +535,7 @@ impl molecule::prelude::Entity for Identity {
::core::default::Default::default()
}
fn as_builder(self) -> Self::Builder {
Self::new_builder()
.identity(self.identity())
.proofs(self.proofs())
Self::new_builder().identity(self.identity()).proofs(self.proofs())
}
}
#[derive(Clone, Copy)]
Expand Down Expand Up @@ -680,9 +675,7 @@ impl molecule::prelude::Builder for IdentityBuilder {
type Entity = Identity;
const NAME: &'static str = "IdentityBuilder";
fn expected_length(&self) -> usize {
molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
+ self.identity.as_slice().len()
+ self.proofs.as_slice().len()
molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) + self.identity.as_slice().len() + self.proofs.as_slice().len()
}
fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
Expand All @@ -701,8 +694,7 @@ impl molecule::prelude::Builder for IdentityBuilder {
}
fn build(&self) -> Self::Entity {
let mut inner = Vec::with_capacity(self.expected_length());
self.write(&mut inner)
.unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
self.write(&mut inner).unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
Identity::new_unchecked(inner.into())
}
}
Expand Down Expand Up @@ -851,21 +843,14 @@ impl molecule::prelude::Builder for IdentityOptBuilder {
type Entity = IdentityOpt;
const NAME: &'static str = "IdentityOptBuilder";
fn expected_length(&self) -> usize {
self.0
.as_ref()
.map(|ref inner| inner.as_slice().len())
.unwrap_or(0)
self.0.as_ref().map(|ref inner| inner.as_slice().len()).unwrap_or(0)
}
fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
self.0
.as_ref()
.map(|ref inner| writer.write_all(inner.as_slice()))
.unwrap_or(Ok(()))
self.0.as_ref().map(|ref inner| writer.write_all(inner.as_slice())).unwrap_or(Ok(()))
}
fn build(&self) -> Self::Entity {
let mut inner = Vec::with_capacity(self.expected_length());
self.write(&mut inner)
.unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
self.write(&mut inner).unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
IdentityOpt::new_unchecked(inner.into())
}
}
Expand Down Expand Up @@ -970,10 +955,7 @@ impl molecule::prelude::Entity for OmniLockWitnessLock {
::core::default::Default::default()
}
fn as_builder(self) -> Self::Builder {
Self::new_builder()
.signature(self.signature())
.omni_identity(self.omni_identity())
.preimage(self.preimage())
Self::new_builder().signature(self.signature()).omni_identity(self.omni_identity()).preimage(self.preimage())
}
}
#[derive(Clone, Copy)]
Expand Down Expand Up @@ -1151,8 +1133,7 @@ impl molecule::prelude::Builder for OmniLockWitnessLockBuilder {
}
fn build(&self) -> Self::Entity {
let mut inner = Vec::with_capacity(self.expected_length());
self.write(&mut inner)
.unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
self.write(&mut inner).unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
OmniLockWitnessLock::new_unchecked(inner.into())
}
}
Loading