niri-ipc: use feature-flag for deriving schemars::JsonSchema

This commit is contained in:
olistrik 2024-07-11 11:09:57 +02:00
parent 0704e1e330
commit 5c27b93e07
2 changed files with 40 additions and 21 deletions

View File

@ -9,9 +9,10 @@ repository.workspace = true
[dependencies] [dependencies]
clap = { workspace = true, optional = true } clap = { workspace = true, optional = true }
schemars = "0.8.21" schemars = { version = "0.8.21", optional = true }
serde.workspace = true serde.workspace = true
serde_json.workspace = true serde_json.workspace = true
[features] [features]
clap = ["dep:clap"] clap = ["dep:clap"]
json-schema = ["dep:schemars"]

View File

@ -4,14 +4,14 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::str::FromStr; use std::str::FromStr;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
mod socket; mod socket;
pub use socket::{Socket, SOCKET_PATH_ENV}; pub use socket::{Socket, SOCKET_PATH_ENV};
/// Request from client to niri. /// Request from client to niri.
#[derive(Debug, Serialize, Deserialize, Clone, JsonSchema)] #[derive(Debug, Serialize, Deserialize, Clone)]
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
pub enum Request { pub enum Request {
/// Request the version string for the running niri instance. /// Request the version string for the running niri instance.
Version, Version,
@ -51,7 +51,8 @@ pub enum Request {
pub type Reply = Result<Response, String>; pub type Reply = Result<Response, String>;
/// Successful response from niri to client. /// Successful response from niri to client.
#[derive(Debug, Serialize, Deserialize, Clone, JsonSchema)] #[derive(Debug, Serialize, Deserialize, Clone)]
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
pub enum Response { pub enum Response {
/// A request that does not need a response was handled successfully. /// A request that does not need a response was handled successfully.
Handled, Handled,
@ -74,10 +75,11 @@ pub enum Response {
/// Actions that niri can perform. /// Actions that niri can perform.
// Variants in this enum should match the spelling of the ones in niri-config. Most, but not all, // Variants in this enum should match the spelling of the ones in niri-config. Most, but not all,
// variants from niri-config should be present here. // variants from niri-config should be present here.
#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema)] #[derive(Serialize, Deserialize, Debug, Clone)]
#[cfg_attr(feature = "clap", derive(clap::Parser))] #[cfg_attr(feature = "clap", derive(clap::Parser))]
#[cfg_attr(feature = "clap", command(subcommand_value_name = "ACTION"))] #[cfg_attr(feature = "clap", command(subcommand_value_name = "ACTION"))]
#[cfg_attr(feature = "clap", command(subcommand_help_heading = "Actions"))] #[cfg_attr(feature = "clap", command(subcommand_help_heading = "Actions"))]
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
pub enum Action { pub enum Action {
/// Exit niri. /// Exit niri.
Quit { Quit {
@ -278,7 +280,8 @@ pub enum Action {
} }
/// Change in window or column size. /// Change in window or column size.
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, JsonSchema)] #[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
pub enum SizeChange { pub enum SizeChange {
/// Set the size in logical pixels. /// Set the size in logical pixels.
SetFixed(i32), SetFixed(i32),
@ -291,7 +294,8 @@ pub enum SizeChange {
} }
/// Workspace reference (index or name) to operate on. /// Workspace reference (index or name) to operate on.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, JsonSchema)] #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
pub enum WorkspaceReferenceArg { pub enum WorkspaceReferenceArg {
/// Index of the workspace. /// Index of the workspace.
Index(u8), Index(u8),
@ -300,7 +304,8 @@ pub enum WorkspaceReferenceArg {
} }
/// Layout to switch to. /// Layout to switch to.
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, JsonSchema)] #[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
pub enum LayoutSwitchTarget { pub enum LayoutSwitchTarget {
/// The next configured layout. /// The next configured layout.
Next, Next,
@ -311,10 +316,11 @@ pub enum LayoutSwitchTarget {
/// Output actions that niri can perform. /// Output actions that niri can perform.
// Variants in this enum should match the spelling of the ones in niri-config. Most thigs from // Variants in this enum should match the spelling of the ones in niri-config. Most thigs from
// niri-config should be present here. // niri-config should be present here.
#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema)] #[derive(Serialize, Deserialize, Debug, Clone)]
#[cfg_attr(feature = "clap", derive(clap::Parser))] #[cfg_attr(feature = "clap", derive(clap::Parser))]
#[cfg_attr(feature = "clap", command(subcommand_value_name = "ACTION"))] #[cfg_attr(feature = "clap", command(subcommand_value_name = "ACTION"))]
#[cfg_attr(feature = "clap", command(subcommand_help_heading = "Actions"))] #[cfg_attr(feature = "clap", command(subcommand_help_heading = "Actions"))]
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
pub enum OutputAction { pub enum OutputAction {
/// Turn off the output. /// Turn off the output.
Off, Off,
@ -362,7 +368,8 @@ pub enum OutputAction {
} }
/// Output mode to set. /// Output mode to set.
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, JsonSchema)] #[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
pub enum ModeToSet { pub enum ModeToSet {
/// Niri will pick the mode automatically. /// Niri will pick the mode automatically.
Automatic, Automatic,
@ -371,7 +378,8 @@ pub enum ModeToSet {
} }
/// Output mode as set in the config file. /// Output mode as set in the config file.
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, JsonSchema)] #[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
pub struct ConfiguredMode { pub struct ConfiguredMode {
/// Width in physical pixels. /// Width in physical pixels.
pub width: u16, pub width: u16,
@ -382,7 +390,8 @@ pub struct ConfiguredMode {
} }
/// Output scale to set. /// Output scale to set.
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, JsonSchema)] #[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
pub enum ScaleToSet { pub enum ScaleToSet {
/// Niri will pick the scale automatically. /// Niri will pick the scale automatically.
Automatic, Automatic,
@ -391,10 +400,11 @@ pub enum ScaleToSet {
} }
/// Output position to set. /// Output position to set.
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, JsonSchema)] #[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "clap", derive(clap::Subcommand))] #[cfg_attr(feature = "clap", derive(clap::Subcommand))]
#[cfg_attr(feature = "clap", command(subcommand_value_name = "POSITION"))] #[cfg_attr(feature = "clap", command(subcommand_value_name = "POSITION"))]
#[cfg_attr(feature = "clap", command(subcommand_help_heading = "Position Values"))] #[cfg_attr(feature = "clap", command(subcommand_help_heading = "Position Values"))]
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
pub enum PositionToSet { pub enum PositionToSet {
/// Position the output automatically. /// Position the output automatically.
#[cfg_attr(feature = "clap", command(name = "auto"))] #[cfg_attr(feature = "clap", command(name = "auto"))]
@ -405,8 +415,9 @@ pub enum PositionToSet {
} }
/// Output position as set in the config file. /// Output position as set in the config file.
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, JsonSchema)] #[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "clap", derive(clap::Args))] #[cfg_attr(feature = "clap", derive(clap::Args))]
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
pub struct ConfiguredPosition { pub struct ConfiguredPosition {
/// Logical X position. /// Logical X position.
pub x: i32, pub x: i32,
@ -415,7 +426,8 @@ pub struct ConfiguredPosition {
} }
/// Connected output. /// Connected output.
#[derive(Debug, Serialize, Deserialize, Clone, JsonSchema)] #[derive(Debug, Serialize, Deserialize, Clone)]
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
pub struct Output { pub struct Output {
/// Name of the output. /// Name of the output.
pub name: String, pub name: String,
@ -442,7 +454,8 @@ pub struct Output {
} }
/// Output mode. /// Output mode.
#[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq, JsonSchema)] #[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
pub struct Mode { pub struct Mode {
/// Width in physical pixels. /// Width in physical pixels.
pub width: u16, pub width: u16,
@ -455,7 +468,8 @@ pub struct Mode {
} }
/// Logical output in the compositor's coordinate space. /// Logical output in the compositor's coordinate space.
#[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq, JsonSchema)] #[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
pub struct LogicalOutput { pub struct LogicalOutput {
/// Logical X position. /// Logical X position.
pub x: i32, pub x: i32,
@ -472,8 +486,9 @@ pub struct LogicalOutput {
} }
/// Output transform, which goes counter-clockwise. /// Output transform, which goes counter-clockwise.
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, JsonSchema)] #[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "clap", derive(clap::ValueEnum))] #[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
pub enum Transform { pub enum Transform {
/// Untransformed. /// Untransformed.
Normal, Normal,
@ -500,7 +515,8 @@ pub enum Transform {
} }
/// Toplevel window. /// Toplevel window.
#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema)] #[derive(Serialize, Deserialize, Debug, Clone)]
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
pub struct Window { pub struct Window {
/// Title, if set. /// Title, if set.
pub title: Option<String>, pub title: Option<String>,
@ -509,7 +525,8 @@ pub struct Window {
} }
/// Output configuration change result. /// Output configuration change result.
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, JsonSchema)] #[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
pub enum OutputConfigChanged { pub enum OutputConfigChanged {
/// The target output was connected and the change was applied. /// The target output was connected and the change was applied.
Applied, Applied,
@ -518,7 +535,8 @@ pub enum OutputConfigChanged {
} }
/// A workspace. /// A workspace.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, JsonSchema)] #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
pub struct Workspace { pub struct Workspace {
/// Index of the workspace on its monitor. /// Index of the workspace on its monitor.
/// ///