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;
11use indexmap::IndexMap;
12use json_comments::StripComments;
13use log::LevelFilter;
14use serde::Deserialize;
15use simplelog::{TermLogger, TerminalMode};
16use theme::{Appearance, AppearanceContent};
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
79fn main() -> Result<()> {
80 let args = Args::parse();
81
82 let log_config = {
83 let mut config = simplelog::ConfigBuilder::new();
84 config
85 .set_level_color(log::Level::Trace, simplelog::Color::Cyan)
86 .set_level_color(log::Level::Info, simplelog::Color::Blue)
87 .set_level_color(log::Level::Warn, simplelog::Color::Yellow)
88 .set_level_color(log::Level::Error, simplelog::Color::Red);
89
90 if !args.warn_on_missing {
91 config.add_filter_ignore_str("theme_printer");
92 }
93
94 config.build()
95 };
96
97 TermLogger::init(LevelFilter::Trace, log_config, TerminalMode::Mixed)
98 .expect("could not initialize logger");
99
100 let theme_file_path = args.theme_path;
101
102 let theme_file = match File::open(&theme_file_path) {
103 Ok(file) => file,
104 Err(err) => {
105 log::info!("Failed to open file at path: {:?}", theme_file_path);
106 return Err(err)?;
107 }
108 };
109
110 let theme_without_comments = StripComments::new(theme_file);
111 let vscode_theme: VsCodeTheme = serde_json::from_reader(theme_without_comments)
112 .context(format!("failed to parse theme {theme_file_path:?}"))?;
113
114 let theme_metadata = ThemeMetadata {
115 name: "".to_string(),
116 appearance: ThemeAppearanceJson::Dark,
117 file_name: "".to_string(),
118 };
119
120 let converter = VsCodeThemeConverter::new(vscode_theme, theme_metadata, IndexMap::new());
121
122 let theme = converter.convert()?;
123
124 let theme_json = serde_json::to_string_pretty(&theme).unwrap();
125
126 println!("{}", theme_json);
127
128 log::info!("Done!");
129
130 Ok(())
131}