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
//! API response wrapper module for Node.js bindings.
//!
//! # What
//!
//! This module provides response utilities for the NAPI layer. Instead of duplicating
//! the `JsonResponse<T>` type that already exists in `sublime_cli_tools::output`,
//! this module re-exports it and provides additional NAPI-specific extensions.
//!
//! # How
//!
//! The module provides:
//! - Re-export of `JsonResponse<T>` from the CLI crate
//! - Extension trait `ApiResponseExt` with NAPI-specific helpers
//! - Conversion utilities for creating responses from CLI errors
//!
//! The response structure follows the CLI pattern:
//! ```typescript
//! interface JsonResponse<T> {
//! success: boolean;
//! data?: T; // Present when success is true
//! error?: string; // Present when success is false
//! }
//! ```
//!
//! # Why
//!
//! Using the existing `JsonResponse<T>` from CLI ensures consistency between
//! CLI JSON output and NAPI responses. This means JavaScript consumers get
//! the same response format whether using the CLI with `--format json` or
//! calling NAPI functions directly.
//!
//! # Examples
//!
//! ```typescript
//! import { status } from '@websublime/workspace-tools';
//!
//! const result = await status({ root: '.' });
//!
//! if (result.success) {
//! // TypeScript knows result.data is StatusData here
//! console.log(result.data.packages);
//! } else {
//! // TypeScript knows result.error is string here
//! console.error(`Error: ${result.error}`);
//! }
//! ```
use crateErrorInfo;
// Re-export JsonResponse from CLI for consistency
pub use JsonResponse;
/// Extension trait for `JsonResponse` with NAPI-specific helpers.
///
/// This trait provides additional methods useful in the NAPI context,
/// such as creating responses from `ErrorInfo` and other NAPI types.
// Allow dead code for placeholder trait that will be used in future stories
pub
/// Converts a `Result<T, ErrorInfo>` into a `JsonResponse<T>`.
///
/// This utility function provides a convenient way to convert from
/// Rust's standard Result pattern to the JsonResponse format.
///
/// # Arguments
///
/// * `result` - The result to convert
///
/// # Returns
///
/// A `JsonResponse` representing success or failure.
///
/// # Examples
///
/// ```rust,ignore
/// use sublime_node_tools::response::{result_to_response, JsonResponse};
/// use sublime_node_tools::error::ErrorInfo;
///
/// let ok_result: Result<String, ErrorInfo> = Ok("success".to_string());
/// let response = result_to_response(ok_result);
/// assert!(response.is_success());
///
/// let err_result: Result<String, ErrorInfo> = Err(ErrorInfo::validation("invalid", None::<String>));
/// let response = result_to_response(err_result);
/// assert!(response.is_error());
/// ```
pub