Skip to content

Commit

Permalink
make column names and states case insensitive
Browse files Browse the repository at this point in the history
  • Loading branch information
daywalker90 committed Jun 29, 2024
1 parent 641a393 commit 3fb8c2b
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 120 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@

- sats values are rounded to the closest integer instead of rounded down

### Fixed

- column names and states are now case insensitive

## [3.3.0] 2024-06-05

### Added
Expand Down
144 changes: 42 additions & 102 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,36 +109,11 @@ fn parse_option(name: &str, value: &serde_json::Value) -> Result<options::Value,
}
}

fn validate_columns_input(input: &str) -> Result<Vec<String>, Error> {
let cleaned_input: String = input
.chars()
.filter(|&c| !c.is_whitespace())
.collect::<String>()
.to_ascii_uppercase();
let split_input: Vec<&str> = cleaned_input.split(',').collect();

let mut uniq = HashSet::new();
for i in &split_input {
if !uniq.insert(i) {
return Err(anyhow!(
"Duplicate entry detected in {}: {}",
OPT_COLUMNS,
i
));
}
}

for i in &split_input {
if !Summary::FIELD_NAMES_AS_ARRAY.contains(i) {
return Err(anyhow!("`{}` not found in valid column names!", i));
}
}

let cleaned_strings: Vec<String> = split_input.into_iter().map(String::from).collect();
Ok(cleaned_strings)
}

fn validate_forwards_columns_input(input: &str) -> Result<Vec<String>, Error> {
fn validate_columns_input(
input: &str,
column_name: &str,
columns: &[&'static str],
) -> Result<Vec<String>, Error> {
let cleaned_input: String = input
.chars()
.filter(|&c| !c.is_whitespace())
Expand All @@ -151,73 +126,15 @@ fn validate_forwards_columns_input(input: &str) -> Result<Vec<String>, Error> {
if !uniq.insert(i) {
return Err(anyhow!(
"Duplicate entry detected in {}: {}",
OPT_FORWARDS_COLUMNS,
column_name,
i
));
}
}

for i in &split_input {
if !Forwards::FIELD_NAMES_AS_ARRAY.contains(i) {
return Err(anyhow!("`{}` not found in valid forwards column names!", i));
}
}

let cleaned_strings: Vec<String> = split_input.into_iter().map(String::from).collect();
Ok(cleaned_strings)
}

fn validate_pays_columns_input(input: &str) -> Result<Vec<String>, Error> {
let cleaned_input: String = input
.chars()
.filter(|&c| !c.is_whitespace())
.collect::<String>()
.to_ascii_lowercase();
let split_input: Vec<&str> = cleaned_input.split(',').collect();

let mut uniq = HashSet::new();
for i in &split_input {
if !uniq.insert(i) {
return Err(anyhow!(
"Duplicate entry detected in {}: {}",
OPT_PAYS_COLUMNS,
i
));
}
}

for i in &split_input {
if !Pays::FIELD_NAMES_AS_ARRAY.contains(i) {
return Err(anyhow!("`{}` not found in valid pays column names!", i));
}
}

let cleaned_strings: Vec<String> = split_input.into_iter().map(String::from).collect();
Ok(cleaned_strings)
}

fn validate_invoices_columns_input(input: &str) -> Result<Vec<String>, Error> {
let cleaned_input: String = input
.chars()
.filter(|&c| !c.is_whitespace())
.collect::<String>()
.to_ascii_lowercase();
let split_input: Vec<&str> = cleaned_input.split(',').collect();

let mut uniq = HashSet::new();
for i in &split_input {
if !uniq.insert(i) {
return Err(anyhow!(
"Duplicate entry detected in {}: {}",
OPT_INVOICES_COLUMNS,
i
));
}
}

for i in &split_input {
if !Invoices::FIELD_NAMES_AS_ARRAY.contains(i) {
return Err(anyhow!("`{}` not found in valid invoices column names!", i));
if !columns.contains(i) {
return Err(anyhow!("`{}` not found in valid {} names!", i, column_name));
}
}

Expand All @@ -230,11 +147,16 @@ fn validate_sort_input(input: &str) -> Result<String, Error> {

let sortable_columns = Summary::FIELD_NAMES_AS_ARRAY
.into_iter()
.filter(|t| t != &"GRAPH_SATS")
.collect::<Vec<&str>>();
.filter(|t| t != &"graph_sats")
.map(|s| s.to_string())
.collect::<Vec<String>>();

if reverse && sortable_columns.contains(&&input[1..]) || sortable_columns.contains(&input) {
Ok(input.to_string())
if reverse && sortable_columns.contains(&(input[1..].to_ascii_lowercase()))
|| sortable_columns.contains(&input.to_ascii_lowercase())
{
Ok(input.to_ascii_uppercase())
} else if input.to_ascii_lowercase().contains("graph_sats") {
Err(anyhow!("Can not sort by `GRAPH_SATS`!"))
} else {
Err(anyhow!(
"Not a valid column name: `{}`. Must be one of: {}",
Expand All @@ -245,7 +167,11 @@ fn validate_sort_input(input: &str) -> Result<String, Error> {
}

fn validate_exclude_states_input(input: &str) -> Result<ExcludeStates, Error> {
let cleaned_input: String = input.chars().filter(|&c| !c.is_whitespace()).collect();
let cleaned_input: String = input
.chars()
.filter(|&c| !c.is_whitespace())
.collect::<String>()
.to_ascii_uppercase();
let split_input: Vec<&str> = cleaned_input.split(',').collect();
if split_input.contains(&"PUBLIC") && split_input.contains(&"PRIVATE") {
return Err(anyhow!("Can only filter `PUBLIC` OR `PRIVATE`, not both."));
Expand Down Expand Up @@ -539,7 +465,11 @@ pub fn get_startup_options(
fn check_option(config: &mut Config, name: &str, value: &options::Value) -> Result<(), Error> {
match name {
n if n.eq(OPT_COLUMNS) => {
config.columns.value = validate_columns_input(value.as_str().unwrap())?;
config.columns.value = validate_columns_input(
value.as_str().unwrap(),
OPT_COLUMNS,
&Summary::FIELD_NAMES_AS_ARRAY,
)?;
}
n if n.eq(OPT_SORT_BY) => {
config.sort_by.value = validate_sort_input(value.as_str().unwrap())?
Expand All @@ -553,8 +483,11 @@ fn check_option(config: &mut Config, name: &str, value: &options::Value) -> Resu
options_value_to_u64(OPT_FORWARDS, value.as_i64().unwrap(), 0, true)?;
}
n if n.eq(OPT_FORWARDS_COLUMNS) => {
config.forwards_columns.value =
validate_forwards_columns_input(value.as_str().unwrap())?;
config.forwards_columns.value = validate_columns_input(
value.as_str().unwrap(),
OPT_FORWARDS_COLUMNS,
&Forwards::FIELD_NAMES_AS_ARRAY,
)?;
}
n if n.eq(OPT_FORWARDS_FILTER_AMT) => {
config.forwards_filter_amt_msat.value =
Expand All @@ -571,7 +504,11 @@ fn check_option(config: &mut Config, name: &str, value: &options::Value) -> Resu
config.pays.value = options_value_to_u64(OPT_PAYS, value.as_i64().unwrap(), 0, true)?
}
n if n.eq(OPT_PAYS_COLUMNS) => {
config.pays_columns.value = validate_pays_columns_input(value.as_str().unwrap())?;
config.pays_columns.value = validate_columns_input(
value.as_str().unwrap(),
OPT_PAYS_COLUMNS,
&Pays::FIELD_NAMES_AS_ARRAY,
)?;
}
n if n.eq(OPT_MAX_DESC_LENGTH) => {
config.max_desc_length.value =
Expand All @@ -582,8 +519,11 @@ fn check_option(config: &mut Config, name: &str, value: &options::Value) -> Resu
options_value_to_u64(OPT_INVOICES, value.as_i64().unwrap(), 0, true)?
}
n if n.eq(OPT_INVOICES_COLUMNS) => {
config.invoices_columns.value =
validate_invoices_columns_input(value.as_str().unwrap())?;
config.invoices_columns.value = validate_columns_input(
value.as_str().unwrap(),
OPT_INVOICES_COLUMNS,
&Invoices::FIELD_NAMES_AS_ARRAY,
)?;
}
n if n.eq(OPT_MAX_LABEL_LENGTH) => {
config.max_label_length.value =
Expand Down
3 changes: 1 addition & 2 deletions src/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl Config {
value: {
Summary::FIELD_NAMES_AS_ARRAY
.into_iter()
.filter(|t| t != &"GRAPH_SATS")
.filter(|t| t != &"graph_sats")
.map(ToString::to_string)
.collect::<Vec<String>>()
},
Expand Down Expand Up @@ -267,7 +267,6 @@ pub struct PeerAvailability {
}

#[derive(Debug, Tabled, FieldNamesAsArray, Serialize)]
#[field_names_as_array(rename_all = "SCREAMING_SNAKE_CASE")]
#[tabled(rename_all = "SCREAMING_SNAKE_CASE")]
pub struct Summary {
#[serde(skip_serializing)]
Expand Down
4 changes: 3 additions & 1 deletion src/tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1167,7 +1167,9 @@ fn format_summary(config: &Config, sumtable: &mut Table) -> Result<(), Error> {
config.style.value.apply(sumtable);
for head in Summary::FIELD_NAMES_AS_ARRAY {
if !config.columns.value.contains(&head.to_string()) {
sumtable.with(Disable::column(ByColumnName::new(head)));
sumtable.with(Disable::column(ByColumnName::new(
head.to_ascii_uppercase(),
)));
}
}

Expand Down
Loading

0 comments on commit 3fb8c2b

Please sign in to comment.