yerba 0.1.0-x86_64-linux

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/rust/src/error.rs ADDED
@@ -0,0 +1,50 @@
1
+ #[derive(Debug)]
2
+ pub enum YerbaError {
3
+ ParseError(String),
4
+ IoError(std::io::Error),
5
+ PathNotFound(String),
6
+ NotASequence(String),
7
+ IndexOutOfBounds(usize, usize),
8
+ UnknownKeys(Vec<String>),
9
+ ReferenceNotFound(String),
10
+ }
11
+
12
+ impl std::fmt::Display for YerbaError {
13
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
14
+ match self {
15
+ YerbaError::ParseError(msg) => write!(f, "parse error: {}", msg),
16
+ YerbaError::IoError(err) => write!(f, "io error: {}", err),
17
+ YerbaError::PathNotFound(path) => write!(f, "path not found: {}", path),
18
+ YerbaError::NotASequence(path) => write!(f, "not a sequence: {}", path),
19
+
20
+ YerbaError::IndexOutOfBounds(index, length) => {
21
+ write!(f, "index {} out of bounds (length {})", index, length)
22
+ }
23
+
24
+ YerbaError::ReferenceNotFound(reference) => {
25
+ write!(f, "template reference not found: ${{{}}}", reference)
26
+ }
27
+
28
+ YerbaError::UnknownKeys(keys) => {
29
+ let suggestion = keys
30
+ .iter()
31
+ .map(|key| format!("\"{}\"", key))
32
+ .collect::<Vec<_>>()
33
+ .join(", ");
34
+
35
+ write!(
36
+ f,
37
+ "found keys not listed in sort order: {}\n\n Add them to your sort order or Yerbafile:\n {}\n",
38
+ keys.join(", "),
39
+ suggestion
40
+ )
41
+ }
42
+ }
43
+ }
44
+ }
45
+
46
+ impl From<std::io::Error> for YerbaError {
47
+ fn from(err: std::io::Error) -> Self {
48
+ YerbaError::IoError(err)
49
+ }
50
+ }
data/rust/src/json.rs ADDED
@@ -0,0 +1,169 @@
1
+ pub fn yaml_to_json(value: &serde_yaml::Value) -> serde_json::Value {
2
+ match value {
3
+ serde_yaml::Value::Null => serde_json::Value::Null,
4
+ serde_yaml::Value::Bool(boolean) => serde_json::Value::Bool(*boolean),
5
+
6
+ serde_yaml::Value::Number(number) => {
7
+ if let Some(integer) = number.as_i64() {
8
+ serde_json::Value::Number(integer.into())
9
+ } else if let Some(float) = number.as_f64() {
10
+ serde_json::json!(float)
11
+ } else {
12
+ serde_json::Value::String(number.to_string())
13
+ }
14
+ }
15
+
16
+ serde_yaml::Value::String(string) => serde_json::Value::String(string.clone()),
17
+
18
+ serde_yaml::Value::Sequence(sequence) => serde_json::Value::Array(sequence.iter().map(yaml_to_json).collect()),
19
+
20
+ serde_yaml::Value::Mapping(mapping) => {
21
+ let mut map = serde_json::Map::new();
22
+
23
+ for (key, yaml_value) in mapping {
24
+ let json_key = match key {
25
+ serde_yaml::Value::String(string) => string.clone(),
26
+ _ => format!("{:?}", key),
27
+ };
28
+
29
+ map.insert(json_key, yaml_to_json(yaml_value));
30
+ }
31
+
32
+ serde_json::Value::Object(map)
33
+ }
34
+
35
+ serde_yaml::Value::Tagged(tagged) => yaml_to_json(&tagged.value),
36
+ }
37
+ }
38
+
39
+ pub fn resolve_select_field(value: &serde_yaml::Value, field: &str) -> serde_json::Value {
40
+ if !field.contains('.') && !field.contains('[') {
41
+ if let serde_yaml::Value::Mapping(map) = value {
42
+ for (key, yaml_value) in map {
43
+ if let serde_yaml::Value::String(key_string) = key {
44
+ if key_string == field {
45
+ return yaml_to_json(yaml_value);
46
+ }
47
+ }
48
+ }
49
+ }
50
+
51
+ return serde_json::Value::Null;
52
+ }
53
+
54
+ let mut current_values = vec![value.clone()];
55
+
56
+ for segment in parse_select_segments(field) {
57
+ let mut next_values = Vec::new();
58
+
59
+ for current in &current_values {
60
+ if segment.starts_with('[') {
61
+ if let serde_yaml::Value::Sequence(sequence) = current {
62
+ let index = segment
63
+ .strip_prefix('[')
64
+ .and_then(|rest| rest.strip_suffix(']'))
65
+ .and_then(|inner| {
66
+ if inner.is_empty() {
67
+ None
68
+ } else {
69
+ inner.parse::<usize>().ok()
70
+ }
71
+ });
72
+
73
+ match index {
74
+ None => next_values.extend(sequence.iter().cloned()),
75
+ Some(index) => {
76
+ if let Some(item) = sequence.get(index) {
77
+ next_values.push(item.clone());
78
+ }
79
+ }
80
+ }
81
+ }
82
+ } else if let serde_yaml::Value::Mapping(map) = current {
83
+ for (map_key, yaml_value) in map {
84
+ if let serde_yaml::Value::String(key_string) = map_key {
85
+ if key_string == segment {
86
+ next_values.push(yaml_value.clone());
87
+ }
88
+ }
89
+ }
90
+ }
91
+ }
92
+
93
+ current_values = next_values;
94
+ }
95
+
96
+ let used_brackets = field.contains("[]");
97
+
98
+ if current_values.is_empty() {
99
+ if used_brackets {
100
+ serde_json::Value::Array(Vec::new())
101
+ } else {
102
+ serde_json::Value::Null
103
+ }
104
+ } else if current_values.len() == 1 && !used_brackets {
105
+ yaml_to_json(&current_values[0])
106
+ } else {
107
+ serde_json::Value::Array(current_values.iter().map(yaml_to_json).collect())
108
+ }
109
+ }
110
+
111
+ pub fn select_field_key(field: &str) -> String {
112
+ let root = field.split(['.', '[']).next().unwrap_or(field);
113
+
114
+ root.to_string()
115
+ }
116
+
117
+ fn parse_select_segments(field: &str) -> Vec<&str> {
118
+ let mut segments = Vec::new();
119
+ let mut rest = field;
120
+
121
+ while !rest.is_empty() {
122
+ if rest.starts_with('[') {
123
+ if let Some(close) = rest.find(']') {
124
+ segments.push(&rest[..close + 1]);
125
+ rest = &rest[close + 1..];
126
+
127
+ if rest.starts_with('.') {
128
+ rest = &rest[1..];
129
+ }
130
+ } else {
131
+ segments.push(rest);
132
+ break;
133
+ }
134
+ } else {
135
+ let dot_index = rest.find('.');
136
+ let bracket_index = rest.find('[');
137
+
138
+ let split_at = match (dot_index, bracket_index) {
139
+ (Some(dot), Some(bracket)) => Some(dot.min(bracket)),
140
+ (Some(dot), None) => Some(dot),
141
+ (None, Some(bracket)) => Some(bracket),
142
+ (None, None) => None,
143
+ };
144
+
145
+ match split_at {
146
+ Some(index) => {
147
+ let segment = &rest[..index];
148
+
149
+ if !segment.is_empty() {
150
+ segments.push(segment);
151
+ }
152
+
153
+ rest = &rest[index..];
154
+
155
+ if rest.starts_with('.') {
156
+ rest = &rest[1..];
157
+ }
158
+ }
159
+
160
+ None => {
161
+ segments.push(rest);
162
+ break;
163
+ }
164
+ }
165
+ }
166
+ }
167
+
168
+ segments
169
+ }
data/rust/src/lib.rs ADDED
@@ -0,0 +1,22 @@
1
+ mod document;
2
+ mod error;
3
+ mod quote_style;
4
+ mod syntax;
5
+ pub mod yerbafile;
6
+
7
+ pub use document::{Document, FindResult, InsertPosition, SortField};
8
+ pub use error::YerbaError;
9
+ pub use quote_style::QuoteStyle;
10
+ pub use yerbafile::{resolve_template, Variable, Yerbafile};
11
+
12
+ pub fn version() -> &'static str {
13
+ env!("CARGO_PKG_VERSION")
14
+ }
15
+
16
+ pub fn parse(source: &str) -> Result<Document, YerbaError> {
17
+ Document::parse(source)
18
+ }
19
+
20
+ pub fn parse_file(path: impl AsRef<std::path::Path>) -> Result<Document, YerbaError> {
21
+ Document::parse_file(path)
22
+ }