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

Apply rustfmt. #385

Closed
wants to merge 3 commits into from
Closed
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ running `cargo test` and `cargo test --features=rsa_signing`. See
[BUILDING.md](BUILDING.md) for more info about the features flags that are
useful for people hacking on *ring*.

You can also make sure your code will pass `rustfmt`'s checks before committing
by configuring a pre-commit hook. Install `rustfmt` with `cargo install rustfmt`.
You can now `cargo fmt` manually. To set the pre-commit hook, run
`ln -s ../../mk/git-pre-commit-rustfmt-hook.sh .git/hooks/pre-commit`.


Online Automated Testing
Expand Down
48 changes: 32 additions & 16 deletions examples/checkdigest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,20 @@ use ring::*;
use std::error::Error;
use std::io::{Read, Write};


#[cfg_attr(rustfmt, rustfmt_skip)]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rustfmt handles multi line string literals poorly at the moment

fn print_usage(program_name: &str) {
let program_file_name = std::path::Path::new(program_name)
.file_name().unwrap().to_str().unwrap();
.file_name()
.unwrap()
.to_str()
.unwrap();

println!(
"Usage: {} sha256|sha384|sha512 <digest value in hex> <filename>\n\
\n\
On success nothing is output, and 0 is returned.\n\
On failure, an error message is printed, and a non-zero value is returned\n\
On failure, an error message is printed, and a non-zero value is \
returned\n\
\n\
Example:\n\
{} sha256 \
Expand All @@ -37,12 +41,15 @@ fn print_usage(program_name: &str) {
}

fn run(digest_name: &str, expected_digest_hex: &str,
file_path: &std::path::Path) -> Result<(), &'static str> {
file_path: &std::path::Path)
-> Result<(), &'static str> {
let digest_alg = match digest_name {
"sha256" => &digest::SHA256,
"sha384" => &digest::SHA384,
"sha512" => &digest::SHA512,
_ => { return Err("unsupported digest algorithm"); }
_ => {
return Err("unsupported digest algorithm");
},
};

let mut ctx = digest::Context::new(digest_alg);
Expand All @@ -51,8 +58,11 @@ fn run(digest_name: &str, expected_digest_hex: &str,
let mut file = match std::fs::File::open(file_path) {
Ok(file) => file,
// TODO: don't use panic here.
Err(why) => panic!("couldn't open {}: {}", file_path.display(),
why.description())
Err(why) => {
panic!("couldn't open {}: {}",
file_path.display(),
why.description())
},
};

let mut chunk = vec![0u8; 128 * 1024];
Expand All @@ -61,8 +71,11 @@ fn run(digest_name: &str, expected_digest_hex: &str,
Ok(0) => break,
Ok(bytes_read) => ctx.update(&chunk[0..bytes_read]),
// TODO: don't use panic here
Err(why) => panic!("couldn't open {}: {}", file_path.display(),
why.description())
Err(why) => {
panic!("couldn't open {}: {}",
file_path.display(),
why.description())
},
}
}
}
Expand All @@ -71,20 +84,23 @@ fn run(digest_name: &str, expected_digest_hex: &str,

let matched = match from_hex(&expected_digest_hex) {
Ok(expected) => actual_digest.as_ref() == &expected[..],
Err(msg) => panic!("syntactically invalid digest: {} in {}", msg,
&expected_digest_hex),
Err(msg) => {
panic!("syntactically invalid digest: {} in {}",
msg,
&expected_digest_hex)
},
};

match matched {
true => Ok(()),
false => Err("digest mismatch") // TODO: calculated digest.
false => Err("digest mismatch"), // TODO: calculated digest.
}
}

pub fn from_hex(hex_str: &str) -> Result<Vec<u8>, String> {
if hex_str.len() % 2 != 0 {
return Err(
String::from("Hex string does not have an even number of digits"));
return Err(String::from("Hex string does not have an even number of \
digits"));
}

fn from_hex_digit(d: u8) -> Result<u8, String> {
Expand Down Expand Up @@ -113,7 +129,7 @@ fn main() {

if args.iter().any(|arg| arg == "-h") {
print_usage(&args[0]);
return
return;
} else if args.len() < 4 {
print_usage(&args[0]);
std::process::exit(1);
Expand All @@ -124,6 +140,6 @@ fn main() {
Err(s) => {
let _ = writeln!(&mut std::io::stderr(), "{}", s);
std::process::exit(1)
}
},
}
}
5 changes: 5 additions & 0 deletions mk/git-pre-commit-rustfmt-hook.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh

exec 1>&2

cargo fmt -- --write-mode=Diff
42 changes: 42 additions & 0 deletions mk/travis-install-rustfmt.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env bash
#
# Copyright (c) 2016 Marshall Pierce
# Copyright (c) 2016 Pietro Monteiro
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
set -ex

# different versions behave differently even with the same config
RUSTFMT_VERSION=${RUSTFMT_VERSION:-"0.6.3"}

RUSTFMT_INSTALL_PREFIX="${HOME}/rustfmt-${TARGET_X}"

# Check if rustfmt has been cached on travis.
if [[ -f "$RUSTFMT_INSTALL_PREFIX/bin/rustfmt" ]]; then
RUSTFMT_INSTALLED_VERSION=`$RUSTFMT_INSTALL_PREFIX/bin/rustfmt --version | awk '{print $1}'`
if [[ "$RUSTFMT_VERSION" = "$RUSTFMT_INSTALLED_VERSION" ]]; then
echo "Using cached rustfmt version: ${RUSTFMT_INSTALLED_VERSION}"
exit 0
else
rm -rf "$RUSTFMT_INSTALL_PREFIX"
fi
fi

# cargo's version resolution is annoying; force exact version
cargo install --vers "=${RUSTFMT_VERSION}" --root "$RUSTFMT_INSTALL_PREFIX" rustfmt
3 changes: 3 additions & 0 deletions mk/travis.sh
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ arm-linux-androideabi)
;;
esac

./mk/travis-install-rustfmt.sh
"${HOME}/rustfmt-${TARGET_X}/bin/rustfmt" -- --write-mode=Diff

if [[ "$KCOV" == "1" ]]; then
# kcov reports coverage as a percentage of code *linked into the executable*
# (more accurately, code that has debug info linked into the executable), not
Expand Down
Loading