-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathlib.rs
More file actions
193 lines (175 loc) · 6.24 KB
/
Copy pathlib.rs
File metadata and controls
193 lines (175 loc) · 6.24 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Copyright (C) 2026 Bryan A. Jones.
//
// This file is part of the CodeChat Editor. The CodeChat Editor is free
// software: you can redistribute it and/or modify it under the terms of the GNU
// General Public License as published by the Free Software Foundation, either
// version 3 of the License, or (at your option) any later version.
//
// The CodeChat Editor is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details.
//
// You should have received a copy of the GNU General Public License along with
// the CodeChat Editor. If not, see
// [http://www.gnu.org/licenses](http://www.gnu.org/licenses).
//
// `lib.rs` -- Interface to the CodeChat Editor for VSCode
// =======================================================
//
// Imports
// -------
//
// ### Standard library
use std::path::PathBuf;
// ### Third-party
use log::LevelFilter;
use napi::{Error, Status};
use napi_derive::napi;
// ### Local
use code_chat_editor::{ide, webserver};
// Code
// ----
#[napi]
pub fn init_server(extension_base_path: String) -> Result<(), Error> {
// `extension_base_path` is this extension's install directory. In a dev
// build, that's `extensions/VSCode/`, two directories below the
// repository root that `webserver::set_root_path` needs; in a packaged
// build, `client/static`, `log4rs.yml`, and `hashLocations.json` are
// copied alongside the extension (see `builder`'s `run_postrelease`), so
// the extension's own directory is already the root.
let base_path = PathBuf::from(extension_base_path);
let base_path = if cfg!(debug_assertions) {
base_path.join("../..")
} else {
base_path
};
webserver::init_server(
&base_path,
if cfg!(debug_assertions) {
LevelFilter::Debug
} else {
LevelFilter::Warn
},
)
.map_err(|err| Error::new(Status::GenericFailure, err.to_string()))
}
#[napi]
struct CodeChatEditorServer(ide::CodeChatEditorServer);
#[napi]
// NAPI's C code calls this, which Rust can't see.
#[allow(dead_code)]
impl CodeChatEditorServer {
#[napi(constructor)]
pub fn new(capture_spool_path: String) -> Result<CodeChatEditorServer, Error> {
Ok(CodeChatEditorServer(
ide::CodeChatEditorServer::new_with_capture_spool(&PathBuf::from(capture_spool_path))?,
))
}
// This returns an error if the conversion to JSON fails, `None` if the
// queue is closed, or a JSON-encoded string containing the message
// otherwise.
#[napi]
pub async fn get_message(&self) -> Result<Option<String>, Error> {
let editor_message = self.0.get_message().await;
// Encode then deliver it.
match editor_message {
Some(editor_message) => match serde_json::to_string(&editor_message) {
Ok(v) => Ok(Some(v)),
Err(err) => Err(Error::new(Status::GenericFailure, err.to_string())),
},
None => Ok(None),
}
}
#[napi]
pub async fn send_message_opened(&self, hosted_in_ide: bool) -> std::io::Result<f64> {
self.0.send_message_opened(hosted_in_ide).await
}
#[napi]
pub async fn send_capture_event(&self, capture_event_json: String) -> std::io::Result<f64> {
let capture_event = serde_json::from_str(&capture_event_json)
.map_err(|err| std::io::Error::other(err.to_string()))?;
self.0.send_capture_event(capture_event).await
}
#[napi]
pub fn get_capture_status(&self) -> Result<String, Error> {
serde_json::to_string(&self.0.capture_status())
.map_err(|err| Error::new(Status::GenericFailure, err.to_string()))
}
#[napi]
// We must pass `base_url` as a `String` per NAPI requirements.
#[allow(clippy::needless_pass_by_value)]
pub fn configure_capture_service(
&self,
base_url: String,
token: Option<String>,
) -> Result<(), Error> {
self.0
.configure_capture_service(&base_url, token)
.map_err(|err| Error::new(Status::GenericFailure, err))
}
#[napi]
pub fn clear_capture_token(&self) -> Result<(), Error> {
self.0
.clear_capture_token()
.map_err(|err| Error::new(Status::GenericFailure, err))
}
#[napi]
pub fn check_capture_service_status(&self) -> Result<String, Error> {
serde_json::to_string(
&self
.0
.check_capture_service_status()
.map_err(|err| Error::new(Status::GenericFailure, err))?,
)
.map_err(|err| Error::new(Status::GenericFailure, err.to_string()))
}
#[napi]
pub async fn send_message_current_file(&self, url: String) -> std::io::Result<f64> {
self.0.send_message_current_file(url).await
}
#[napi]
pub async fn send_message_update_plain(
&self,
file_path: String,
// `null` to send no source code; a `(string, version)` to send the
// source code.
option_contents: Option<(String, f64)>,
cursor_position: Option<u32>,
scroll_position: Option<f64>,
) -> std::io::Result<f64> {
self.0
.send_message_update_plain(file_path, option_contents, cursor_position, scroll_position)
.await
}
#[napi]
pub async fn send_result(
&self,
id: f64,
// If provided, a JSON-encoded `ResultErrTypes`.
message_result: Option<String>,
) -> std::io::Result<()> {
let message = if let Some(err_json) = message_result {
match serde_json::from_str(&err_json) {
Ok(v) => Some(v),
Err(err) => return Err(std::io::Error::other(err.to_string())),
}
} else {
None
};
self.0.send_result(id, message).await
}
#[napi]
pub async fn send_result_loadfile(
&self,
id: f64,
load_file: Option<(String, f64)>,
) -> std::io::Result<()> {
self.0.send_result_loadfile(id, load_file).await
}
// This returns after the server shuts down.
#[napi]
pub async fn stop_server(&self) {
self.0.stop_server().await;
}
}