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
// Copyright notice and licensing information.
// These lines indicate the copyright of the software and its licensing terms.
// SPDX-License-Identifier: Apache-2.0 OR MIT indicates dual licensing under Apache 2.0 or MIT licenses.
// Copyright © 2023-2024 LibMake. All rights reserved.
//! # Test: Retrieving a Field from a YAML File
//!
//! This is a test that demonstrates how to retrieve a specific field from a YAML file
//! using the `get_yaml_field` function from the `libmake` crate.
//!
//! ## Purpose
//!
//! The purpose of this test is to show how to extract a YAML field (`field_keywords`)
//! from a YAML file located at the specified path (`file_path`). The test specifically
//! handles YAML arrays and converts them into a formatted string.
//!
//! ## Usage
//!
//! To run this test, ensure that you have a valid YAML file at the specified path.
//! The test checks if the file exists and then uses the `get_yaml_field` function
//! to retrieve the specified YAML field (`field_keywords`). If the file exists and
//! the field is found, it processes the field's content (assuming it's an array),
//! formats it into a string, and prints the result. If the file doesn't exist,
//! it prints an empty string.
//!
//! ```rust
//! // Import the necessary function for retrieving a field from a YAML file.
//! use libmake::utils::get_yaml_field;
//! use std::error::Error;
//! use std::path::Path;
//!
//! // Specify the path to the YAML file.
//! let file_path = "../tests/data/mylibrary.yaml";
//!
//! // Define the YAML field to retrieve.
//! let field_keywords = "keywords";
//!
//! // Check if the YAML file exists before retrieving the field.
//! let value = if Path::new(file_path).exists() {
//! // If the file exists, use the `get_yaml_field` function to retrieve the field.
//! let keywords: Result<Vec<String>, Box<dyn Error>> = get_yaml_field(Some(file_path), field_keywords)
//! .map(|s| s.split('\n')
//! .map(|s| s.trim_start_matches("- "))
//! .filter(|s| !s.is_empty())
//! .map(|s| format!("\"{}\"", s))
//! .collect());
//!
//! match keywords {
//! Ok(keywords) => format!("[{}]", keywords.join(", ")),
//! Err(e) => {
//! eprintln!("Error retrieving keywords: {}", e);
//! String::new()
//! }
//! }
//! } else {
//! // If the file doesn't exist, set the value to an empty string.
//! String::new()
//! };
//!
//! // Print the result.
//! println!("🦀 get_yaml_field, ✅ {}: {}", field_keywords, value);
//! ```
// Title: Test: Retrieving a field from a YAML file
use get_yaml_field;
use Error;
use Path;
/// # Test: Retrieving a Field from a YAML File
///
/// This is a test that demonstrates how to retrieve a specific field from a YAML file
/// using the `get_yaml_field` function from the `libmake` crate.
///
/// ## Purpose
///
/// The purpose of this test is to show how to extract a YAML field (`field_keywords`)
/// from a YAML file located at the specified path (`file_path`). The test specifically
/// handles YAML arrays and converts them into a formatted string.
///
/// ## Usage
///
/// To run this test, ensure that you have a valid YAML file at the specified path.
/// The test checks if the file exists and then uses the `get_yaml_field` function
/// to retrieve the specified YAML field (`field_keywords`). If the file exists and
/// the field is found, it processes the field's content (assuming it's an array),
/// formats it into a string, and prints the result. If the file doesn't exist,
/// it prints an empty string.
///
/// ```rust
/// // Import the necessary function for retrieving a field from a YAML file.
/// use libmake::utils::get_yaml_field;
/// use std::error::Error;
/// use std::path::Path;
///
/// // Specify the path to the YAML file.
/// let file_path = "../tests/data/mylibrary.yaml";
///
/// // Define the YAML field to retrieve.
/// let field_keywords = "keywords";
///
/// // Check if the YAML file exists before retrieving the field.
/// let value = if Path::new(file_path).exists() {
/// // If the file exists, use the `get_yaml_field` function to retrieve the field.
/// let keywords: Result<Vec<String>, Box<dyn Error>> = get_yaml_field(Some(file_path), field_keywords)
/// .map(|s| s.split('\n')
/// .map(|s| s.trim_start_matches("- "))
/// .filter(|s| !s.is_empty())
/// .map(|s| format!("\"{}\"", s))
/// .collect());
///
/// match keywords {
/// Ok(keywords) => format!("[{}]", keywords.join(", ")),
/// Err(e) => {
/// eprintln!("Error retrieving keywords: {}", e);
/// String::new()
/// }
/// }
/// } else {
/// // If the file doesn't exist, set the value to an empty string.
/// String::new()
/// };
///
/// // Print the result.
/// println!("🦀 get_yaml_field, ✅ {}: {}", field_keywords, value);
/// ```
///
pub