Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions examples/parts/arrays.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use serde::{
Deserialize,
Serialize,
};
use serde::{Deserialize, Serialize};
use serde_json::json;
use toon_format::encode_default;

Expand Down
5 changes: 1 addition & 4 deletions examples/parts/arrays_of_arrays.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use serde::{
Deserialize,
Serialize,
};
use serde::{Deserialize, Serialize};
use serde_json::json;
use toon_format::encode_default;

Expand Down
5 changes: 1 addition & 4 deletions examples/parts/decode_strict.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use serde_json::Value;
use toon_format::{
decode,
DecodeOptions,
};
use toon_format::{decode, DecodeOptions};

pub fn decode_strict() {
// Malformed: header says 2 rows, but only 1 provided
Expand Down
6 changes: 1 addition & 5 deletions examples/parts/delimiters.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
use serde_json::json;
use toon_format::{
encode,
Delimiter,
EncodeOptions,
};
use toon_format::{encode, Delimiter, EncodeOptions};

pub fn delimiters() {
let data = json!({
Expand Down
5 changes: 1 addition & 4 deletions examples/parts/mixed_arrays.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use serde::{
Deserialize,
Serialize,
};
use serde::{Deserialize, Serialize};
use serde_json::json;
use toon_format::encode_default;

Expand Down
5 changes: 1 addition & 4 deletions examples/parts/objects.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use serde::{
Deserialize,
Serialize,
};
use serde::{Deserialize, Serialize};
use serde_json::json;
use toon_format::encode_default;

Expand Down
15 changes: 3 additions & 12 deletions examples/parts/round_trip.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
use serde::{
Deserialize,
Serialize,
};
use serde_json::{
json,
Value,
};
use toon_format::{
decode_default,
encode_default,
};
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use toon_format::{decode_default, encode_default};

#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct Product {
Expand Down
10 changes: 2 additions & 8 deletions examples/parts/structs.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
use serde::{
Deserialize,
Serialize,
};
use toon_format::{
decode_default,
encode_default,
};
use serde::{Deserialize, Serialize};
use toon_format::{decode_default, encode_default};

#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct User {
Expand Down
5 changes: 1 addition & 4 deletions examples/parts/tabular.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use serde::{
Deserialize,
Serialize,
};
use serde::{Deserialize, Serialize};
use serde_json::json;
use toon_format::encode_default;

Expand Down
29 changes: 5 additions & 24 deletions src/cli/main.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,17 @@
use std::{
fs,
io::{
self,
Read,
Write,
},
path::{
Path,
PathBuf,
},
io::{self, Read, Write},
path::{Path, PathBuf},
};

use anyhow::{
bail,
Context,
Result,
};
use anyhow::{bail, Context, Result};
use clap::Parser;
use comfy_table::Table;
use serde::Serialize;
use tiktoken_rs::cl100k_base;
use toon_format::{
decode,
encode,
types::{
DecodeOptions,
Delimiter,
EncodeOptions,
Indent,
KeyFoldingMode,
PathExpansionMode,
},
decode, encode,
types::{DecodeOptions, Delimiter, EncodeOptions, Indent, KeyFoldingMode, PathExpansionMode},
};

#[derive(Parser, Debug)]
Expand Down
10 changes: 2 additions & 8 deletions src/decode/expansion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@ use indexmap::IndexMap;

use crate::{
constants::QUOTED_KEY_MARKER,
types::{
is_identifier_segment,
JsonValue as Value,
PathExpansionMode,
ToonError,
ToonResult,
},
types::{is_identifier_segment, JsonValue as Value, PathExpansionMode, ToonError, ToonResult},
};

pub fn should_expand_key(key: &str, mode: PathExpansionMode) -> Option<Vec<String>> {
Expand Down Expand Up @@ -90,7 +84,7 @@ pub fn deep_merge_value(
}
} else {
target.insert(first_key.clone(), Value::Object(IndexMap::new()));
match target.get_mut(first_key).unwrap() {
match target.get_mut(first_key).expect("key was just inserted") {
Value::Object(obj) => obj,
_ => unreachable!(),
}
Expand Down
5 changes: 1 addition & 4 deletions src/decode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ pub mod validation;

use serde_json::Value;

use crate::types::{
DecodeOptions,
ToonResult,
};
use crate::types::{DecodeOptions, ToonResult};

/// Decode a TOON string into any deserializable type.
///
Expand Down
30 changes: 5 additions & 25 deletions src/decode/parser.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,12 @@
use serde_json::{
Map,
Number,
Value,
};
use serde_json::{Map, Number, Value};

use crate::{
constants::{
KEYWORDS,
MAX_DEPTH,
QUOTED_KEY_MARKER,
},
constants::{KEYWORDS, MAX_DEPTH, QUOTED_KEY_MARKER},
decode::{
scanner::{
Scanner,
Token,
},
scanner::{Scanner, Token},
validation,
},
types::{
DecodeOptions,
Delimiter,
ErrorContext,
ToonError,
ToonResult,
},
types::{DecodeOptions, Delimiter, ErrorContext, ToonError, ToonResult},
utils::validation::validate_depth,
};

Expand Down Expand Up @@ -1441,10 +1424,7 @@ mod tests {

#[test]
fn test_round_trip_parentheses() {
use crate::{
decode::decode_default,
encode::encode_default,
};
use crate::{decode::decode_default, encode::encode_default};

let original = json!({
"message": "Mostly Functions (3 of 3)",
Expand Down
27 changes: 15 additions & 12 deletions src/decode/scanner.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
use crate::types::{
Delimiter,
ToonError,
ToonResult,
};
use crate::types::{Delimiter, ToonError, ToonResult};

/// Tokens produced by the scanner during lexical analysis.
#[derive(Debug, Clone, PartialEq)]
Expand Down Expand Up @@ -352,9 +348,10 @@ impl Scanner {

// Leading zeros like "05" are strings, but "0", "0.5", "-0" are numbers
if s.starts_with('0') && s.len() > 1 {
let second_char = s.chars().nth(1).unwrap();
if second_char.is_ascii_digit() {
return Ok(Token::String(s.to_string(), false));
if let Some(second_char) = s.chars().nth(1) {
if second_char.is_ascii_digit() {
return Ok(Token::String(s.to_string(), false));
}
}
}

Expand Down Expand Up @@ -458,12 +455,18 @@ impl Scanner {
_ => {}
}

if trimmed.starts_with('-') || trimmed.chars().next().unwrap().is_ascii_digit() {
if trimmed.starts_with('-')
|| trimmed
.chars()
.next()
.is_some_and(|c| c.is_ascii_digit())
{
// Leading zeros like "05" are strings
if trimmed.starts_with('0') && trimmed.len() > 1 {
let second_char = trimmed.chars().nth(1).unwrap();
if second_char.is_ascii_digit() {
return Ok(Token::String(trimmed.to_string(), false));
if let Some(second_char) = trimmed.chars().nth(1) {
if second_char.is_ascii_digit() {
return Ok(Token::String(trimmed.to_string(), false));
}
}
}

Expand Down
5 changes: 1 addition & 4 deletions src/decode/validation.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use crate::types::{
ToonError,
ToonResult,
};
use crate::types::{ToonError, ToonResult};

/// Validate that array length matches expected value.
pub fn validate_array_length(expected: usize, actual: usize) -> ToonResult<()> {
Expand Down
6 changes: 1 addition & 5 deletions src/encode/folding.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
use crate::types::{
is_identifier_segment,
JsonValue as Value,
KeyFoldingMode,
};
use crate::types::{is_identifier_segment, JsonValue as Value, KeyFoldingMode};

/// Result of chain analysis for folding.
pub struct FoldableChain {
Expand Down
16 changes: 3 additions & 13 deletions src/encode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,9 @@ use indexmap::IndexMap;
use crate::{
constants::MAX_DEPTH,
types::{
EncodeOptions,
IntoJsonValue,
JsonValue as Value,
KeyFoldingMode,
ToonError,
ToonResult,
},
utils::{
format_canonical_number,
normalize,
validation::validate_depth,
QuotingContext,
EncodeOptions, IntoJsonValue, JsonValue as Value, KeyFoldingMode, ToonError, ToonResult,
},
utils::{format_canonical_number, normalize, validation::validate_depth, QuotingContext},
};

/// Encode any serializable value to TOON format.
Expand Down Expand Up @@ -225,7 +215,7 @@ fn write_object_impl(
writer.write_newline()?;
}

let value = &obj[*key];
let value = obj.get(*key).expect("key exists in field list");

// Check if this key-value pair can be folded (v1.5 feature)
// Don't fold if any sibling key is a dotted path starting with this key
Expand Down
12 changes: 2 additions & 10 deletions src/encode/writer.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
use crate::{
types::{
Delimiter,
EncodeOptions,
ToonResult,
},
types::{Delimiter, EncodeOptions, ToonResult},
utils::{
string::{
is_valid_unquoted_key,
needs_quoting,
quote_string,
},
string::{is_valid_unquoted_key, needs_quoting, quote_string},
QuotingContext,
},
};
Expand Down
Loading
Loading