Skip to content

Commit

Permalink
gate json schema generation behind feature flag
Browse files Browse the repository at this point in the history
  • Loading branch information
LivacoNew committed Sep 12, 2024
1 parent 3addd37 commit 7818ea0
Show file tree
Hide file tree
Showing 27 changed files with 80 additions and 27 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ player = ["dep:dbus"]
music = ["player"]
android = ["dep:android_system_properties"]
rpm_packages = ["dep:sqlite"]
jsonschema = ["dep:schemars", "dep:serde_json"]

[dependencies]
clap = { version = "4.5.3", features = ["derive"] }
Expand All @@ -34,5 +35,5 @@ sha2 = "0.10.8"
hex = "0.4.3"
strip-ansi-escapes = "0.2.0"
raw-cpuid = "11.1.0"
schemars = "0.8.21"
serde_json = "1.0.128"
schemars = { version = "0.8.21", optional = true }
serde_json = { version = "1.0.128", optional = true }
4 changes: 3 additions & 1 deletion src/ascii.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use colored::ColoredString;
#[cfg(feature = "jsonschema")]
use schemars::JsonSchema;
use serde::Deserialize;

use crate::{config_manager::{self, Configuration}, formatter::CrabFetchColor};

#[derive(Deserialize, JsonSchema)]
#[derive(Deserialize)]
#[cfg_attr(feature = "jsonschema", derive(JsonSchema))]
pub struct AsciiConfiguration {
pub display: bool,
pub side: String,
Expand Down
4 changes: 3 additions & 1 deletion src/config_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ use std::{env, fmt::{Debug, Display}, fs::{self, File}, io::Write, path::{Path,

use config::{builder::DefaultState, Config, ConfigBuilder};
use serde::Deserialize;
#[cfg(feature = "jsonschema")]
use schemars::JsonSchema;

use crate::{ascii::AsciiConfiguration, battery::BatteryConfiguration, cpu::CPUConfiguration, datetime::DateTimeConfiguration, desktop::DesktopConfiguration, displays::DisplayConfiguration, editor::EditorConfiguration, formatter::CrabFetchColor, gpu::GPUConfiguration, host::HostConfiguration, hostname::HostnameConfiguration, initsys::InitSystemConfiguration, locale::LocaleConfiguration, memory::MemoryConfiguration, modules::localip::LocalIPConfiguration, mounts::MountConfiguration, os::OSConfiguration, packages::PackagesConfiguration, processes::ProcessesConfiguration, shell::ShellConfiguration, swap::SwapConfiguration, terminal::TerminalConfiguration, uptime::UptimeConfiguration, util};
#[cfg(feature = "player")]
use crate::player::PlayerConfiguration;


#[derive(Deserialize, JsonSchema)]
#[derive(Deserialize)]
#[cfg_attr(feature = "jsonschema", derive(JsonSchema))]
pub struct Configuration {
pub modules: Vec<String>,
pub unknown_as_text: bool,
Expand Down
4 changes: 3 additions & 1 deletion src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
use std::str::FromStr;

use colored::{ColoredString, Colorize};
#[cfg(feature = "jsonschema")]
use schemars::JsonSchema;
use serde::Deserialize;

use crate::config_manager::Configuration;

// This is a hack to get the color deserializaton working
// Essentially it uses my own enum, and to print it you need to call color_string
#[derive(Deserialize, Debug, Clone, PartialEq, JsonSchema)]
#[derive(Deserialize, Debug, Clone, PartialEq)]
#[cfg_attr(feature = "jsonschema", derive(JsonSchema))]
#[serde(rename_all = "snake_case")]
pub enum CrabFetchColor {
Black,
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pub struct Args {
/// Generates a default config file
generate_config_file: bool,

#[cfg(feature = "jsonschema")]
#[arg(short('G'), long)]
generate_config_json_schema: bool,

Expand Down Expand Up @@ -279,6 +280,7 @@ fn main() {
print_bench_time(args.benchmark, args.benchmark_warn, "Generating Config File", bench);
exit(0);
}
#[cfg(feature = "jsonschema")]
if args.generate_config_json_schema {
let bench: Option<Instant> = benchmark_point(args.benchmark);
let schema_gen = schemars::gen::SchemaGenerator::default();
Expand Down
4 changes: 3 additions & 1 deletion src/modules/battery.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{fs::{self, DirEntry, ReadDir}, path::PathBuf};

#[cfg(feature = "jsonschema")]
use schemars::JsonSchema;
use serde::Deserialize;

Expand All @@ -9,7 +10,8 @@ pub struct BatteryInfo {
index: String,
percentage: f32,
}
#[derive(Deserialize, JsonSchema)]
#[derive(Deserialize)]
#[cfg_attr(feature = "jsonschema", derive(JsonSchema))]
pub struct BatteryConfiguration {
pub title: String,
pub title_color: Option<CrabFetchColor>,
Expand Down
4 changes: 3 additions & 1 deletion src/modules/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{fs::{read_dir, File, ReadDir}, io::{BufRead, BufReader, Read}, path::{
use {android_system_properties::AndroidSystemProperties, std::env};
#[cfg(target_arch = "x86_64")]
use raw_cpuid::CpuId;
#[cfg(feature = "jsonschema")]
use schemars::JsonSchema;
use serde::Deserialize;

Expand All @@ -18,7 +19,8 @@ pub struct CPUInfo {
max_clock_mhz: f32,
arch: String
}
#[derive(Deserialize, JsonSchema)]
#[derive(Deserialize)]
#[cfg_attr(feature = "jsonschema", derive(JsonSchema))]
pub struct CPUConfiguration {
pub title: String,
pub title_color: Option<CrabFetchColor>,
Expand Down
4 changes: 3 additions & 1 deletion src/modules/datetime.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use chrono::{DateTime, Local};
#[cfg(feature = "jsonschema")]
use schemars::JsonSchema;
use serde::Deserialize;

Expand All @@ -7,7 +8,8 @@ use crate::{formatter::CrabFetchColor, config_manager::Configuration, module::Mo
pub struct DateTimeInfo {
datetime: DateTime<Local>,
}
#[derive(Deserialize, JsonSchema)]
#[derive(Deserialize)]
#[cfg_attr(feature = "jsonschema", derive(JsonSchema))]
pub struct DateTimeConfiguration {
pub title: String,
pub title_color: Option<CrabFetchColor>,
Expand Down
4 changes: 3 additions & 1 deletion src/modules/desktop.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::env;

#[cfg(feature = "jsonschema")]
use schemars::JsonSchema;
use serde::Deserialize;

Expand All @@ -9,7 +10,8 @@ pub struct DesktopInfo {
desktop: String,
display_type: String
}
#[derive(Deserialize, JsonSchema)]
#[derive(Deserialize)]
#[cfg_attr(feature = "jsonschema", derive(JsonSchema))]
pub struct DesktopConfiguration {
pub title: String,
pub title_color: Option<CrabFetchColor>,
Expand Down
4 changes: 3 additions & 1 deletion src/modules/displays.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use core::str;
use std::{collections::HashMap, env, fs::{self, read_dir, ReadDir}};

#[cfg(feature = "jsonschema")]
use schemars::JsonSchema;
use serde::Deserialize;
use wayland_client::{protocol::{wl_output::{self, Transform}, wl_registry}, ConnectError, Connection, Dispatch, QueueHandle, WEnum};
Expand Down Expand Up @@ -32,7 +33,8 @@ impl DisplayInfo {
}
}

#[derive(Deserialize, JsonSchema)]
#[derive(Deserialize)]
#[cfg_attr(feature = "jsonschema", derive(JsonSchema))]
pub struct DisplayConfiguration {
pub title: String,
pub title_color: Option<CrabFetchColor>,
Expand Down
4 changes: 3 additions & 1 deletion src/modules/editor.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::env;

#[cfg(feature = "jsonschema")]
use schemars::JsonSchema;
use serde::Deserialize;

Expand All @@ -10,7 +11,8 @@ pub struct EditorInfo {
path: String,
version: String
}
#[derive(Deserialize, JsonSchema)]
#[derive(Deserialize)]
#[cfg_attr(feature = "jsonschema", derive(JsonSchema))]
pub struct EditorConfiguration {
pub title: String,
pub title_color: Option<CrabFetchColor>,
Expand Down
4 changes: 3 additions & 1 deletion src/modules/gpu.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use core::str;
use std::{fs::{self, File, ReadDir}, io::{BufRead, BufReader}, path::Path};

#[cfg(feature = "jsonschema")]
use schemars::JsonSchema;
use serde::Deserialize;

Expand All @@ -13,7 +14,8 @@ pub struct GPUInfo {
model: String,
vram_mb: u32,
}
#[derive(Deserialize, JsonSchema)]
#[derive(Deserialize)]
#[cfg_attr(feature = "jsonschema", derive(JsonSchema))]
pub struct GPUConfiguration {
pub amd_accuracy: bool,
pub ignore_disabled_gpus: bool,
Expand Down
4 changes: 3 additions & 1 deletion src/modules/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::path::Path;

#[cfg(feature = "android")]
use {android_system_properties::AndroidSystemProperties, std::env};
#[cfg(feature = "jsonschema")]
use schemars::JsonSchema;
use serde::Deserialize;

Expand All @@ -12,7 +13,8 @@ pub struct HostInfo {
host: String,
chassis: String
}
#[derive(Deserialize, JsonSchema)]
#[derive(Deserialize)]
#[cfg_attr(feature = "jsonschema", derive(JsonSchema))]
pub struct HostConfiguration {
pub title: String,
pub format: String,
Expand Down
4 changes: 3 additions & 1 deletion src/modules/hostname.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use core::str;
use std::{env, process::Command};

#[cfg(feature = "jsonschema")]
use schemars::JsonSchema;
use serde::Deserialize;

Expand All @@ -10,7 +11,8 @@ pub struct HostnameInfo {
username: String,
hostname: String,
}
#[derive(Deserialize, JsonSchema)]
#[derive(Deserialize)]
#[cfg_attr(feature = "jsonschema", derive(JsonSchema))]
pub struct HostnameConfiguration {
pub title: String,
pub title_color: Option<CrabFetchColor>,
Expand Down
4 changes: 3 additions & 1 deletion src/modules/initsys.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use core::str;
use std::fs;

#[cfg(feature = "jsonschema")]
use schemars::JsonSchema;
use serde::Deserialize;

Expand All @@ -11,7 +12,8 @@ pub struct InitSystemInfo {
path: String,
version: String
}
#[derive(Deserialize, JsonSchema)]
#[derive(Deserialize)]
#[cfg_attr(feature = "jsonschema", derive(JsonSchema))]
pub struct InitSystemConfiguration {
pub title: String,
pub format: String,
Expand Down
4 changes: 3 additions & 1 deletion src/modules/locale.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::env;

#[cfg(feature = "jsonschema")]
use schemars::JsonSchema;
use serde::Deserialize;

Expand All @@ -9,7 +10,8 @@ pub struct LocaleInfo {
language: String,
encoding: String,
}
#[derive(Deserialize, JsonSchema)]
#[derive(Deserialize)]
#[cfg_attr(feature = "jsonschema", derive(JsonSchema))]
pub struct LocaleConfiguration {
pub title: String,
pub title_color: Option<CrabFetchColor>,
Expand Down
4 changes: 3 additions & 1 deletion src/modules/localip.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{fs::{self, ReadDir}, mem, net::{IpAddr, Ipv4Addr, Ipv6Addr}};

#[cfg(feature = "jsonschema")]
use schemars::JsonSchema;
use serde::Deserialize;

Expand All @@ -9,7 +10,8 @@ pub struct LocalIPInfo {
interface: String,
ip_addr: String,
}
#[derive(Deserialize, JsonSchema)]
#[derive(Deserialize)]
#[cfg_attr(feature = "jsonschema", derive(JsonSchema))]
pub struct LocalIPConfiguration {
pub title: String,
pub title_color: Option<CrabFetchColor>,
Expand Down
4 changes: 3 additions & 1 deletion src/modules/memory.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::fs::File;
use std::io::{BufRead, BufReader};

#[cfg(feature = "jsonschema")]
use schemars::JsonSchema;
use serde::Deserialize;

Expand All @@ -11,7 +12,8 @@ pub struct MemoryInfo {
max_kb: u64,
percentage: f32
}
#[derive(Deserialize, JsonSchema)]
#[derive(Deserialize)]
#[cfg_attr(feature = "jsonschema", derive(JsonSchema))]
pub struct MemoryConfiguration {
pub title: String,
pub title_color: Option<CrabFetchColor>,
Expand Down
4 changes: 3 additions & 1 deletion src/modules/mounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::mem;
use std::env;

use libc::statfs;
#[cfg(feature = "jsonschema")]
use schemars::JsonSchema;
use serde::Deserialize;

Expand All @@ -18,7 +19,8 @@ pub struct MountInfo {
space_total_kb: u64,
percent: f32
}
#[derive(Deserialize, JsonSchema)]
#[derive(Deserialize)]
#[cfg_attr(feature = "jsonschema", derive(JsonSchema))]
pub struct MountConfiguration {
pub title: String,
pub title_color: Option<CrabFetchColor>,
Expand Down
4 changes: 3 additions & 1 deletion src/modules/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::path::Path;
#[cfg(feature = "android")]
use {android_system_properties::AndroidSystemProperties, std::env};

#[cfg(feature = "jsonschema")]
use schemars::JsonSchema;
use serde::Deserialize;

Expand All @@ -14,7 +15,8 @@ pub struct OSInfo {
pub distro_id: String,
kernel: String,
}
#[derive(Deserialize, JsonSchema)]
#[derive(Deserialize)]
#[cfg_attr(feature = "jsonschema", derive(JsonSchema))]
pub struct OSConfiguration {
pub title: String,
pub title_color: Option<CrabFetchColor>,
Expand Down
4 changes: 3 additions & 1 deletion src/modules/packages.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use core::str;

use colored::{ColoredString, Colorize};
#[cfg(feature = "jsonschema")]
use schemars::JsonSchema;
use serde::Deserialize;

Expand All @@ -9,7 +10,8 @@ use crate::{config_manager::Configuration, formatter::CrabFetchColor, module::Mo
pub struct PackagesInfo {
packages: Vec<ManagerInfo>
}
#[derive(Deserialize, JsonSchema)]
#[derive(Deserialize)]
#[cfg_attr(feature = "jsonschema", derive(JsonSchema))]
pub struct PackagesConfiguration {
pub title: String,
pub title_color: Option<CrabFetchColor>,
Expand Down
4 changes: 3 additions & 1 deletion src/modules/player.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::time::Duration;

use dbus::{arg, blocking::{stdintf::org_freedesktop_dbus::Properties, Connection, Proxy}};
#[cfg(feature = "jsonschema")]
use schemars::JsonSchema;
use serde::Deserialize;

Expand All @@ -14,7 +15,8 @@ pub struct PlayerInfo {
track_artists: Vec<String>,
status: String,
}
#[derive(Deserialize, JsonSchema)]
#[derive(Deserialize)]
#[cfg_attr(feature = "jsonschema", derive(JsonSchema))]
pub struct PlayerConfiguration {
pub title: String,
pub ignore: Vec<String>,
Expand Down
4 changes: 3 additions & 1 deletion src/modules/processes.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::fs::{read_dir, ReadDir};

#[cfg(feature = "jsonschema")]
use schemars::JsonSchema;
use serde::Deserialize;

Expand All @@ -8,7 +9,8 @@ use crate::{formatter::CrabFetchColor, config_manager::Configuration, module::Mo
pub struct ProcessesInfo {
count: u32 // god forbid someone manages to hit this limit
}
#[derive(Deserialize, JsonSchema)]
#[derive(Deserialize)]
#[cfg_attr(feature = "jsonschema", derive(JsonSchema))]
pub struct ProcessesConfiguration {
pub title: String,
pub title_color: Option<CrabFetchColor>,
Expand Down
Loading

0 comments on commit 7818ea0

Please sign in to comment.