Skip to content

Commit

Permalink
Merge pull request #46 from odarbelaeze/clippy-this-up
Browse files Browse the repository at this point in the history
Clippy this up
  • Loading branch information
odarbelaeze committed Apr 3, 2019
2 parents 1123155 + a8ea2aa commit ebd6437
Show file tree
Hide file tree
Showing 17 changed files with 185 additions and 231 deletions.
34 changes: 13 additions & 21 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,16 @@ rust:
- stable
- beta

before_deploy:
- cargo package

env:
global:
- secure: "NHQ+pHx+XHghzKwA8UL7vbNYeu43IPHWCMUpeD4hJ57iFvCfqDPQ++a1cOAdS0XlFFxvALR7S27MJhXzCd641X/+Ns5inL/IvPLjBzSBx8BjsDz1wgY9fLDdfuSsPndKJz4cNiRFxPELnnfWFTCQUhP8j+S3LID7qerC+ccYxnR+d2CnTFoiMUjXj+zeatuvP4faBT0TOQALSwsPlZEMDT//zl3FI1+k9HhwCSo1mM/EvvQQwe7i44BvKEdQvSYhIcO/2HAmDmMGmBZRi/Ft4LfLHSF+aJtBQKMIZn+saTX8awFD5iEnei679H4i8IAqm9Y+/ETwQKBPNcZ+3Jc8V3Lxa2YiWy/uq3xskRrYuyPLL/0j8dGlROtgUzthKxYtx9y5wC0QWSRL9r8VhcXWSbMv7IAC5AwNVET7nt+qtREoRWx3tJvsWeDkNgmn7nxcQrV5wF0XxpFOaNTtsMHCIXAomaPYLQg5TUiMkU7PWWnvU/PUyMukz4eRsvAWrPdO9ZsTkqj7c6R/ipjxhYAEHGpj2D4CuckacCf/KkvaWIkcIgMPLM9fuCWszNOAkVA+kciISwEmLI3bL2/kzyDULYMEsd+qJWpJZqvwMlV//q9eE6kS740qY6oBcKhGD1ptRMuajc+Cj3YDmFz5Ff05ec+VGoCg87jUlQoY+M8fiYI="

deploy:
provider: releases
api_key:
secure: h5i/VHyodXpOOu3IF+GaqZFXsMdKSrR90Ry5RwUNMJICKNlIpNQ0scChwgyfXzQ1hjBC3qvA71R78upH3BqX1ub1VaBC+tU98u0VqH6FDNHuStwcB9R9MAc+VnHjTkMiWt9yTLcU96IPDsni2VKSrjP1+UBnlJtPyH63R5YVmpisbWs/iyUrsiqKtZx5f5b0qvKAwSlisLcjDgIVrsLaJqa0q3AFGxvjw78U9xnmpjfa2sbI28UmmWzWVhg6C8uGCUVGrqBt9De4iqA+MohHXiy96eHqJN+Oc7rW/kGjXd6kii42eb6q7eFifqr9EJQgsT9Fy6fkE1qNEj3v5CrkW3UX1CivXA14XAWEfmG7fOfla1Ct5VMYrZZXmYTKRX7lTQqssn+d0haELhRIfZdaqpYsU0HQPfiYSNVCMhFZRzbB0uhhkN+EVqRMkhps1RDTBHTcYXCk5TANEiA22PM9imhkM48CTjOywGDr/gymE49syj62qCCusByCUfztpTyzyPDzt+uPVGIIPXdqIOXQMtwowYVdet952X8pC19mB5cpUf5uM/V0GanPZK2O0IgB6TfNScipPn9OzUlw43TxkLAm/URi9Hdvs5aHxffvWRT7kVt5GJHv/CYOjHtA7tq2uHOXdp0rNKxyQY1bQkSzC6m1D3GZpZM8XWu7RLLbcrc=
file: target/package/vegas-lattice-$TRAVIS_TAG.crate
skip_cleanup: true
on:
repo: pcm-ca/vegas-lattice-rs
branch: master
tags: true
condition: "$TRAVIS_RUST_VERSION = stable"

after_deploy:
- cargo publish --token "$CRATESIO_TOKEN"
jobs:
include:
- stage: deploy
rust: stable
script: skip
deploy:
- provider: cargo
token:
secure: Aa0jA4xXa8Bai6M1pO2dsujPM/DVHLHHkczx0eSO7ZiP0zKaaDbCrvy3tzTTStm6AI4NtIU69xIWg4j2JPnqxgHn0ZeYSZDGbe6UjjfzSQmTRqc56zoP3hFcdjVGNqaYE0RNJWxSvzEryE47HLKU51OGJkY1AWJuGG3tQM+mBM0AkPIEl/AlgGBvE+nAqOH7jE3rEsfqPoVrRzgTAuAVp6kOSHL36eGXxcKSIG/TsnQjUFmNxvSVHMizPGFUagjfDWMdW9bwds5Cr2gkvEOJpALEqZ/eg0+sdHosqL0mB/R0WQ7wLkPDe7xS8GBHhIh8GESzbNoN5FGBCpVzMWxgEMtDeBPLEQ9hX4YLfJkb4MueT1G09/yQR6o3XTgft/TumE/b8rXBSsniSMPjpHI+XOKZlQo1GK6G9GR37+ziizR8ltSBsop/sltB79e8UN1HJpgXWK8D5dX4dX/LVjDl3AS8kwI3O0dbWk/B/ZS+QInQc1lUjrsNTMU9tVaZlaZZmEp1obG6s7yeK8LGg1WFvFevSVyz6LhUqMBGV2Gvi++AUvqRMWmPSe3rcUoCziQ4SMHJOpZUFCjbjvZYc08erMl1Z6n+1Hg/Oiuhj/iabjOS/ZT/ZAr5QKIap7VDMpXF2dQANLyGVrtFL93IZ3XPA6VXq8j11U1Xx6y/GeTU5As=
on:
repo: odarbelaeze/vegas-lattice-rs
branch: master
tags: true
15 changes: 7 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
[package]
name = "vegas-lattice"
version = "0.3.0"
version = "0.4.0"
authors = ["Oscar David Arbeláez Echeverri <[email protected]>"]
description = "CLI and library to work with lattices"
documentation = "https://github.com/pcm-ca/vegas-lattice-rs/wiki"
homepage = "https://github.com/pcm-ca/vegas-lattice-rs/wiki"
repository = "https://github.com/pcm-ca/vegas-lattice-rs"
documentation = "https://github.com/odarbelaeze/vegas-lattice-rs/wiki"
homepage = "https://github.com/odarbelaeze/vegas-lattice-rs/wiki"
repository = "https://github.com/odarbelaeze/vegas-lattice-rs"
readme = "README.md"
keywords = ["lattice", "graph", "crystall", "materials"]
categories = ["command-line-utilities", "science", "simulation"]
license-file = "LICENSE.txt"

[badges]
travis-ci = { repository = "pcm-ca/vegas-lattice-rs", branch = "master" }
is-it-maintained-issue-resolution = { repository = "pcm-ca/vegas-lattice-rs" }
is-it-maintained-open-issues = { repository = "pcm-ca/vegas-lattice-rs" }
travis-ci = { repository = "odarbelaeze/vegas-lattice-rs", branch = "master" }
is-it-maintained-issue-resolution = { repository = "odarbelaeze/vegas-lattice-rs" }
is-it-maintained-open-issues = { repository = "odarbelaeze/vegas-lattice-rs" }
maintenance = { status = "actively-developed" }

[dependencies]
serde_json = "1.0"
serde = "1.0"
serde_derive = "1.0"
itertools = "0.6"
docopt = "0.8"
image = "0.15"
rand = "0.3"
4 changes: 2 additions & 2 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2017 Oscar David Arbelaez, PCM Computational Applications
Copyright (c) 2017-2019 Oscar David Arbelaez, PCM Computational Applications

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -18,4 +18,4 @@ 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.
SOFTWARE.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# vegas-lattice-rs

[![Build Status](https://travis-ci.org/pcm-ca/vegas-lattice-rs.svg?branch=master)](https://travis-ci.org/pcm-ca/vegas-lattice-rs)
[![Stories in Ready](https://badge.waffle.io/pcm-ca/vegas-lattice-rs.svg?label=ready&title=Ready)](http://waffle.io/pcm-ca/vegas-lattice-rs)
![Crates.io](https://img.shields.io/crates/v/vegas-lattice.svg)
[![Build Status](https://travis-ci.org/odarbelaeze/vegas-lattice-rs.svg?branch=master)](https://travis-ci.org/odarbelaeze/vegas-lattice-rs)
[![DOI](https://zenodo.org/badge/90330925.svg)](https://zenodo.org/badge/latestdoi/90330925)

A little tool/library to build lattices and samples out of patterns written in
A little tool to build lattices and samples out of patterns written in
[rust].

Take a look at the [wiki].
Expand Down Expand Up @@ -63,6 +63,6 @@ For more explanation in how the different commands work, take a look at the

[crates.io]: https://crates.io/
[rust]: https://www.rust-lang.org/en-US/
[wiki]: https://github.com/pcm-ca/vegas-lattice-rs/wiki
[releases]: https://github.com/pcm-ca/vegas-lattice-rs/releases
[wiki]: https://github.com/odarbelaeze/vegas-lattice-rs/wiki
[releases]: https://github.com/odarbelaeze/vegas-lattice-rs/releases

11 changes: 5 additions & 6 deletions src/alloy.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
// Let's abstract an alloy

use rand::distributions::{Weighted};
use rand::distributions::Weighted;

#[derive(Debug)]
pub struct Alloy {
items: Vec<Weighted<String>>,
}


impl Alloy {
pub fn new(kinds: Vec<&str>, ratios: Vec<u32>) -> Self {
let items: Vec<_> = kinds
.into_iter()
.zip(ratios.into_iter())
.map(|(item, weight)| Weighted {
weight: weight,
item: item.to_owned()
weight,
item: item.to_owned(),
})
.collect();
Self { items: items, }
Self { items }
}

pub fn choices(self) -> Vec<Weighted<String>> {
self.items
}
}
}
49 changes: 31 additions & 18 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,54 @@
use std::fmt;
use std::error::Error as StdError;

use serde_json::Error as SerdeError;

use std::error::Error as StdError;
use std::fmt;
use std::io::Error as IoError;

#[derive(Debug)]
pub enum LatticeError {
IoError(IoError),
JsonParseError(SerdeError),
InconsistentVertices,
NegativeSize,
}

impl StdError for LatticeError {
fn description(&self) -> &str {
match self {
LatticeError::IoError(_) => "There was an error reading your file",
LatticeError::JsonParseError(_) => "There was a problem parsing json",
LatticeError::InconsistentVertices => "These vertices are inconsistent",
LatticeError::NegativeSize => "What are you up to don't give me a negative size",
}
}

fn cause(&self) -> Option<&StdError> {
match self {
LatticeError::IoError(err) => Some(err),
LatticeError::JsonParseError(err) => Some(err),
_ => None,
}
}
}

impl fmt::Display for LatticeError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
LatticeError::IoError(_) => f.write_str("IoError"),
LatticeError::JsonParseError(_) => f.write_str("JsonParseError"),
LatticeError::InconsistentVertices => f.write_str("InconsistentVertices"),
LatticeError::NegativeSize => f.write_str("NegativeSize"),
}
}
}


impl StdError for LatticeError {
fn description(&self) -> &str {
match *self {
LatticeError::JsonParseError(_) => "failed to parse JSON",
LatticeError::InconsistentVertices => "the vertices are inconsistent",
LatticeError::NegativeSize => "a negative size value is hard to grasp",
}
impl From<SerdeError> for LatticeError {
fn from(err: SerdeError) -> Self {
LatticeError::JsonParseError(err)
}
}

fn cause(&self) -> Option<&StdError> {
match self {
&LatticeError::JsonParseError(ref err) => Some(err),
_ => None,
}
impl From<IoError> for LatticeError {
fn from(err: IoError) -> Self {
LatticeError::IoError(err)
}
}
}
57 changes: 32 additions & 25 deletions src/io.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
use serde_json::error::Result;
use serde_json::ser::{Serializer, Formatter};
use serde::ser;
use serde_json::error::Result;
use serde_json::ser::{Formatter, Serializer};
use std::io;


pub struct LatticeFormatter {
current_indent: usize,
has_value: bool,
max_indent: usize,
indent: &'static [u8],
}


impl LatticeFormatter {
pub fn new() -> Self {
LatticeFormatter::with_max_indent(2)
Expand All @@ -21,7 +19,7 @@ impl LatticeFormatter {
LatticeFormatter {
current_indent: 0,
has_value: false,
max_indent: max_indent,
max_indent,
indent: b" ",
}
}
Expand All @@ -33,7 +31,8 @@ impl LatticeFormatter {

#[inline]
fn begin_colletion_item<W: ?Sized>(&mut self, writer: &mut W, first: bool) -> io::Result<()>
where W: io::Write
where
W: io::Write,
{
match (first, self.above_max_indent()) {
(true, false) => {
Expand All @@ -59,11 +58,11 @@ impl Default for LatticeFormatter {
}
}


impl Formatter for LatticeFormatter {
#[inline]
fn begin_array<W: ?Sized>(&mut self, writer: &mut W) -> io::Result<()>
where W: io::Write
where
W: io::Write,
{
self.current_indent += 1;
self.has_value = false;
Expand All @@ -72,7 +71,8 @@ impl Formatter for LatticeFormatter {

#[inline]
fn end_array<W: ?Sized>(&mut self, writer: &mut W) -> io::Result<()>
where W: io::Write
where
W: io::Write,
{
if !self.above_max_indent() && self.has_value {
try!(writer.write_all(b"\n"));
Expand All @@ -84,22 +84,25 @@ impl Formatter for LatticeFormatter {

#[inline]
fn begin_array_value<W: ?Sized>(&mut self, writer: &mut W, first: bool) -> io::Result<()>
where W: io::Write
where
W: io::Write,
{
self.begin_colletion_item(writer, first)
}

#[inline]
fn end_array_value<W: ?Sized>(&mut self, _writer: &mut W) -> io::Result<()>
where W: io::Write
where
W: io::Write,
{
self.has_value = true;
Ok(())
}

#[inline]
fn begin_object<W: ?Sized>(&mut self, writer: &mut W) -> io::Result<()>
where W: io::Write
where
W: io::Write,
{
self.current_indent += 1;
self.has_value = false;
Expand All @@ -108,7 +111,8 @@ impl Formatter for LatticeFormatter {

#[inline]
fn end_object<W: ?Sized>(&mut self, writer: &mut W) -> io::Result<()>
where W: io::Write
where
W: io::Write,
{
if !self.above_max_indent() && self.has_value {
try!(writer.write_all(b"\n"));
Expand All @@ -120,52 +124,55 @@ impl Formatter for LatticeFormatter {

#[inline]
fn begin_object_key<W: ?Sized>(&mut self, writer: &mut W, first: bool) -> io::Result<()>
where W: io::Write
where
W: io::Write,
{
self.begin_colletion_item(writer, first)
}

#[inline]
fn begin_object_value<W: ?Sized>(&mut self, writer: &mut W) -> io::Result<()>
where W: io::Write
where
W: io::Write,
{
writer.write_all(b": ")
}

#[inline]
fn end_object_value<W: ?Sized>(&mut self, _writer: &mut W) -> io::Result<()>
where W: io::Write
where
W: io::Write,
{
self.has_value = true;
Ok(())
}
}


#[inline]
pub fn to_writer_lattice<W, T: ?Sized>(writer: W, value: &T) -> Result<()>
where W: io::Write,
T: ser::Serialize
where
W: io::Write,
T: ser::Serialize,
{
let mut ser = Serializer::with_formatter(writer, LatticeFormatter::new());
try!(value.serialize(&mut ser));
Ok(())
}


#[inline]
pub fn to_vec_lattice<T: ?Sized>(value: &T) -> Result<Vec<u8>>
where T: ser::Serialize
where
T: ser::Serialize,
{
let mut writer = Vec::with_capacity(128);
try!(to_writer_lattice(&mut writer, value));
Ok(writer)
}


#[inline]
pub fn to_string_lattice<T: ?Sized>(value: &T) -> Result<String>
where T: ser::Serialize
where
T: ser::Serialize,
{
let vec = try!(to_vec_lattice(value));
let string = unsafe {
Expand All @@ -175,9 +182,9 @@ pub fn to_string_lattice<T: ?Sized>(value: &T) -> Result<String>
Ok(string)
}


fn indent<W: ?Sized>(wr: &mut W, n: usize, s: &[u8]) -> io::Result<()>
where W: io::Write
where
W: io::Write,
{
for _ in 0..n {
try!(wr.write_all(s));
Expand Down
Loading

0 comments on commit ebd6437

Please sign in to comment.