-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathlib.rs
More file actions
134 lines (121 loc) · 4.24 KB
/
lib.rs
File metadata and controls
134 lines (121 loc) · 4.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
// Copyright (C) 2025 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> {
webserver::init_server(
Some(&PathBuf::from(extension_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() -> Result<CodeChatEditorServer, Error> {
Ok(CodeChatEditorServer(ide::CodeChatEditorServer::new()?))
}
// 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_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 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
}
}