Skip to content

Commit

Permalink
fix: model: improved value kind enum values
Browse files Browse the repository at this point in the history
  • Loading branch information
pamburus committed Mar 21, 2024
1 parent ace2d32 commit a343ae2
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ categories = ["command-line-utilities"]
description = "Utility for viewing json-formatted log files."
keywords = ["cli", "human", "log"]
name = "hl"
version = "0.27.0-beta.4.2"
version = "0.27.0-beta.4.3"
edition = "2021"
build = "build.rs"

Expand Down
4 changes: 2 additions & 2 deletions src/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ impl RecordFormatter {

fn format_message<'a, S: StylingPush<Buf>>(&self, s: &mut S, value: RawValue<'a>) {
match value.kind() {
ValueKind::QuotedString | ValueKind::String => {
ValueKind::String(_) => {
s.element(Element::Message, |s| s.batch(|buf| value.format_as_str(buf)));
}
ValueKind::Number => {
Expand Down Expand Up @@ -352,7 +352,7 @@ impl<'a> FieldFormatter<'a> {
setting: IncludeExcludeSetting,
) {
match value.kind() {
ValueKind::String | ValueKind::QuotedString => {
ValueKind::String(_) => {
s.element(Element::String, |s| {
s.batch(|buf| {
value.format_as_str(buf);
Expand Down
24 changes: 15 additions & 9 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use crate::types::FieldKind;
// ---

pub use level::Level;
use ValueKindString::*;

// ---

Expand All @@ -47,7 +48,7 @@ impl<'a> RawValue<'a> {
return ValueKind::Null;
}
match bytes[0] {
b'"' => ValueKind::QuotedString,
b'"' => ValueKind::String(Quoted),
b'0'..=b'9' | b'-' | b'+' | b'.' => ValueKind::Number,
b'{' => ValueKind::Object,
b'[' => ValueKind::Array,
Expand All @@ -74,15 +75,15 @@ impl<'a> RawValue<'a> {
};

if !value.get().is_empty() && value.get().as_bytes()[0] == b'"' {
ValueKind::QuotedString
ValueKind::String(Quoted)
} else if value.get() == "false" || value.get() == "true" {
ValueKind::Boolean
} else if value.get() == "null" {
ValueKind::Null
} else if looks_like_number() {
ValueKind::Number
} else {
ValueKind::String
ValueKind::String(Unquoted)
}
}
}
Expand Down Expand Up @@ -237,15 +238,20 @@ impl<'a> From<&'a logfmt::raw::RawValue> for RawValue<'a> {

#[derive(Clone, Debug, Copy, PartialEq, Eq)]
pub enum ValueKind {
String,
QuotedString,
String(ValueKindString),
Number,
Object,
Array,
Boolean,
Null,
}

#[derive(Clone, Debug, Copy, PartialEq, Eq)]
pub enum ValueKindString {
Quoted,
Unquoted,
}

// ---

pub struct Record<'a> {
Expand Down Expand Up @@ -671,7 +677,7 @@ impl FieldSettings {
}
Self::Level(i) => {
let value = match value.kind() {
ValueKind::QuotedString => value.parse().ok().unwrap_or_else(|| value.raw_str()),
ValueKind::String(Quoted) => value.parse().ok().unwrap_or_else(|| value.raw_str()),
_ => value.raw_str(),
};
if let Some(level) = ps.level[i].0.get(value) {
Expand Down Expand Up @@ -719,7 +725,7 @@ impl FieldSettings {
*line = value.raw_str();
true
}
ValueKind::String => {
ValueKind::String(Unquoted) => {
if let Some(value) = value.parse().ok() {
*line = value;
true
Expand Down Expand Up @@ -1294,7 +1300,7 @@ impl FieldFilter {
continue;
}
Some(KeyMatch::Full) => {
return self.match_value(Some(v.raw_str()), v.kind() == ValueKind::String);
return self.match_value(Some(v.raw_str()), v.kind() == ValueKind::String(Unquoted));
}
Some(KeyMatch::Partial(subkey)) => {
return self.match_value_partial(subkey, *v);
Expand Down Expand Up @@ -1344,7 +1350,7 @@ impl RecordFilter for FieldFilter {
match self.match_custom_key(*k) {
None => {}
Some(KeyMatch::Full) => {
let escaped = v.kind() == ValueKind::QuotedString;
let escaped = v.kind() == ValueKind::String(Quoted);
if self.match_value(Some(v.raw_str()), escaped) {
return true;
}
Expand Down

0 comments on commit a343ae2

Please sign in to comment.