Skip to content

Commit

Permalink
refactor(json2yaml): remove multiple-behavior method.
Browse files Browse the repository at this point in the history
  • Loading branch information
enuesaa committed Aug 26, 2023
1 parent 45c8e1c commit 7d93c71
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 21 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
@@ -1,6 +1,6 @@
[package]
name = "jsonwith"
version = "0.0.4"
version = "0.0.5"
edition = "2021"
license = "MIT"

Expand Down
30 changes: 11 additions & 19 deletions src/yaml/render/process_mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ impl MappingProcessor {
}

fn decrement_space(&mut self) {
// todo refactor
if self.spaces > 0 {
self.spaces -= self.indent.clone();
};
Expand Down Expand Up @@ -58,31 +57,18 @@ impl MappingProcessor {
last.enable_empty_dict_blancket();
};
}

fn append_key_or_hyphen(&mut self, line: &mut Line) {
if line.get_kv_path().is_last_index() {
line.enable_hyphen();
} else {
line.set_key(&line.get_kv_path());
};
}
}

impl Processor for MappingProcessor {
fn push(&mut self, line: &Line) {
let mut converted = line.clone();

if line.get_kv_value() != Tokens::MkArray {
self.should_index_if_next_is_array = false;
};

match line.get_kv_value() {
Tokens::MkArray => {
converted.set_indent(self.spaces);
if !self.is_root() {
converted.enable_ln();
};
self.append_key_or_hyphen(&mut converted);
if self.should_index_if_next_is_array {
self.increment_space();
};
Expand All @@ -99,7 +85,6 @@ impl Processor for MappingProcessor {
converted.enable_ln();
self.increment_space();
};
self.append_key_or_hyphen(&mut converted);
}
Tokens::EndDict => {
if self.is_last_start_dict() {
Expand All @@ -111,28 +96,35 @@ impl Processor for MappingProcessor {
converted.set_indent(self.spaces);
converted.set_value(&value);
converted.enable_ln();
self.append_key_or_hyphen(&mut converted);
}
Tokens::Number(value) => {
converted.set_indent(self.spaces);
converted.set_value(&value.to_string());
converted.enable_ln();
self.append_key_or_hyphen(&mut converted);
}
Tokens::Bool(value) => {
converted.set_indent(self.spaces);
converted.set_value(&value.to_string());
converted.enable_ln();
self.append_key_or_hyphen(&mut converted);
}
Tokens::Null => {
converted.set_indent(self.spaces);
converted.set_value("null");
converted.enable_ln();
self.append_key_or_hyphen(&mut converted);
}
};

if line.get_kv_value() != Tokens::MkArray {
self.should_index_if_next_is_array = false;
};
if line.get_kv_value() != Tokens::EndArray && line.get_kv_value() != Tokens::EndDict {
if converted.get_kv_path().is_last_index() {
converted.enable_hyphen();
} else {
converted.set_key(&converted.get_kv_path());
};
};

self.lines.push(converted);
}

Expand Down

0 comments on commit 7d93c71

Please sign in to comment.