Lint for the clippy gods in the edn crate (#340)

Signed-off-by: Victor Porof <victor.porof@gmail.com>
This commit is contained in:
Victor Porof 2017-02-22 18:11:05 +01:00 committed by GitHub
parent 0d3b8e4b29
commit bf707acbc3
3 changed files with 20 additions and 21 deletions

View file

@ -38,12 +38,10 @@ impl Value {
/// 2,
/// 3].
fn bracket<'a, A, T, I>(&'a self, allocator: &'a A, open: T, vs: I, close: T) -> pretty::DocBuilder<'a, A>
where A: pretty::DocAllocator<'a>, T: Into<Cow<'a, str>>,
I: IntoIterator<Item=&'a Value>,
{
where A: pretty::DocAllocator<'a>, T: Into<Cow<'a, str>>, I: IntoIterator<Item=&'a Value> {
let open = open.into();
let n = open.len();
let i = vs.into_iter().map(|ref v| v.as_doc(allocator)).intersperse(allocator.space());
let i = vs.into_iter().map(|v| v.as_doc(allocator)).intersperse(allocator.space());
allocator.text(open)
.append(allocator.concat(i).nest(n))
.append(allocator.text(close))
@ -55,22 +53,22 @@ impl Value {
/// readability and limited whitespace expansion.
pub fn as_doc<'a, A>(&'a self, pp: &'a A) -> pretty::DocBuilder<'a, A>
where A: pretty::DocAllocator<'a> {
match self {
&Value::Vector(ref vs) => self.bracket(pp, "[", vs, "]"),
&Value::List(ref vs) => self.bracket(pp, "(", vs, ")"),
&Value::Set(ref vs) => self.bracket(pp, "#{", vs, "}"),
&Value::Map(ref vs) => {
let xs = vs.iter().rev().map(|(ref k, ref v)| k.as_doc(pp).append(pp.space()).append(v.as_doc(pp)).group()).intersperse(pp.space());
match *self {
Value::Vector(ref vs) => self.bracket(pp, "[", vs, "]"),
Value::List(ref vs) => self.bracket(pp, "(", vs, ")"),
Value::Set(ref vs) => self.bracket(pp, "#{", vs, "}"),
Value::Map(ref vs) => {
let xs = vs.iter().rev().map(|(k, v)| k.as_doc(pp).append(pp.space()).append(v.as_doc(pp)).group()).intersperse(pp.space());
pp.text("{")
.append(pp.concat(xs).nest(1))
.append(pp.text("}"))
.group()
}
&Value::NamespacedSymbol(ref v) => pp.text(v.namespace.as_ref()).append("/").append(v.name.as_ref()),
&Value::PlainSymbol(ref v) => pp.text(v.0.as_ref()),
&Value::NamespacedKeyword(ref v) => pp.text(":").append(v.namespace.as_ref()).append("/").append(v.name.as_ref()),
&Value::Keyword(ref v) => pp.text(":").append(v.0.as_ref()),
&Value::Text(ref v) => pp.text("\"").append(v.as_ref()).append("\""),
Value::NamespacedSymbol(ref v) => pp.text(v.namespace.as_ref()).append("/").append(v.name.as_ref()),
Value::PlainSymbol(ref v) => pp.text(v.0.as_ref()),
Value::NamespacedKeyword(ref v) => pp.text(":").append(v.namespace.as_ref()).append("/").append(v.name.as_ref()),
Value::Keyword(ref v) => pp.text(":").append(v.0.as_ref()),
Value::Text(ref v) => pp.text("\"").append(v.as_ref()).append("\""),
_ => pp.text(self.to_string())
}
}

View file

@ -45,8 +45,8 @@ pub enum Value {
Map(BTreeMap<Value, Value>),
}
/// SpannedValue is the parallel to Value but used in ValueAndSpan.
/// Container types have ValueAndSpan children.
/// `SpannedValue` is the parallel to `Value` but used in `ValueAndSpan`.
/// Container types have `ValueAndSpan` children.
#[derive(PartialEq, Eq, Hash, Clone, Debug)]
pub enum SpannedValue {
Nil,
@ -69,7 +69,7 @@ pub enum SpannedValue {
#[derive(PartialEq, Eq, Hash, Clone, Debug)]
pub struct Span(pub usize, pub usize);
/// A wrapper type around SpannedValue and Span, representing some EDN Value
/// A wrapper type around `SpannedValue` and `Span`, representing some EDN value
/// and the parsing offset (start, end) in the original EDN string.
#[derive(PartialEq, Eq, Hash, Clone, Debug)]
pub struct ValueAndSpan {
@ -113,9 +113,9 @@ macro_rules! def_from {
/// like `from_bigint()` where the conversion is optional.
macro_rules! def_from_option {
($name: ident, $out: ty, $kind: path, $t: ty, $( $transform: expr ),* ) => {
pub fn $name<'a>(src: $t) -> Option<$out> {
pub fn $name(src: $t) -> Option<$out> {
$( let src = $transform(src); )*
src.map(|v| $kind(v))
src.map($kind)
}
}
}
@ -277,7 +277,7 @@ macro_rules! def_common_value_methods {
def_into!(into_set, $t::Set, BTreeSet<$tchild>,);
def_into!(into_map, $t::Map, BTreeMap<$tchild, $tchild>,);
def_from_option!(from_bigint, $t, $t::BigInteger, &'a str, |src: &'a str| src.parse::<BigInt>().ok());
def_from_option!(from_bigint, $t, $t::BigInteger, &str, |src: &str| src.parse::<BigInt>().ok());
def_from!(from_float, $t, $t::Float, f64, |src: f64| OrderedFloat::from(src));
def_from!(from_ordered_float, $t, $t::Float, OrderedFloat<f64>,);

View file

@ -1348,6 +1348,7 @@ macro_rules! def_test_into_type {
}
#[test]
#[cfg_attr(feature = "cargo-clippy", allow(float_cmp))]
fn test_is_and_as_type_helper_functions() {
let max_i64 = i64::max_value().to_bigint().unwrap();
let bigger = &max_i64 * &max_i64;