Skip to content

Commit

Permalink
refactor: extract schemas to own modules
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandre-abrioux committed Nov 13, 2024
1 parent 07148d7 commit 83a4be7
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 45 deletions.
24 changes: 23 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@
use crate::schema::Config;
use indexmap::IndexMap;
use serde::Deserialize;
use serde_yml::from_str;
use std::fs;

#[derive(Deserialize, Debug, PartialEq)]
pub struct Config {
pub tools: Tools,
}

pub type Tools = IndexMap<String, Vec<ToolEnum>>;

#[derive(Deserialize, Debug, PartialEq)]
#[serde(untagged)]
pub enum ToolEnum {
StringLike(String),
StructLike(ToolConfig),
}

#[derive(Deserialize, Debug, PartialEq)]
pub struct ToolConfig {
pub label: String,
pub color: Option<String>,
pub icon: Option<String>,
}

pub fn load_config(config_file_path: &String) -> Config {
let content = fs::read_to_string(&config_file_path).expect("Configuration file not found");
load_config_from_content(&content)
Expand Down
4 changes: 1 addition & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
mod config;
mod markdown;
mod schema;
mod simple_icons;
mod tool;

use crate::config::load_config;
use crate::schema::Config;
use crate::config::{load_config, Config};
use clap::Parser;
use std::fs;

Expand Down
8 changes: 4 additions & 4 deletions src/markdown.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::schema::{Tool, ToolEnum, Tools};
use crate::config::{ToolEnum, Tools};
use crate::simple_icons::color::is_relatively_light_icon_hex;
use crate::tool::{generate_tool_from_slug, generate_tool_from_struct};
use crate::tool::{generate_tool_from_config, generate_tool_from_slug, Tool};
use markdown_table_formatter::format_tables;

pub fn generate_markdown(tools: &Tools) -> String {
Expand Down Expand Up @@ -60,8 +60,8 @@ fn generate_img_tag(tool_enum: &ToolEnum) -> String {
ToolEnum::StringLike(slug) => {
tool = generate_tool_from_slug(slug);
}
ToolEnum::StructLike(tool_yaml) => {
tool = generate_tool_from_struct(tool_yaml);
ToolEnum::StructLike(tool_config) => {
tool = generate_tool_from_config(tool_config);
}
}
generate_img_tag_from_tool(&tool)
Expand Down
29 changes: 0 additions & 29 deletions src/schema.rs

This file was deleted.

22 changes: 14 additions & 8 deletions src/tool.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
use crate::schema::{Tool, ToolYaml};
use crate::config::ToolConfig;
use crate::simple_icons::SimpleIcons;

pub fn generate_tool_from_struct(tool_yaml: &ToolYaml) -> Tool {
pub struct Tool {
pub label: String,
pub color: String,
pub icon: Option<String>,
}

pub fn generate_tool_from_config(tool_config: &ToolConfig) -> Tool {
let mut color_option: Option<String> = None;
if tool_yaml.color.is_some() {
color_option = Some(tool_yaml.color.clone().unwrap().replace("#", ""))
} else if tool_yaml.icon.is_some() {
let tool_from_slug = generate_tool_from_slug(&tool_yaml.icon.clone().unwrap());
if tool_config.color.is_some() {
color_option = Some(tool_config.color.clone().unwrap().replace("#", ""))
} else if tool_config.icon.is_some() {
let tool_from_slug = generate_tool_from_slug(&tool_config.icon.clone().unwrap());
color_option = Some(tool_from_slug.color)
}
let label = tool_yaml.label.clone();
let label = tool_config.label.clone();
let color = color_option
.expect(format!("missing color or icon for item {label}").as_str())
.to_string();
Tool {
label,
color,
icon: tool_yaml.icon.clone(),
icon: tool_config.icon.clone(),
}
}

Expand Down

0 comments on commit 83a4be7

Please sign in to comment.