From f555d2fe809fd37d49aaead7866c3d9a113c08cf Mon Sep 17 00:00:00 2001 From: elkowar <5300871+elkowar@users.noreply.github.com> Date: Sun, 18 Oct 2020 13:03:44 +0200 Subject: [PATCH] replace impl_many! with impl_try_from --- src/util.rs | 12 ++++++------ src/value.rs | 14 +++++++------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/util.rs b/src/util.rs index 386d142..abd8d7f 100644 --- a/src/util.rs +++ b/src/util.rs @@ -6,20 +6,20 @@ use serde::{Deserialize, Serialize}; use std::{fmt, path::Path}; #[macro_export] -macro_rules! impl_many { - ($trait:ident<$typ:ty> $fn_name:ident{ +macro_rules! impl_try_from { + ($typ:ty { $( - for $for:ty => |$var:ident| $code:expr + for $for:ty => |$arg:ident| $code:expr );*; }) => { - $(impl $trait<$typ> for $for { + $(impl TryFrom<$typ> for $for { type Error = anyhow::Error; - fn $fn_name($var: $typ) -> Result { + fn try_from($arg: $typ) -> Result { $code } })* - } + }; } /// read an scss file, replace all environment variable references within it and diff --git a/src/value.rs b/src/value.rs index 326b929..444aa57 100644 --- a/src/value.rs +++ b/src/value.rs @@ -6,7 +6,7 @@ use regex::Regex; use serde::{Deserialize, Serialize}; use std::{convert::TryFrom, fmt}; -use crate::impl_many; +use crate::impl_try_from; #[derive(Clone, PartialEq, Deserialize, Serialize, derive_more::From)] pub struct PrimitiveValue(String); @@ -32,21 +32,21 @@ impl std::str::FromStr for PrimitiveValue { } } -impl_many!(TryFrom try_from { +impl_try_from!(PrimitiveValue { for String => |x| x.as_string(); for f64 => |x| x.as_f64(); for bool => |x| x.as_bool(); }); -impl From for PrimitiveValue { - fn from(x: i32) -> Self { +impl From for PrimitiveValue { + fn from(x: bool) -> Self { PrimitiveValue(format!("{}", x)) } } -impl From for PrimitiveValue { - fn from(x: bool) -> Self { - PrimitiveValue(format!("{}", x)) +impl From for PrimitiveValue { + fn from(s: i32) -> Self { + PrimitiveValue(s.to_string()) } }