Skip to content

Commit

Permalink
cmm: Clean up code
Browse files Browse the repository at this point in the history
Signed-off-by: Nick Spinale <nick@nickspinale.com>
  • Loading branch information
nspin committed Nov 3, 2023
1 parent 528e4a6 commit 956e238
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,15 +36,15 @@ impl<P: Policy> Formatter<P> {
pub fn format(&self, table: &UnformattedTable) -> Result<Document, Error> {
let mut state = FormatterState {
formatter: self,
current_path: vec![],
current_path: Path::new(),
};
state.format_top_level(table)
}
}

struct FormatterState<'a, P> {
formatter: &'a Formatter<P>,
current_path: Vec<PathSegment>,
current_path: Path,
}

#[derive(Debug, Clone)]
Expand All @@ -54,16 +54,16 @@ pub enum Error {

#[derive(Debug, Clone)]
pub struct CannotInlineTableError {
path: Vec<PathSegment>,
path: Path,
}

impl CannotInlineTableError {
fn new(path: Vec<PathSegment>) -> Self {
fn new(path: Path) -> Self {
Self { path }
}

pub fn path(&self) -> &[PathSegment] {
&self.path
self.path.as_slice()
}
}

Expand Down Expand Up @@ -95,7 +95,7 @@ impl<'a, P: Policy> FormatterState<'a, P> {

fn format_table_to_inline_table(&mut self, v: &UnformattedTable) -> Result<InlineTable, Error> {
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() {
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ fn generic_regex_from_expr(expr: &Expr) -> GenericRegex<PathSegmentPredicate> {
}
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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Self, RegexError> {
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 {
Expand Down

0 comments on commit 956e238

Please sign in to comment.