From 956e2382651b16563ceef9a31eadb7749ca9f1e6 Mon Sep 17 00:00:00 2001 From: Nick Spinale Date: Fri, 3 Nov 2023 08:56:41 +0000 Subject: [PATCH] cmm: Clean up code Signed-off-by: Nick Spinale --- .../crates/toml-normalize/src/format.rs | 18 +++++++++--------- .../crates/toml-path-regex/src/lib.rs | 2 +- .../src/path_segment_predicate.rs | 8 ++++---- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/hacking/cargo-manifest-management/crates/toml-normalize/src/format.rs b/hacking/cargo-manifest-management/crates/toml-normalize/src/format.rs index 34c3760d1..afc9427d1 100644 --- a/hacking/cargo-manifest-management/crates/toml-normalize/src/format.rs +++ b/hacking/cargo-manifest-management/crates/toml-normalize/src/format.rs @@ -12,7 +12,7 @@ use toml::value::{ }; use toml_edit::{Array, ArrayOfTables, Document, Formatted, InlineTable, Item, Key, Table, Value}; -use toml_path_regex::PathSegment; +use toml_path_regex::{Path, PathSegment}; pub trait Policy { fn max_width(&self) -> usize; @@ -36,7 +36,7 @@ impl Formatter

{ pub fn format(&self, table: &UnformattedTable) -> Result { let mut state = FormatterState { formatter: self, - current_path: vec![], + current_path: Path::new(), }; state.format_top_level(table) } @@ -44,7 +44,7 @@ impl Formatter

{ struct FormatterState<'a, P> { formatter: &'a Formatter

, - current_path: Vec, + current_path: Path, } #[derive(Debug, Clone)] @@ -54,16 +54,16 @@ pub enum Error { #[derive(Debug, Clone)] pub struct CannotInlineTableError { - path: Vec, + path: Path, } impl CannotInlineTableError { - fn new(path: Vec) -> Self { + fn new(path: Path) -> Self { Self { path } } pub fn path(&self) -> &[PathSegment] { - &self.path + self.path.as_slice() } } @@ -95,7 +95,7 @@ impl<'a, P: Policy> FormatterState<'a, P> { fn format_table_to_inline_table(&mut self, v: &UnformattedTable) -> Result { if self.policy().never_inline_table(self.current_path()) { - return Err(CannotInlineTableError::new(self.current_path().to_vec()).into()); + return Err(CannotInlineTableError::new(self.current_path.clone()).into()); } let mut table = InlineTable::new(); for (k, x) in v.iter() { @@ -214,11 +214,11 @@ impl<'a, P: Policy> FormatterState<'a, P> { } fn current_path(&self) -> &[PathSegment] { - &self.current_path + self.current_path.as_slice() } fn current_key(&self) -> Option<&str> { - self.current_path.last()?.as_key() + self.current_path().last()?.as_key() } fn push(&mut self, seg: PathSegment) { self.current_path.push(seg); diff --git a/hacking/cargo-manifest-management/crates/toml-path-regex/src/lib.rs b/hacking/cargo-manifest-management/crates/toml-path-regex/src/lib.rs index d7b599d33..a6774c444 100644 --- a/hacking/cargo-manifest-management/crates/toml-path-regex/src/lib.rs +++ b/hacking/cargo-manifest-management/crates/toml-path-regex/src/lib.rs @@ -119,7 +119,7 @@ fn generic_regex_from_expr(expr: &Expr) -> GenericRegex { } Expr::Dot => GenericRegex::symbol(PathSegmentPredicate::any()), Expr::KeySymbol(key_regex) => { - GenericRegex::symbol(PathSegmentPredicate::from_key_regex(key_regex)) + GenericRegex::symbol(PathSegmentPredicate::from_key_regex(key_regex).unwrap()) } Expr::IndexSymbol(index_ranges) => { GenericRegex::symbol(PathSegmentPredicate::from_index_ranges(index_ranges)) diff --git a/hacking/cargo-manifest-management/crates/toml-path-regex/src/path_segment_predicate.rs b/hacking/cargo-manifest-management/crates/toml-path-regex/src/path_segment_predicate.rs index a02861931..7b5057530 100644 --- a/hacking/cargo-manifest-management/crates/toml-path-regex/src/path_segment_predicate.rs +++ b/hacking/cargo-manifest-management/crates/toml-path-regex/src/path_segment_predicate.rs @@ -5,7 +5,7 @@ // use rangemap::inclusive_set::RangeInclusiveSet; -use regex::Regex; +use regex::{Error as RegexError, Regex}; use super::generic_regex::Predicate; use super::parse::IndexRange; @@ -30,10 +30,10 @@ impl PathSegmentPredicate { Self::new(Inner::Any) } - pub fn from_key_regex(re: &str) -> Self { + pub fn from_key_regex(re: &str) -> Result { let anchored_re = format!("^{re}$"); // TODO is this sound? - let compiled = Regex::new(&anchored_re).unwrap(); - Self::new(Inner::Key(compiled)) + let compiled = Regex::new(&anchored_re)?; + Ok(Self::new(Inner::Key(compiled))) } pub fn from_index_ranges(ranges: &[IndexRange]) -> Self {