1mod assets;
2mod color;
3mod util;
4mod vscode;
5
6use std::fs::File;
7use std::path::PathBuf;
8
9use anyhow::{Context, Result};
10use clap::{Parser, Subcommand};
11use indexmap::IndexMap;
12use log::LevelFilter;
13use schemars::schema_for;
14use serde::Deserialize;
15use simplelog::{TermLogger, TerminalMode};
16use theme::{Appearance, AppearanceContent, ThemeFamilyContent};
17
18use crate::vscode::VsCodeTheme;
19use crate::vscode::VsCodeThemeConverter;
20
21#[derive(Debug, Deserialize)]
22struct FamilyMetadata {
23 pub name: String,
24 pub author: String,
25 pub themes: Vec<ThemeMetadata>,
26
27 /// Overrides for specific syntax tokens.
28 ///
29 /// Use this to ensure certain Zed syntax tokens are matched
30 /// to an exact set of scopes when it is not otherwise possible
31 /// to rely on the default mappings in the theme importer.
32 #[serde(default)]
33 pub syntax: IndexMap<String, Vec<String>>,
34}
35
36#[derive(Debug, Clone, Copy, Deserialize)]
37#[serde(rename_all = "snake_case")]
38pub enum ThemeAppearanceJson {
39 Light,
40 Dark,
41}
42
43impl From<ThemeAppearanceJson> for AppearanceContent {
44 fn from(value: ThemeAppearanceJson) -> Self {
45 match value {
46 ThemeAppearanceJson::Light => Self::Light,
47 ThemeAppearanceJson::Dark => Self::Dark,
48 }
49 }
50}
51
52impl From<ThemeAppearanceJson> for Appearance {
53 fn from(value: ThemeAppearanceJson) -> Self {
54 match value {
55 ThemeAppearanceJson::Light => Self::Light,
56 ThemeAppearanceJson::Dark => Self::Dark,
57 }
58 }
59}
60
61#[derive(Debug, Deserialize)]
62pub struct ThemeMetadata {
63 pub name: String,
64 pub file_name: String,
65 pub appearance: ThemeAppearanceJson,
66}
67
68#[derive(Parser)]
69#[command(author, version, about, long_about = None)]
70struct Args {
71 /// The path to the theme to import.
72 theme_path: PathBuf,
73
74 /// Whether to warn when values are missing from the theme.
75 #[arg(long)]
76 warn_on_missing: bool,
77
78 #[command(subcommand)]
79 command: Option<Command>,
80}
81
82#[derive(Subcommand)]
83enum Command {
84 /// Prints the JSON schema for a theme.
85 PrintSchema,
86}
87
88fn main() -> Result<()> {
89 let args = Args::parse();
90
91 let log_config = {
92 let mut config = simplelog::ConfigBuilder::new();
93 config
94 .set_level_color(log::Level::Trace, simplelog::Color::Cyan)
95 .set_level_color(log::Level::Info, simplelog::Color::Blue)
96 .set_level_color(log::Level::Warn, simplelog::Color::Yellow)
97 .set_level_color(log::Level::Error, simplelog::Color::Red);
98
99 if !args.warn_on_missing {
100 config.add_filter_ignore_str("theme_printer");
101 }
102
103 config.build()
104 };
105
106 TermLogger::init(LevelFilter::Trace, log_config, TerminalMode::Mixed)
107 .expect("could not initialize logger");
108
109 if let Some(command) = args.command {
110 match command {
111 Command::PrintSchema => {
112 let theme_family_schema = schema_for!(ThemeFamilyContent);
113
114 println!(
115 "{}",
116 serde_json::to_string_pretty(&theme_family_schema).unwrap()
117 );
118
119 return Ok(());
120 }
121 }
122 }
123
124 let theme_file_path = args.theme_path;
125
126 let theme_file = match File::open(&theme_file_path) {
127 Ok(file) => file,
128 Err(err) => {
129 log::info!("Failed to open file at path: {:?}", theme_file_path);
130 return Err(err)?;
131 }
132 };
133
134 let vscode_theme: VsCodeTheme = serde_json_lenient::from_reader(theme_file)
135 .context(format!("failed to parse theme {theme_file_path:?}"))?;
136
137 let theme_metadata = ThemeMetadata {
138 name: vscode_theme.name.clone().unwrap_or("".to_string()),
139 appearance: ThemeAppearanceJson::Dark,
140 file_name: "".to_string(),
141 };
142
143 let converter = VsCodeThemeConverter::new(vscode_theme, theme_metadata, IndexMap::new());
144
145 let theme = converter.convert()?;
146
147 let theme_json = serde_json::to_string_pretty(&theme).unwrap();
148
149 println!("{}", theme_json);
150
151 log::info!("Done!");
152
153 Ok(())
154}