replace impl_many! with impl_try_from

This commit is contained in:
elkowar 2020-10-18 13:03:44 +02:00
parent c11cfaa26a
commit f555d2fe80
2 changed files with 13 additions and 13 deletions

View file

@ -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<Self> {
fn try_from($arg: $typ) -> Result<Self> {
$code
}
})*
}
};
}
/// read an scss file, replace all environment variable references within it and

View file

@ -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<PrimitiveValue> 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<i32> for PrimitiveValue {
fn from(x: i32) -> Self {
impl From<bool> for PrimitiveValue {
fn from(x: bool) -> Self {
PrimitiveValue(format!("{}", x))
}
}
impl From<bool> for PrimitiveValue {
fn from(x: bool) -> Self {
PrimitiveValue(format!("{}", x))
impl From<i32> for PrimitiveValue {
fn from(s: i32) -> Self {
PrimitiveValue(s.to_string())
}
}