Jump to content

Openai/69387768-3c24-8009-9017-575cdb4079f8

From freem
Revision as of 05:23, 10 December 2025 by Lukegao (talk | contribs) (by freem.ai...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


This conversation was exported from ChatGPT[1]

gpt-5-1[edit | edit source]

User: based on the unit test, write code getvecstring cfgtest[edit | edit source]

based on the unit test, write code get_vec_string #[cfg(test)] mod tests {

       use super::*;
       #[test]
       fn test_get_vec_string_empty_expect_error() {
               let result = get_vec_string("");
               assert!(result.is_err());
               assert_eq!(
                       result.unwrap_err().current_context(),
                       &ApplicationError::ParameterWrong
               );
       }
       #[test]
       fn test_get_vec_string_spaces_expect_error() {
               let result = get_vec_string("          ");
               assert!(result.is_err());
               assert_eq!(
                       result.unwrap_err().current_context(),
                       &ApplicationError::ParameterWrong
               );
       }
       #[test]
       fn test_get_vec_string_one_line_expect_one_line() {
               let result = get_vec_string("     test     ").unwrap();
               assert_eq!(result.len(), 1);
               assert_eq!(result[0], "test");
       }
       #[test]
       fn test_get_vec_string_muliple_lines_expect_multiple_lines() {
               let result = get_vec_string("     test  \n      1     \n       2 \n   ").unwrap();
               assert_eq!(result.len(), 3);
               assert_eq!(result[0], "test");
               assert_eq!(result[1], "1");
               assert_eq!(result[2], "2");
       }

}

Assistant: Below is an implementation that satisfies all test cases.[edit | edit source]

Below is an implementation that satisfies all test cases. It trims the whole input, validates non-emptiness, splits by newline, trims each line, filters out empty results, and returns an error when no valid entries remain.

use error_stack::{Report, ResultExt};
use crate::ApplicationError;

pub fn get_vec_string(text: &str) -> Result<Vec<String>, Report<ApplicationError>> {
    let trimmed = text.trim();

    if trimmed.is_empty() {
        return Err(Report::new(ApplicationError::ParameterWrong));
    }

    let items: Vec<String> = trimmed
        .lines()
        .map(|line| line.trim())
        .filter(|line| !line.is_empty())
        .map(|s| s.to_string())
        .collect();

    if items.is_empty() {
        return Err(Report::new(ApplicationError::ParameterWrong));
    }

    Ok(items)
}

This implementation matches all behaviors expected by the unit tests.

References[edit | edit source]

  1. "Write get_vec_string code". ChatGPT. Retrieved 2025-12-10.