-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from yoshihitoh/feature/refactor-refmt
Refactor project structure
- Loading branch information
Showing
22 changed files
with
1,106 additions
and
788 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,26 @@ | ||
[package] | ||
name = "refmt" | ||
version = "0.1.4" | ||
version = "0.2.0" | ||
authors = ["yoshihitoh <[email protected]>"] | ||
edition = "2018" | ||
|
||
[lib] | ||
name = "refmt_core" | ||
path = "src/lib.rs" | ||
[badges] | ||
travis-ci = { repository = "yoshihitoh/refmt" } | ||
|
||
[[bin]] | ||
name = "refmt" | ||
path = "src/bin/refmt.rs" | ||
path = "src/bin/refmt/main.rs" | ||
|
||
[[bin]] | ||
name = "refmt-generate-assets" | ||
path = "src/bin/generate_assets.rs" | ||
path = "src/bin/generate_assets/main.rs" | ||
|
||
[dependencies] | ||
ansi_term = "0.11" | ||
atty = "0.2" | ||
bincode = "1.0" | ||
env_logger = "0.6" | ||
failure = "0.1" | ||
log = "0.4" | ||
serde = "1.0" | ||
serde_yaml = "0.8" | ||
strum = "0.13" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#!/bin/bash | ||
#!/usr/bin/env bash | ||
|
||
set -exo pipefail | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#!/bin/bash | ||
#!/usr/bin/env bash | ||
|
||
set -exo pipefail | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,38 @@ | ||
use std::path::Path; | ||
use syntect::highlighting::{Theme, ThemeSet}; | ||
use syntect::parsing::{SyntaxReference, SyntaxSet}; | ||
|
||
use syntect::dumps::{dump_to_file, from_binary}; | ||
use syntect::highlighting::ThemeSet; | ||
use syntect::parsing::{SyntaxSet, SyntaxSetBuilder}; | ||
const DEFAULT_THEME: &str = "Monokai Extended"; | ||
|
||
struct AssetBuilder<'a> { | ||
assets_dir: &'a Path, | ||
} | ||
|
||
impl<'a> AssetBuilder<'a> { | ||
fn build_syntaxes(&self) -> Result<SyntaxSet, syntect::LoadingError> { | ||
let mut syntax_set_builder = SyntaxSetBuilder::new(); | ||
syntax_set_builder.add_plain_text_syntax(); | ||
syntax_set_builder.add_from_folder(self.assets_dir.join("syntaxes"), true)?; | ||
|
||
Ok(syntax_set_builder.build()) | ||
} | ||
|
||
fn build_themes(&self) -> Result<ThemeSet, syntect::LoadingError> { | ||
let mut theme_set = ThemeSet::default(); | ||
theme_set.add_from_folder(self.assets_dir.join("themes"))?; | ||
|
||
Ok(theme_set) | ||
} | ||
pub struct HighlightAssets { | ||
pub syntax_set: SyntaxSet, | ||
pub theme_set: ThemeSet, | ||
} | ||
|
||
struct AssetLoader {} | ||
|
||
impl AssetLoader { | ||
fn load_syntaxes(&self) -> SyntaxSet { | ||
from_binary(include_bytes!("../assets/syntaxes.bin")) | ||
impl HighlightAssets { | ||
pub fn new(syntax_set: SyntaxSet, theme_set: ThemeSet) -> HighlightAssets { | ||
HighlightAssets { | ||
syntax_set, | ||
theme_set, | ||
} | ||
} | ||
|
||
fn load_themes(&self) -> ThemeSet { | ||
from_binary(include_bytes!("../assets/themes.bin")) | ||
pub fn get_syntax(&self, name: &str) -> &SyntaxReference { | ||
self.syntax_set.find_syntax_by_extension(name).unwrap() | ||
} | ||
} | ||
|
||
impl Default for AssetLoader { | ||
fn default() -> Self { | ||
AssetLoader {} | ||
pub fn syntaxes(&self) -> &[SyntaxReference] { | ||
self.syntax_set.syntaxes() | ||
} | ||
} | ||
|
||
pub struct HighlightAssets { | ||
pub syntax_set: SyntaxSet, | ||
pub theme_set: ThemeSet, | ||
} | ||
|
||
impl HighlightAssets { | ||
pub fn load_integrated() -> Result<HighlightAssets, syntect::LoadingError> { | ||
let loader = AssetLoader::default(); | ||
Ok(HighlightAssets { | ||
syntax_set: loader.load_syntaxes(), | ||
theme_set: loader.load_themes(), | ||
}) | ||
pub fn get_default_theme(&self) -> &Theme { | ||
self.get_theme(DEFAULT_THEME) | ||
} | ||
|
||
pub fn build_from_files(assets_dir: &Path) -> Result<HighlightAssets, syntect::LoadingError> { | ||
let builder = AssetBuilder { assets_dir }; | ||
Ok(HighlightAssets { | ||
syntax_set: builder.build_syntaxes()?, | ||
theme_set: builder.build_themes()?, | ||
}) | ||
pub fn get_theme(&self, name: &str) -> &Theme { | ||
&self.theme_set.themes[name] | ||
} | ||
|
||
pub fn save(&self, assets_dir: &Path) -> Result<(), bincode::Error> { | ||
// TODO: Remove bincode from dependencies. It is only used for this error handling. | ||
dump_to_file(&self.syntax_set, assets_dir.join("syntaxes.bin"))?; | ||
dump_to_file(&self.theme_set, assets_dir.join("themes.bin"))?; | ||
|
||
Ok(()) | ||
pub fn themes(&self) -> Vec<&Theme> { | ||
self.theme_set.themes.iter().map(|(_, v)| v).collect() | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
use std::path::Path; | ||
|
||
use clap::{App, Arg}; | ||
use failure::ResultExt; | ||
use syntect::dumps::dump_to_file; | ||
use syntect::highlighting::ThemeSet; | ||
use syntect::parsing::{SyntaxSet, SyntaxSetBuilder}; | ||
|
||
use refmt::assets::HighlightAssets; | ||
use refmt::errors; | ||
|
||
struct AssetBuilder {} | ||
|
||
impl AssetBuilder { | ||
pub fn build_from_files( | ||
syntaxes_dir: &Path, | ||
themes_dir: &Path, | ||
) -> Result<HighlightAssets, errors::Error> { | ||
let builder = AssetBuilder::default(); | ||
Ok(HighlightAssets { | ||
syntax_set: builder.build_syntaxes(syntaxes_dir)?, | ||
theme_set: builder.build_themes(themes_dir)?, | ||
}) | ||
} | ||
|
||
pub fn save(assets: HighlightAssets, assets_dir: &Path) -> Result<(), errors::Error> { | ||
dump_to_file(&assets.syntax_set, assets_dir.join("syntaxes.bin")) | ||
.context(errors::ErrorKind::CreatingAssets)?; | ||
dump_to_file(&assets.theme_set, assets_dir.join("themes.bin")) | ||
.context(errors::ErrorKind::CreatingAssets)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
fn build_syntaxes(&self, syntaxes_dir: &Path) -> Result<SyntaxSet, errors::Error> { | ||
let mut syntax_set_builder = SyntaxSetBuilder::new(); | ||
syntax_set_builder.add_plain_text_syntax(); | ||
syntax_set_builder | ||
.add_from_folder(syntaxes_dir, true) | ||
.context(errors::ErrorKind::CreatingAssets)?; | ||
|
||
Ok(syntax_set_builder.build()) | ||
} | ||
|
||
fn build_themes(&self, themes_dir: &Path) -> Result<ThemeSet, errors::Error> { | ||
let mut theme_set = ThemeSet::default(); | ||
theme_set | ||
.add_from_folder(themes_dir) | ||
.context(errors::ErrorKind::CreatingAssets)?; | ||
|
||
Ok(theme_set) | ||
} | ||
} | ||
|
||
impl Default for AssetBuilder { | ||
fn default() -> Self { | ||
AssetBuilder {} | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
struct ProgramOption { | ||
assets_dir: String, | ||
} | ||
|
||
fn parse_args() -> ProgramOption { | ||
let m = App::new("generate_assets") | ||
.arg( | ||
Arg::with_name("ASSETS_DIR") | ||
.long("assets-dir") | ||
.help("Set path to assets directory") | ||
.takes_value(true) | ||
.value_name("ASSETS_DIR") | ||
.required(true), | ||
) | ||
.get_matches(); | ||
|
||
ProgramOption { | ||
assets_dir: m.value_of("ASSETS_DIR").unwrap().to_string(), | ||
} | ||
} | ||
|
||
fn main() -> Result<(), errors::Error> { | ||
let options = parse_args(); | ||
let assets_dir = Path::new(&options.assets_dir); | ||
let assets = | ||
AssetBuilder::build_from_files(&assets_dir.join("syntaxes"), &assets_dir.join("themes"))?; | ||
AssetBuilder::save(assets, Path::new(&options.assets_dir))?; | ||
Ok(()) | ||
} |
Oops, something went wrong.