From 0c0c154b66fcb18d0be61652b01d54bf5625400e Mon Sep 17 00:00:00 2001 From: Connor Skees Date: Tue, 7 Jul 2020 21:11:45 -0400 Subject: [PATCH] avoid cloning identifiers for functions --- src/builtin/meta.rs | 10 +++++----- src/common.rs | 5 ++--- src/parse/value/parse.rs | 5 ++--- src/scope.rs | 17 ++++++----------- 4 files changed, 15 insertions(+), 22 deletions(-) diff --git a/src/builtin/meta.rs b/src/builtin/meta.rs index a86c633..6e72e21 100644 --- a/src/builtin/meta.rs +++ b/src/builtin/meta.rs @@ -4,7 +4,7 @@ use codemap::Spanned; use crate::{ args::CallArgs, - common::QuoteKind, + common::{Identifier, QuoteKind}, error::SassResult, parse::Parser, unit::Unit, @@ -153,8 +153,8 @@ fn function_exists(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult) -> SassResult { args.max_args(3)?; - let name = match parser.arg(&mut args, 0, "name")? { - Value::String(s, _) => s, + let name: Identifier = match parser.arg(&mut args, 0, "name")? { + Value::String(s, _) => s.into(), v => { return Err(( format!("$name: {} is not a string.", v.inspect(args.span())?), @@ -193,9 +193,9 @@ fn get_function(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult SassFunction::UserDefined(Box::new(f), name.into()), + Ok(f) => SassFunction::UserDefined(Box::new(f), name), Err(..) => match GLOBAL_FUNCTIONS.get(name.as_str()) { - Some(f) => SassFunction::Builtin(f.clone(), name.into()), + Some(f) => SassFunction::Builtin(f.clone(), name), None => return Err((format!("Function not found: {}", name), args.span()).into()), }, }; diff --git a/src/common.rs b/src/common.rs index ccf472c..578e0f3 100644 --- a/src/common.rs +++ b/src/common.rs @@ -139,9 +139,8 @@ impl Default for Identifier { } impl Identifier { - #[allow(clippy::missing_const_for_fn)] - pub fn into_inner(self) -> String { - self.0 + pub fn as_str(&self) -> &str { + &self.0 } } diff --git a/src/parse/value/parse.rs b/src/parse/value/parse.rs index dd40e9b..d100e57 100644 --- a/src/parse/value/parse.rs +++ b/src/parse/value/parse.rs @@ -255,17 +255,16 @@ impl<'a> Parser<'a> { } let as_ident = Identifier::from(&s); - let ident_as_string = as_ident.clone().into_inner(); let func = match self.scopes.last().get_fn( Spanned { - node: as_ident.clone(), + node: &as_ident, span, }, self.global_scope, ) { Ok(f) => f, Err(_) => { - if let Some(f) = GLOBAL_FUNCTIONS.get(ident_as_string.as_str()) { + if let Some(f) = GLOBAL_FUNCTIONS.get(as_ident.as_str()) { return Ok(IntermediateValue::Value(HigherIntermediateValue::Function( SassFunction::Builtin(f.clone(), as_ident), self.parse_call_args()?, diff --git a/src/scope.rs b/src/scope.rs index da470a0..0496b82 100644 --- a/src/scope.rs +++ b/src/scope.rs @@ -90,22 +90,17 @@ impl Scope { self.mixins.contains_key(&name) || global_scope.mixin_exists_no_global(&name) } - fn get_fn_no_global(&self, name: &Spanned) -> SassResult { - match self.functions.get(&name.node) { + fn get_fn_no_global(&self, name: Spanned<&Identifier>) -> SassResult { + match self.functions.get(name.node) { Some(v) => Ok(v.clone()), None => Err(("Undefined function.", name.span).into()), } } - pub fn get_fn>( - &self, - name: Spanned, - global_scope: &Scope, - ) -> SassResult { - let name = name.map_node(Into::into); - match self.functions.get(&name.node) { + pub fn get_fn(&self, name: Spanned<&Identifier>, global_scope: &Scope) -> SassResult { + match self.functions.get(name.node) { Some(v) => Ok(v.clone()), - None => global_scope.get_fn_no_global(&name), + None => global_scope.get_fn_no_global(name), } } @@ -121,6 +116,6 @@ impl Scope { let name = v.into(); self.functions.contains_key(&name) || global_scope.fn_exists_no_global(&name) - || GLOBAL_FUNCTIONS.contains_key(name.clone().into_inner().as_str()) + || GLOBAL_FUNCTIONS.contains_key(name.as_str()) } }