From 622ca8ceefb863ff2f91117caa73d948f38fabdf Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Sat, 18 Jan 2020 19:00:49 -0500 Subject: [PATCH] Remove more instances of `unwrap()` --- src/color.rs | 2 +- src/css.rs | 15 ++++++++++++--- src/function.rs | 5 +---- src/imports.rs | 35 +++++++++++++++++++++-------------- src/main.rs | 41 ++++++++++++++++++++++++++++++----------- src/mixin.rs | 12 +++++++++--- 6 files changed, 74 insertions(+), 36 deletions(-) diff --git a/src/color.rs b/src/color.rs index 2c9ebae..9417b26 100644 --- a/src/color.rs +++ b/src/color.rs @@ -156,9 +156,9 @@ pub enum Color { } impl fmt::UpperHex for Color { + #[allow(clippy::match_same_arms, clippy::many_single_char_names)] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { // I want them all to be on separate lines so doing things with regex or multiple cursors is easier - #[allow(clippy::match_same_arms)] match self { Self::AliceBlue => write!(f, "#F0F8FF"), Self::AntiqueWhite => write!(f, "#FAEBD7"), diff --git a/src/css.rs b/src/css.rs index b91f94e..8732367 100644 --- a/src/css.rs +++ b/src/css.rs @@ -70,16 +70,25 @@ impl Css { match stmt { Stmt::Style(s) => { if self.at_root { - self.blocks[self.idx - 1].push_style(s) + self.blocks + .get_mut(self.idx - 1) + .expect("expected block to exist at root") + .push_style(s) } else { - self.blocks[self.idx + self.inner_rulesets - 1].push_style(s) + self.blocks + .get_mut(self.idx + self.inner_rulesets - 1) + .expect("expected block to exist") + .push_style(s) } } Stmt::MultilineComment(s) => { if self.idx == 0 { self.blocks.push(Toplevel::MultilineComment(s)); } else { - self.blocks[self.idx + self.inner_rulesets - 1].push_comment(s) + self.blocks + .get_mut(self.idx + self.inner_rulesets - 1) + .expect("expected block to exist") + .push_comment(s) } } Stmt::RuleSet(RuleSet { diff --git a/src/function.rs b/src/function.rs index f33d041..4a90ada 100644 --- a/src/function.rs +++ b/src/function.rs @@ -116,10 +116,7 @@ pub fn eat_call_args>(toks: &mut Peekable) -> CallA // } } TokenKind::Symbol(Symbol::CloseParen) => { - args.push(CallArg { - name: name, - val: val, - }); + args.push(CallArg { name, val }); break; } TokenKind::Symbol(Symbol::Comma) => { diff --git a/src/imports.rs b/src/imports.rs index 28d74e0..64a95dd 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -1,34 +1,41 @@ use crate::common::Scope; -use crate::{Stmt, StyleSheet}; +use crate::{SassResult, Stmt, StyleSheet}; use std::ffi::OsStr; use std::path::Path; -pub fn import>(name: P) -> (Vec, Scope) { +pub fn import>(path: P) -> SassResult<(Vec, Scope)> { let mut rules: Vec = Vec::new(); let mut scope = Scope::new(); - let path = name.as_ref().to_path_buf(); - let name = path.file_name().unwrap(); - if path.extension() == Some(OsStr::new(".css")) { + let path_buf = path.as_ref().to_path_buf(); + let name = path_buf.file_name().expect("todo! path ended in `..`"); + if path_buf.extension() == Some(OsStr::new(".css")) { // || name.starts_with("http://") || name.starts_with("https://") { todo!("handle css imports") } - let mut p1 = path.clone(); - p1.push("/index.scss"); - let mut p2 = path.clone(); - p2.push("/_index.scss"); + let mut p1 = path_buf.clone(); + p1.push("index.scss"); + let mut p2 = path_buf.clone(); + p2.push("_index.scss"); let paths = [ - path.with_file_name(format!("{}.scss", name.to_str().unwrap())), - path.with_file_name(format!("_{}.scss", name.to_str().unwrap())), - path, + path_buf.with_file_name(format!( + "{}.scss", + name.to_str().expect("path should be UTF-8") + )), + path_buf.with_file_name(format!( + "_{}.scss", + name.to_str().expect("path should be UTF-8") + )), + path_buf, p1, p2, ]; for name in &paths { if name.is_file() { - let (rules2, scope2) = StyleSheet::export_from_path(name.to_str().unwrap()).unwrap(); + let (rules2, scope2) = + StyleSheet::export_from_path(name.to_str().expect("path should be UTF-8"))?; rules.extend(rules2); scope.merge(scope2); } } - (rules, scope) + Ok((rules, scope)) } diff --git a/src/main.rs b/src/main.rs index a43100b..d12c332 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,7 @@ clippy::restriction, clippy::pedantic, clippy::nursery, - // clippy::cargo + clippy::cargo )] #![deny(missing_debug_implementations)] #![allow( @@ -28,7 +28,7 @@ // todo! handle erroring on styles at the toplevel use std::fmt::{self, Display}; use std::fs; -use std::io::{self, Write, BufWriter, stdout}; +use std::io::{self, stdout, BufWriter, Write}; use std::iter::{Iterator, Peekable}; use std::path::Path; @@ -289,10 +289,18 @@ impl<'a> StyleSheetParser<'a> { rules.push(Stmt::MultilineComment(comment)); } TokenKind::AtRule(AtRule::Import) => { - self.lexer.next(); + let Token { pos, .. } = self + .lexer + .next() + .expect("this must exist because we have already peeked"); devour_whitespace(&mut self.lexer); let mut file_name = String::new(); - match self.lexer.next().unwrap().kind { + match self + .lexer + .next() + .unwrap_or_else(|| self.error(pos, "expected value after @import")) + .kind + { TokenKind::Symbol(Symbol::DoubleQuote) => { while let Some(tok) = self.lexer.next() { if tok.kind == TokenKind::Symbol(Symbol::DoubleQuote) { @@ -311,12 +319,15 @@ impl<'a> StyleSheetParser<'a> { } _ => todo!("expected ' or \" after @import"), } - let Token { kind, pos } = self.lexer.next().unwrap(); + let Token { kind, pos } = self + .lexer + .next() + .expect("this must exist because we have already peeked"); if kind != TokenKind::Symbol(Symbol::SemiColon) { self.error(pos, "expected `;` after @import declaration"); } - let (new_rules, new_scope) = import(file_name); + let (new_rules, new_scope) = import(file_name)?; rules.extend(new_rules); self.global_scope.merge(new_scope); } @@ -399,7 +410,9 @@ fn eat_include>( ) -> Result, (Pos, &'static str)> { toks.next(); devour_whitespace(toks); - let Token { kind, pos } = toks.next().unwrap(); + let Token { kind, pos } = toks + .next() + .expect("this must exist because we have already peeked"); let name = if let TokenKind::Ident(s) = kind { s } else { @@ -444,7 +457,9 @@ fn parse_mixin>( toks: &mut Peekable, scope: Scope, ) -> Result<(String, Mixin), Printer> { - let Token { pos, .. } = toks.next().unwrap(); + let Token { pos, .. } = toks + .next() + .expect("this must exist because we have already peeked"); devour_whitespace(toks); let name = if let Some(Token { kind: TokenKind::Ident(s), @@ -561,7 +576,9 @@ pub(crate) fn eat_expr>( )))); } TokenKind::Variable(_) => { - let tok = toks.next().unwrap(); + let tok = toks + .next() + .expect("this must exist because we have already peeked"); let name = if let TokenKind::Variable(n) = tok.kind { n } else { @@ -584,7 +601,9 @@ pub(crate) fn eat_expr>( } } TokenKind::MultilineComment(_) => { - let tok = toks.next().unwrap(); + let tok = toks + .next() + .expect("this must exist because we have already peeked"); let s = if let TokenKind::MultilineComment(s) = &tok.kind { s } else { @@ -1205,8 +1224,8 @@ mod test_mixins { #[cfg(test)] mod test_imports { use super::*; - use Write; use tempfile::Builder; + use Write; macro_rules! test_import { ($func:ident, $input:literal => $output:literal | $( $name:literal($content:literal) ),*) => { diff --git a/src/mixin.rs b/src/mixin.rs index f24760e..17e9569 100644 --- a/src/mixin.rs +++ b/src/mixin.rs @@ -30,9 +30,15 @@ impl Mixin { if arg.is_named() { todo!("keyword args") } else { - self.scope - .vars - .insert(self.args.0[idx].name.clone(), arg.val.clone()); + self.scope.vars.insert( + self.args + .0 + .get(idx) + .expect("too many args passed to mixin") + .name + .clone(), + arg.val.clone(), + ); } } self