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

Numerous automated fixes #37

Merged
merged 3 commits into from
Jun 7, 2023
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
13 changes: 6 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ jobs:
override: true
default: true

# Uncomment once linting issues have been fixed
# Enable this for clippy linting.
# - name: Check and Lint Code
# run: cargo clippy -- -D warnings
- name: Run cargo fmt
run: cargo fmt --check

- name: Check and Lint Code
run: cargo clippy
#run: cargo clippy -- -D warnings

- name: Run cargo check
run: cargo check --locked
Expand All @@ -44,6 +46,3 @@ jobs:

- name: Run cargo test
run: cargo test

- name: Run cargo fmt
run: cargo fmt --check
29 changes: 15 additions & 14 deletions tezos-contract/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,28 +184,29 @@ impl ParametersValueConstructor for Type {
}
match self {
Self::Pair(pair) => {
let args = (&pair.types)
let args = pair
.types
.iter()
.map(|pair_type| pair_type.construct_parameter_value(arguments))
.collect::<Result<Vec<_>>>()?;
return Ok(PrimitiveApplication::new(
Ok(PrimitiveApplication::new(
Primitive::Data(DataPrimitive::Pair).into(),
Some(args),
None,
)
.into());
.into())
}
Self::List(list) => {
let value = (&list.r#type).construct_parameter_value(arguments)?;
let value = list.r#type.construct_parameter_value(arguments)?;
Ok(Micheline::Sequence(vec![value].into()))
}
Self::Set(set) => {
let value = (&set.r#type).construct_parameter_value(arguments)?;
let value = set.r#type.construct_parameter_value(arguments)?;
Ok(Micheline::Sequence(vec![value].into()))
}
Self::Map(map) => {
let key = (&map.key_type).construct_parameter_value(arguments)?;
let value = (&map.value_type).construct_parameter_value(arguments)?;
let key = map.key_type.construct_parameter_value(arguments)?;
let value = map.value_type.construct_parameter_value(arguments)?;
Ok(Micheline::Sequence(
vec![PrimitiveApplication::new(
Primitive::Data(DataPrimitive::Elt).into(),
Expand All @@ -217,8 +218,8 @@ impl ParametersValueConstructor for Type {
))
}
Type::BigMap(big_map) => {
let key = (&big_map.key_type).construct_parameter_value(arguments)?;
let value = (&big_map.value_type).construct_parameter_value(arguments)?;
let key = big_map.key_type.construct_parameter_value(arguments)?;
let value = big_map.value_type.construct_parameter_value(arguments)?;
Ok(Micheline::Sequence(
vec![PrimitiveApplication::new(
Primitive::Data(DataPrimitive::Elt).into(),
Expand Down Expand Up @@ -342,7 +343,7 @@ impl CompatibleWith<Type> for DataSequence {
let element_type = &value.r#type;
self.values()
.iter()
.all(|item| item.is_compatible_with(&element_type))
.all(|item| item.is_compatible_with(element_type))
}
Type::Pair(_) => {
let pair: Data = Pair::new(self.clone().into_values()).into();
Expand All @@ -360,13 +361,13 @@ impl CompatibleWith<Type> for Map {
let key = &*item.key;
let value = &*item.value;

return key.is_compatible_with(key_type) && value.is_compatible_with(value_type);
key.is_compatible_with(key_type) && value.is_compatible_with(value_type)
})
}

match value {
Type::Map(map) => check(self, &*map.key_type, &*map.value_type),
Type::BigMap(map) => check(self, &*map.key_type, &*map.value_type),
Type::Map(map) => check(self, &map.key_type, &map.value_type),
Type::BigMap(map) => check(self, &map.key_type, &map.value_type),
_ => false,
}
}
Expand Down Expand Up @@ -476,7 +477,7 @@ impl<HttpClient: Http + Sync> ContractFetcher for TezosRpc<HttpClient> {
.code
.values()
.iter()
.nth(0)
.next()
.ok_or(Error::InvalidContractScript)?
.clone()
.try_into()?;
Expand Down
8 changes: 4 additions & 4 deletions tezos-contract/src/contract/entrypoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ impl MappedEntrypoints {
} else {
vec![]
};
return Ok(MappedEntrypoints {
Ok(MappedEntrypoints {
parameters_type,
entrypoint_paths,
});
})
}

pub fn get(&self, entrypoint: &Entrypoint) -> Option<&Type> {
Expand Down Expand Up @@ -70,12 +70,12 @@ fn entrypoint_paths(
entries: &mut Vec<(Entrypoint, Vec<EntrypointPathComponent>)>,
) {
if let Type::Or(lhs) = value {
let mut path = current_path.map(|value| value.clone()).unwrap_or(vec![]);
let mut path = current_path.cloned().unwrap_or(vec![]);
path.push(path_component);
let entrypoints = entrypoint_paths(lhs, Some(path));
entries.extend(entrypoints.into_iter());
} else if let Some(name) = value.metadata().field_name() {
let mut path = current_path.map(|value| value.clone()).unwrap_or(vec![]);
let mut path = current_path.cloned().unwrap_or(vec![]);
path.push(path_component);
entries.push((Entrypoint::from_str(name.value_without_prefix()), path));
}
Expand Down
2 changes: 1 addition & 1 deletion tezos-contract/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ mod test {
.header("content-type", "application/json")
.body(include_str!("__TEST_DATA__/contract.json"));
});
let rpc = TezosRpc::new(rpc_url.into());
let rpc = TezosRpc::new(rpc_url);
let contract = rpc.contract_at(contract_address, None).await?;
let partial_transaction = contract.call(
"transfer".into(),
Expand Down
4 changes: 2 additions & 2 deletions tezos-core/src/internal/coder/encoded/address_bytes_coder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl Encoder<Address, Vec<u8>, Error> for AddressBytesCoder {

impl Decoder<Address, [u8], Error> for AddressBytesCoder {
fn decode(value: &[u8]) -> Result<Address> {
let tag = AddressTag::recognize(&value).ok_or(Error::InvalidBytes)?;
let tag = AddressTag::recognize(value).ok_or(Error::InvalidBytes)?;
let bytes = value[tag.value().len()..].to_vec();

match tag {
Expand Down Expand Up @@ -74,7 +74,7 @@ impl AddressTag {
Self::values()
.iter()
.find(|item| bytes.starts_with(item.value()))
.map(|tag| *tag)
.copied()
}

fn values() -> &'static [AddressTag] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl Decoder<ContractAddress, [u8], Error> for ContractAddressBytesCoder {
};
Ok(ContractAddress::from_components(
&contract_hash,
entrypoint.as_ref().map(|x| &**x),
entrypoint.as_deref(),
))
}
}
Expand Down
4 changes: 2 additions & 2 deletions tezos-core/src/internal/coder/encoded/encoded_bytes_coder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ impl EncodedBytesCoder {
);
}

return Err(Error::InvalidBytes);
Err(Error::InvalidBytes)
}

pub fn decode_consuming_with_meta<E: Encoded, CL: ConsumableList<u8>>(
value: &mut CL,
meta: &MetaEncoded,
) -> Result<E> {
let bytes = value.consume_until(meta.bytes_length)?;
Self::decode_with_meta(&bytes, meta)
Self::decode_with_meta(bytes, meta)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub struct ImplicitAddressBytesCoder;

impl Encoder<ImplicitAddress, Vec<u8>, Error> for ImplicitAddressBytesCoder {
fn encode(value: &ImplicitAddress) -> Result<Vec<u8>> {
EncodedGroupBytesCoder::<Self>::encode(&value)
EncodedGroupBytesCoder::<Self>::encode(value)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub struct PublicKeyBytesCoder;

impl Encoder<PublicKey, Vec<u8>, Error> for PublicKeyBytesCoder {
fn encode(value: &PublicKey) -> Result<Vec<u8>> {
EncodedGroupBytesCoder::<Self>::encode(&value)
EncodedGroupBytesCoder::<Self>::encode(value)
}
}

Expand Down Expand Up @@ -49,7 +49,7 @@ impl PublicKeyTag {
Self::values()
.iter()
.find(|item| item.is_valid(bytes))
.map(|item| *item)
.copied()
}
}

Expand Down
2 changes: 1 addition & 1 deletion tezos-core/src/internal/coder/number/integer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl ConsumingDecoder<Int, u8, Error> for IntegerBytesCoder {
};
let result: BigInt = abs * sign;

return Ok(result.into());
Ok(result.into())
}
}

Expand Down
2 changes: 1 addition & 1 deletion tezos-core/src/internal/coder/number/natural.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl NaturalBytesCoder {
if has_next {
return Self::decode_with(value, decoded, shift + 7);
}
return Ok(decoded);
Ok(decoded)
}
}

Expand Down
5 changes: 1 addition & 4 deletions tezos-core/src/internal/consumable_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ pub struct ConsumableBytes<'a> {

impl<'a> ConsumableBytes<'a> {
pub fn new(bytes: &'a [u8]) -> Self {
Self {
bytes: bytes.as_ref(),
position: 0,
}
Self { bytes, position: 0 }
}
}

Expand Down
6 changes: 3 additions & 3 deletions tezos-core/src/internal/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub trait EncodedTag: BytesTag + Sized + Copy {
Self::values()
.iter()
.find(|item| item.is_valid(bytes))
.map(|item| *item)
.copied()
}

fn recognize_consumable(bytes: &[u8]) -> Option<Self>
Expand All @@ -38,7 +38,7 @@ pub trait EncodedTag: BytesTag + Sized + Copy {
Self::values()
.iter()
.find(|item| item.is_valid_consumable(bytes))
.map(|item| *item)
.copied()
}

fn from_encoded<E: Encoded>(value: &E) -> Option<Self>
Expand All @@ -48,6 +48,6 @@ pub trait EncodedTag: BytesTag + Sized + Copy {
Self::values()
.iter()
.find(|item| item.meta() == value.meta())
.map(|item| *item)
.copied()
}
}
2 changes: 1 addition & 1 deletion tezos-core/src/types/encoded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub trait Encoded: Sized {
/// Returns an error of the provided value is not a valid base58 string for the type being created.
fn new(value: String) -> Result<Self>;
/// Returns the base58 string value.
fn into_string(&self) -> String {
fn into_string(self) -> String {
self.value().into()
}
/// Encodes the value to its bytes representation
Expand Down
6 changes: 3 additions & 3 deletions tezos-core/src/types/encoded/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,11 +325,11 @@ impl ContractAddress {
} else {
value
};
return ContractHash::is_valid_bytes(bytes);
ContractHash::is_valid_bytes(bytes)
}

fn split_to_components(value: &str) -> Result<(&str, Option<&str>)> {
let components = value.split("%").collect::<Vec<_>>();
let components = value.split('%').collect::<Vec<_>>();
if components.len() > 2 {
return Err(Error::InvalidContractAddress);
}
Expand Down Expand Up @@ -457,7 +457,7 @@ mod test {
String::from(address),
"KT1QTcAXeefhJ3iXLurRt81WRKdv7YqyYFmo"
);
return Ok(());
Ok(())
}

#[test]
Expand Down
10 changes: 5 additions & 5 deletions tezos-core/src/types/encoded/meta_encoded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl MetaEncoded {
}

pub fn is_valid_prefixed_bytes(&self, value: &[u8]) -> bool {
value.starts_with(&self.versioned_bytes_prefix())
value.starts_with(self.versioned_bytes_prefix())
&& value.len() == self.prefixed_bytes_length()
}

Expand All @@ -59,15 +59,15 @@ impl MetaEncoded {
}

pub fn is_valid_prefixed_consumable_bytes(&self, value: &[u8]) -> bool {
value.starts_with(&self.versioned_bytes_prefix())
value.starts_with(self.versioned_bytes_prefix())
&& value.len() >= (self.bytes_length + self.versioned_bytes_prefix().len())
}

pub fn recognize_base58(value: &str) -> Result<&'static MetaEncoded> {
META_ENCODED_VALUES
.iter()
.find(|item| item.is_valid_base58(value))
.map(|item| *item)
.copied()
.ok_or(Error::InvalidBase58EncodedData {
description: value.into(),
})
Expand All @@ -77,15 +77,15 @@ impl MetaEncoded {
META_ENCODED_VALUES
.iter()
.find(|item| item.is_valid_prefixed_bytes(value))
.map(|item| *item)
.copied()
.ok_or(Error::InvalidBytes)
}

pub fn recognize_consumable_bytes(value: &[u8]) -> Result<&'static MetaEncoded> {
META_ENCODED_VALUES
.iter()
.find(|item| item.is_valid_prefixed_consumable_bytes(value))
.map(|item| *item)
.copied()
.ok_or(Error::InvalidBytes)
}
}
Expand Down
6 changes: 5 additions & 1 deletion tezos-core/src/types/hex_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,17 @@ impl HexString {
Err(Error::InvalidHexString)
}

pub fn len(&self) -> usize {
pub fn len(self) -> usize {
if self.0.starts_with(Self::PREFIX) {
return self.0.len() - Self::PREFIX.len();
}
self.0.len()
}

pub fn is_empty(self) -> bool {
self.len() == 0
}

pub fn len_with_prefix(&self) -> usize {
if self.0.starts_with(Self::PREFIX) {
return self.0.len();
Expand Down
Loading