1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
//! This module contains structs that represent JSON objects and parts of JSON
//! objects.
use core::fmt;
use regex::Regex;
use std::ops::Deref;
/// This struct represents a JSONL string being built.
///
/// # Fields
///
/// * `string` - The JSONL string being built.
/// * `clean_re_pattern` - A regular expression pattern used to clean the
/// JSONL string.
pub struct JSONLString {
string: String,
clean_re_pattern: Regex,
}
impl Deref for JSONLString {
type Target = String;
fn deref(&self) -> &Self::Target {
&self.string
}
}
impl JSONLString {
/// Creates a new instance of `JSONLString`.
pub fn new() -> Self {
JSONLString {
string: String::new(),
clean_re_pattern: Regex::new(r"\s{0,}\n\s{0,}").unwrap(),
}
}
/// Adds a character to the `string`.
///
/// # Arguments
///
/// * `c` - A character.
///
/// # Examples
///
/// ```
/// use jsonl_converter::json_object::JSONLString;
///
/// let mut jsonl_string = JSONLString::new();
/// jsonl_string.push_char(&'a');
/// ```
pub fn push_char(&mut self, c: &char) {
self.string.push(*c);
}
/// Adds a string to the `string`.
///
/// # Arguments
///
/// * `s` - A string.
///
/// # Examples
///
/// ```
/// use jsonl_converter::json_object::JSONLString;
///
/// let mut jsonl_string = JSONLString::new();
/// jsonl_string.push_str(&"abc");
/// ```
pub fn push_str(&mut self, s: &str) {
self.string.push_str(s);
}
/// Clears the `string`.
///
/// # Examples
///
/// ```
/// use jsonl_converter::json_object::JSONLString;
///
/// let mut jsonl_string = JSONLString::new();
/// jsonl_string.push_str(&"abc");
/// jsonl_string.clear();
/// ```
pub fn clear(&mut self) {
self.string.clear();
}
}
impl fmt::Display for JSONLString {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let result = self.clean_re_pattern.replace_all(&self.string, "");
write!(
f,
"{}",
result
.trim_start_matches(',')
.trim_end_matches(',')
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_jsonl_string_new_instance_is_empty() {
let jsonl_string = JSONLString::new();
assert_eq!(jsonl_string.string, "");
}
#[test]
fn test_jsonl_string_push_char_adds_char_to_string() {
let mut jsonl_string = JSONLString::new();
jsonl_string.push_char(&'a');
assert_eq!(jsonl_string.string, "a");
}
#[test]
fn test_jsonl_string_push_str_adds_str_to_string() {
let mut jsonl_string = JSONLString::new();
jsonl_string.push_str("abc");
assert_eq!(jsonl_string.string, "abc");
}
#[test]
fn test_jsonl_string_clear_removes_all_chars_from_string() {
let mut jsonl_string = JSONLString::new();
jsonl_string.push_str("abc");
jsonl_string.clear();
assert_eq!(jsonl_string.string, "");
}
#[test]
fn test_jsonl_string_display_trait_impl_returns_string_without_spaces() {
let mut jsonl_string = JSONLString::new();
jsonl_string.push_str(" \n{\"a\": 1}\n\"");
assert_eq!(jsonl_string.to_string(), "{\"a\": 1}\"");
}
#[test]
fn test_jsonl_string_display_removes_leading_comma() {
let mut jsonl_string = JSONLString::new();
jsonl_string.push_str(",\n{\"a\": 1}");
assert_eq!(jsonl_string.to_string(), "{\"a\": 1}");
}
#[test]
fn test_jsonl_len_returns_string_length() {
let mut jsonl_string = JSONLString::new();
jsonl_string.push_str("abc");
assert_eq!(jsonl_string.len(), 3);
}
}