Remove schemars and clean up errors

pull/189/head
Chip Senkbeil 1 year ago
parent 810567b5e9
commit 27e1e22208
No known key found for this signature in database
GPG Key ID: 35EF1F8EC72A4131

37
Cargo.lock generated

@ -888,7 +888,6 @@ dependencies = [
"rand",
"regex",
"rstest",
"schemars",
"serde",
"serde_bytes",
"serde_json",
@ -921,7 +920,6 @@ dependencies = [
"paste",
"rand",
"rmp-serde",
"schemars",
"serde",
"serde_bytes",
"serde_json",
@ -2709,30 +2707,6 @@ dependencies = [
"winapi-util",
]
[[package]]
name = "schemars"
version = "0.8.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "02c613288622e5f0c3fdc5dbd4db1c5fbe752746b1d1a56a0630b78fd00de44f"
dependencies = [
"dyn-clone",
"schemars_derive",
"serde",
"serde_json",
]
[[package]]
name = "schemars_derive"
version = "0.8.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "109da1e6b197438deb6db99952990c7f959572794b80ff93707d55a232545e7c"
dependencies = [
"proc-macro2",
"quote",
"serde_derive_internals",
"syn 1.0.109",
]
[[package]]
name = "scopeguard"
version = "1.1.0"
@ -2806,17 +2780,6 @@ dependencies = [
"syn 2.0.16",
]
[[package]]
name = "serde_derive_internals"
version = "0.26.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85bf8229e7920a9f636479437026331ce11aa132b4dde37d121944a44d6e5f3c"
dependencies = [
"proc-macro2",
"quote",
"syn 1.0.109",
]
[[package]]
name = "serde_json"
version = "1.0.96"

@ -32,7 +32,7 @@ clap_complete = "4.2.0"
config = { version = "0.13.3", default-features = false, features = ["toml"] }
derive_more = { version = "0.99.17", default-features = false, features = ["display", "from", "error", "is_variant"] }
dialoguer = { version = "0.10.3", default-features = false }
distant-core = { version = "=0.20.0-alpha.7", path = "distant-core", features = ["schemars"] }
distant-core = { version = "=0.20.0-alpha.7", path = "distant-core" }
directories = "5.0.0"
file-mode = "0.1.2"
flexi_logger = "0.25.3"

@ -11,9 +11,6 @@ repository = "https://github.com/chipsenkbeil/distant"
readme = "README.md"
license = "MIT OR Apache-2.0"
[features]
schemars = ["dep:schemars", "distant-net/schemars"]
[dependencies]
async-trait = "0.1.68"
bitflags = "2.0.2"
@ -43,9 +40,6 @@ walkdir = "2.3.3"
whoami = "1.4.0"
winsplit = "0.1.0"
# Optional dependencies based on features
schemars = { version = "0.8.12", optional = true }
[dev-dependencies]
assert_fs = "1.0.12"
env_logger = "0.10.0"

@ -28,18 +28,9 @@ You can import the dependency by adding the following to your `Cargo.toml`:
```toml
[dependencies]
distant-core = "0.19"
distant-core = "0.20"
```
## Features
Currently, the library supports the following features:
- `schemars`: derives the `schemars::JsonSchema` interface on
`DistantMsg`, `DistantRequestData`, and `DistantResponseData` data types
By default, no features are enabled on the library.
## Examples
Below is an example of connecting to a distant server over TCP without any

@ -5,12 +5,13 @@ use std::path::{Path, PathBuf};
use distant_net::common::ConnectionId;
use log::*;
use notify::event::{AccessKind, AccessMode, ModifyKind};
use notify::{
Config as WatcherConfig, Error as WatcherError, ErrorKind as WatcherErrorKind,
Event as WatcherEvent, PollWatcher, RecursiveMode, Watcher,
Event as WatcherEvent, EventKind, PollWatcher, RecursiveMode, Watcher,
};
use tokio::sync::mpsc;
use tokio::sync::mpsc::error::TrySendError;
use tokio::sync::mpsc::{self};
use tokio::sync::oneshot;
use tokio::task::JoinHandle;
@ -256,7 +257,20 @@ async fn watcher_task(mut watcher: impl Watcher, mut rx: mpsc::Receiver<InnerWat
}
}
InnerWatcherMsg::Event { ev } => {
let kind = ChangeKind::from(ev.kind);
let kind = match ev.kind {
EventKind::Access(AccessKind::Read) => ChangeKind::Access,
EventKind::Modify(ModifyKind::Metadata(_)) => ChangeKind::Attribute,
EventKind::Access(AccessKind::Close(AccessMode::Write)) => {
ChangeKind::CloseWrite
}
EventKind::Access(AccessKind::Close(_)) => ChangeKind::CloseNoWrite,
EventKind::Create(_) => ChangeKind::Create,
EventKind::Remove(_) => ChangeKind::Delete,
EventKind::Modify(ModifyKind::Data(_)) => ChangeKind::Modify,
EventKind::Access(AccessKind::Open(_)) => ChangeKind::Open,
EventKind::Modify(ModifyKind::Name(_)) => ChangeKind::Rename,
_ => ChangeKind::Unknown,
};
for registered_path in registered_paths.iter() {
match registered_path.filter_and_send(kind, &ev.paths).await {

@ -267,7 +267,7 @@ mod tests {
paths: vec![test_path.to_path_buf()],
}),
protocol::Response::Changed(Change {
kind: ChangeKind::Content,
kind: ChangeKind::Modify,
paths: vec![test_path.to_path_buf()],
}),
],
@ -289,7 +289,7 @@ mod tests {
assert_eq!(
change,
Change {
kind: ChangeKind::Content,
kind: ChangeKind::Modify,
paths: vec![test_path.to_path_buf()]
}
);
@ -342,7 +342,7 @@ mod tests {
.write_frame_for(&Response::new(
req.id.clone() + "1",
protocol::Response::Changed(Change {
kind: ChangeKind::Content,
kind: ChangeKind::Modify,
paths: vec![test_path.to_path_buf()],
}),
))
@ -354,7 +354,7 @@ mod tests {
.write_frame_for(&Response::new(
req.id,
protocol::Response::Changed(Change {
kind: ChangeKind::Remove,
kind: ChangeKind::Delete,
paths: vec![test_path.to_path_buf()],
}),
))
@ -375,7 +375,7 @@ mod tests {
assert_eq!(
change,
Change {
kind: ChangeKind::Remove,
kind: ChangeKind::Delete,
paths: vec![test_path.to_path_buf()]
}
);
@ -418,11 +418,11 @@ mod tests {
paths: vec![test_path.to_path_buf()],
}),
protocol::Response::Changed(Change {
kind: ChangeKind::Content,
kind: ChangeKind::Modify,
paths: vec![test_path.to_path_buf()],
}),
protocol::Response::Changed(Change {
kind: ChangeKind::Remove,
kind: ChangeKind::Delete,
paths: vec![test_path.to_path_buf()],
}),
],
@ -482,14 +482,14 @@ mod tests {
assert_eq!(
watcher.lock().await.next().await,
Some(Change {
kind: ChangeKind::Content,
kind: ChangeKind::Modify,
paths: vec![test_path.to_path_buf()]
})
);
assert_eq!(
watcher.lock().await.next().await,
Some(Change {
kind: ChangeKind::Remove,
kind: ChangeKind::Delete,
paths: vec![test_path.to_path_buf()]
})
);

@ -1,5 +1,5 @@
use assert_fs::prelude::*;
use distant_core::protocol::ChangeKindSet;
use distant_core::protocol::{ChangeKind, ChangeKindSet};
use distant_core::DistantChannelExt;
use rstest::*;
use test_log::test;
@ -29,7 +29,7 @@ async fn should_handle_large_volume_of_file_watching(#[future] ctx: DistantClien
.watch(
file.path(),
false,
ChangeKindSet::modify_set(),
ChangeKindSet::new([ChangeKind::Modify]),
ChangeKindSet::empty(),
)
.await

@ -32,9 +32,6 @@ serde_bytes = "0.11.9"
strum = { version = "0.24.1", features = ["derive"] }
tokio = { version = "1.27.0", features = ["full"] }
# Optional dependencies based on features
schemars = { version = "0.8.12", optional = true }
[dev-dependencies]
distant-auth = { version = "=0.20.0-alpha.7", path = "../distant-auth", features = ["tests"] }
env_logger = "0.10.0"

@ -25,18 +25,9 @@ You can import the dependency by adding the following to your `Cargo.toml`:
```toml
[dependencies]
distant-net = "0.19"
distant-net = "0.20"
```
## Features
Currently, the library supports the following features:
- `schemars`: derives the `schemars::JsonSchema` interface on `Request`
and `Response` data types
By default, no features are enabled on the library.
## License
This project is licensed under either of

@ -13,7 +13,6 @@ use crate::common::utils::{deserialize_from_str, serialize_to_str};
/// Contains map information for connections and other use cases
#[derive(Clone, Debug, From, IntoIterator, PartialEq, Eq)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub struct Map(HashMap<String, String>);
impl Map {
@ -77,13 +76,6 @@ impl Map {
}
}
#[cfg(feature = "schemars")]
impl Map {
pub fn root_schema() -> schemars::schema::RootSchema {
schemars::schema_for!(Map)
}
}
impl Default for Map {
fn default() -> Self {
Self::new()

@ -10,7 +10,6 @@ use crate::common::utils;
/// Represents a request to send
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub struct Request<T> {
/// Unique id associated with the request
pub id: Id,
@ -62,13 +61,6 @@ where
}
}
#[cfg(feature = "schemars")]
impl<T: schemars::JsonSchema> Request<T> {
pub fn root_schema() -> schemars::schema::RootSchema {
schemars::schema_for!(Request<T>)
}
}
impl<T> From<T> for Request<T> {
fn from(payload: T) -> Self {
Self::new(payload)

@ -10,7 +10,6 @@ use crate::common::utils;
/// Represents a response received related to some response
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub struct Response<T> {
/// Unique id associated with the response
pub id: Id,
@ -67,13 +66,6 @@ where
}
}
#[cfg(feature = "schemars")]
impl<T: schemars::JsonSchema> Response<T> {
pub fn root_schema() -> schemars::schema::RootSchema {
schemars::schema_for!(Response<T>)
}
}
/// Error encountered when attempting to parse bytes as an untyped response
#[derive(Copy, Clone, Debug, Display, Error, PartialEq, Eq, Hash)]
pub enum UntypedResponseParseError {

@ -12,7 +12,6 @@ use super::ManagerCapabilityKind;
/// Set of supported capabilities for a manager
#[derive(Clone, Debug, From, Into, PartialEq, Eq, IntoIterator, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(transparent)]
pub struct ManagerCapabilities(#[into_iterator(owned, ref)] HashSet<ManagerCapability>);
@ -76,13 +75,6 @@ impl ManagerCapabilities {
}
}
#[cfg(feature = "schemars")]
impl ManagerCapabilities {
pub fn root_schema() -> schemars::schema::RootSchema {
schemars::schema_for!(ManagerCapabilities)
}
}
impl BitAnd for &ManagerCapabilities {
type Output = ManagerCapabilities;
@ -133,7 +125,6 @@ impl FromIterator<ManagerCapability> for ManagerCapabilities {
/// ManagerCapability tied to a manager. A capability is equivalent based on its kind and not
/// description.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub struct ManagerCapability {
/// Label describing the kind of capability
@ -196,17 +187,3 @@ impl From<ManagerCapabilityKind> for ManagerCapability {
}
}
}
#[cfg(feature = "schemars")]
impl ManagerCapability {
pub fn root_schema() -> schemars::schema::RootSchema {
schemars::schema_for!(ManagerCapability)
}
}
#[cfg(feature = "schemars")]
impl ManagerCapabilityKind {
pub fn root_schema() -> schemars::schema::RootSchema {
schemars::schema_for!(ManagerCapabilityKind)
}
}

@ -21,10 +21,6 @@ use crate::common::{ConnectionId, Destination, Map, UntypedRequest};
Serialize,
Deserialize
))]
#[cfg_attr(
feature = "schemars",
strum_discriminants(derive(schemars::JsonSchema))
)]
#[strum_discriminants(name(ManagerCapabilityKind))]
#[strum_discriminants(strum(serialize_all = "snake_case"))]
#[serde(rename_all = "snake_case", deny_unknown_fields, tag = "type")]

@ -57,27 +57,18 @@ pub enum ChangeKind {
/// A file, directory, or something else was created within a watched directory
Create,
/// A file, directory, or something else was deleted within a watched directory
/// A file, directory, or something else was deleted
Delete,
/// A watched file or directory was deleted
DeleteSelf,
/// A file's content was modified
Modify,
/// A file, directory, or something else was moved out of a watched directory
MoveFrom,
/// A watched file or directory was moved
MoveSelf,
/// A file, directory, or something else was moved into a watched directory
MoveTo,
/// A file was opened
Open,
/// A file, directory, or something else was renamed in some way
Rename,
/// Catch-all for any other change
Unknown,
}
@ -101,11 +92,26 @@ impl ChangeKind {
)
}
/// Returns true if kind is part of the create family.
pub fn is_create(&self) -> bool {
matches!(self, Self::Create)
}
/// Returns true if kind is part of the delete family.
pub fn is_delete(&self) -> bool {
matches!(self, Self::Delete)
}
/// Returns true if kind is part of the modify family.
pub fn is_modify(&self) -> bool {
matches!(self, Self::Attribute | Self::Modify)
}
/// Returns true if kind is part of the rename family.
pub fn is_rename(&self) -> bool {
matches!(self, Self::Rename)
}
/// Returns true if kind is unknown.
pub fn is_unknown(&self) -> bool {
matches!(self, Self::Unknown)
@ -146,12 +152,9 @@ impl ChangeKindSet {
ChangeKind::CloseNoWrite,
ChangeKind::Create,
ChangeKind::Delete,
ChangeKind::DeleteSelf,
ChangeKind::Modify,
ChangeKind::MoveFrom,
ChangeKind::MoveSelf,
ChangeKind::MoveTo,
ChangeKind::Open,
ChangeKind::Rename,
ChangeKind::Unknown,
]
.into_iter()
@ -292,23 +295,23 @@ mod tests {
#[test]
fn should_be_able_to_serialize_to_json() {
let set = ChangeKindSet::new([ChangeKind::MoveTo]);
let set = ChangeKindSet::new([ChangeKind::CloseWrite]);
let value = serde_json::to_value(set).unwrap();
assert_eq!(value, serde_json::json!(["move_to"]));
assert_eq!(value, serde_json::json!(["close_write"]));
}
#[test]
fn should_be_able_to_deserialize_from_json() {
let value = serde_json::json!(["move_to"]);
let value = serde_json::json!(["close_write"]);
let set: ChangeKindSet = serde_json::from_value(value).unwrap();
assert_eq!(set, ChangeKindSet::new([ChangeKind::MoveTo]));
assert_eq!(set, ChangeKindSet::new([ChangeKind::CloseWrite]));
}
#[test]
fn should_be_able_to_serialize_to_msgpack() {
let set = ChangeKindSet::new([ChangeKind::MoveTo]);
let set = ChangeKindSet::new([ChangeKind::CloseWrite]);
// NOTE: We don't actually check the output here because it's an implementation detail
// and could change as we change how serialization is done. This is merely to verify
@ -324,10 +327,11 @@ mod tests {
// client/server and then trying to deserialize on the other side. This has happened
// enough times with minor changes that we need tests to verify.
let buf =
rmp_serde::encode::to_vec_named(&ChangeKindSet::new([ChangeKind::MoveTo])).unwrap();
rmp_serde::encode::to_vec_named(&ChangeKindSet::new([ChangeKind::CloseWrite]))
.unwrap();
let set: ChangeKindSet = rmp_serde::decode::from_slice(&buf).unwrap();
assert_eq!(set, ChangeKindSet::new([ChangeKind::MoveTo]));
assert_eq!(set, ChangeKindSet::new([ChangeKind::CloseWrite]));
}
}
@ -336,23 +340,23 @@ mod tests {
#[test]
fn should_be_able_to_serialize_to_json() {
let kind = ChangeKind::MoveTo;
let kind = ChangeKind::CloseWrite;
let value = serde_json::to_value(kind).unwrap();
assert_eq!(value, serde_json::json!("move_to"));
assert_eq!(value, serde_json::json!("close_write"));
}
#[test]
fn should_be_able_to_deserialize_from_json() {
let value = serde_json::json!("move_to");
let value = serde_json::json!("close_write");
let kind: ChangeKind = serde_json::from_value(value).unwrap();
assert_eq!(kind, ChangeKind::MoveTo);
assert_eq!(kind, ChangeKind::CloseWrite);
}
#[test]
fn should_be_able_to_serialize_to_msgpack() {
let kind = ChangeKind::MoveTo;
let kind = ChangeKind::CloseWrite;
// NOTE: We don't actually check the output here because it's an implementation detail
// and could change as we change how serialization is done. This is merely to verify
@ -367,10 +371,10 @@ mod tests {
// verify that we are not corrupting or causing issues when serializing on a
// client/server and then trying to deserialize on the other side. This has happened
// enough times with minor changes that we need tests to verify.
let buf = rmp_serde::encode::to_vec_named(&ChangeKind::MoveTo).unwrap();
let buf = rmp_serde::encode::to_vec_named(&ChangeKind::CloseWrite).unwrap();
let kind: ChangeKind = rmp_serde::decode::from_slice(&buf).unwrap();
assert_eq!(kind, ChangeKind::MoveTo);
assert_eq!(kind, ChangeKind::CloseWrite);
}
}
}

@ -57,7 +57,7 @@ where
if environment.is_empty() {
None
} else {
Some(environment.into_map())
Some(environment)
},
)
.compat()
@ -143,7 +143,7 @@ where
if environment.is_empty() {
None
} else {
Some(environment.into_map())
Some(environment)
},
)
.compat()

@ -402,7 +402,12 @@ async fn async_run(cmd: ClientSubcommand) -> CliResult {
cmd.as_deref().unwrap_or(r"$SHELL")
);
Shell::new(channel.into_client().into_channel())
.spawn(cmd, environment, current_dir, MAX_PIPE_CHUNK_SIZE)
.spawn(
cmd,
environment.into_map(),
current_dir,
MAX_PIPE_CHUNK_SIZE,
)
.await?;
}
ClientSubcommand::Spawn {
@ -449,7 +454,12 @@ async fn async_run(cmd: ClientSubcommand) -> CliResult {
environment, current_dir, cmd
);
Shell::new(channel.into_client().into_channel())
.spawn(cmd, environment, current_dir, MAX_PIPE_CHUNK_SIZE)
.spawn(
cmd,
environment.into_map(),
current_dir,
MAX_PIPE_CHUNK_SIZE,
)
.await?;
} else {
debug!(
@ -457,7 +467,7 @@ async fn async_run(cmd: ClientSubcommand) -> CliResult {
environment, current_dir, cmd
);
let mut proc = RemoteCommand::new()
.environment(environment)
.environment(environment.into_map())
.current_dir(current_dir)
.pty(None)
.spawn(channel.into_client().into_channel(), &cmd)

@ -156,10 +156,10 @@ fn format_shell(state: &mut FormatterState, data: protocol::Response) -> Output
"{}{}",
match change.kind {
ChangeKind::Create => "Following paths were created:\n",
ChangeKind::Remove => "Following paths were removed:\n",
x if x.is_access_kind() => "Following paths were accessed:\n",
x if x.is_modify_kind() => "Following paths were modified:\n",
x if x.is_rename_kind() => "Following paths were renamed:\n",
ChangeKind::Delete => "Following paths were removed:\n",
x if x.is_access() => "Following paths were accessed:\n",
x if x.is_modify() => "Following paths were modified:\n",
x if x.is_rename() => "Following paths were renamed:\n",
_ => "Following paths were affected:\n",
},
change

@ -3,8 +3,6 @@ use std::{fs, io};
use anyhow::Context;
use clap::CommandFactory;
use clap_complete::generate as clap_generate;
use distant_core::net::common::{Request, Response};
use distant_core::protocol;
use crate::options::{Config, GenerateSubcommand};
use crate::{CliResult, Options};
@ -20,35 +18,6 @@ async fn async_run(cmd: GenerateSubcommand) -> CliResult {
.await
.context("Failed to write default config to {file:?}")?,
GenerateSubcommand::Schema { file } => {
let request_schema =
serde_json::to_value(&Request::<protocol::Msg<protocol::Request>>::root_schema())
.context("Failed to serialize request schema")?;
let response_schema =
serde_json::to_value(&Response::<protocol::Msg<protocol::Response>>::root_schema())
.context("Failed to serialize response schema")?;
let schema = serde_json::json!({
"request": request_schema,
"response": response_schema,
});
if let Some(path) = file {
serde_json::to_writer_pretty(
&mut fs::OpenOptions::new()
.create(true)
.write(true)
.open(&path)
.with_context(|| format!("Failed to open {path:?}"))?,
&schema,
)
.context("Failed to write to {path:?}")?;
} else {
serde_json::to_writer_pretty(&mut io::stdout(), &schema)
.context("Failed to print to stdout")?;
}
}
GenerateSubcommand::Completion { file, shell } => {
let name = "distant";
let mut cmd = Options::command();

@ -7,7 +7,7 @@ use clap_complete::Shell as ClapCompleteShell;
use derive_more::IsVariant;
use distant_core::net::common::{ConnectionId, Destination, Map, PortRange};
use distant_core::net::server::Shutdown;
use distant_core::protocol::{ChangeKind, Environment};
use distant_core::protocol::ChangeKind;
use service_manager::ServiceManagerKind;
use crate::constants;
@ -392,7 +392,7 @@ pub enum ClientSubcommand {
/// Environment variables to provide to the shell
#[clap(long, default_value_t)]
environment: Environment,
environment: Map,
/// Optional command to run instead of $SHELL
#[clap(name = "CMD", last = true)]
@ -434,7 +434,7 @@ pub enum ClientSubcommand {
/// Environment variables to provide to the shell
#[clap(long, default_value_t)]
environment: Environment,
environment: Map,
/// Command to run
#[clap(name = "CMD", num_args = 1.., last = true)]
@ -894,13 +894,6 @@ pub enum GenerateSubcommand {
file: PathBuf,
},
/// Generate JSON schema for server request/response
Schema {
/// If specified, will output to the file at the given path instead of stdout
#[clap(long)]
file: Option<PathBuf>,
},
// Generate completion info for CLI
Completion {
/// If specified, will output to the file at the given path instead of stdout
@ -1666,7 +1659,7 @@ mod tests {
windows_pipe: None,
},
current_dir: None,
environment: map!(),
environment: Default::default(),
cmd: None,
}),
};

Loading…
Cancel
Save