Skip to content

Commit

Permalink
WIP:
Browse files Browse the repository at this point in the history
  • Loading branch information
chasinglogic committed Nov 11, 2023
1 parent 5f772fd commit 0f053c9
Show file tree
Hide file tree
Showing 5 changed files with 205 additions and 0 deletions.
56 changes: 56 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ edition = "2021"
clap = { version = "4.2.4", features = ["derive", "cargo"] }
clap_complete = "4.2.1"
lazy_static = "1.4.0"
regex = "1.10.2"
serde = { version = "1.0.160", features = ["derive"] }
serde_json = "1.0.96"
serde_regex = "1.1.0"
serde_yaml = "0.9.21"
shellexpand = "3.1.0"
shlex = "1.1.0"
Expand Down
3 changes: 3 additions & 0 deletions src/profiles/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{fs::File, io::BufReader, path::Path};

use super::hooks::Hooks;
use super::mappings::Mapping;

#[derive(Default, Debug, Clone, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "snake_case")]
Expand Down Expand Up @@ -32,6 +33,7 @@ pub struct DFMConfig {
pub link: LinkMode,
#[serde(default = "Vec::new")]
pub modules: Vec<DFMConfig>,
pub mappings: Option<Vec<Mapping>>,
}

impl Default for DFMConfig {
Expand All @@ -44,6 +46,7 @@ impl Default for DFMConfig {
location: "".to_string(),
hooks: Hooks::new(),
modules: Vec::new(),
mappings: None,
}
}
}
Expand Down
143 changes: 143 additions & 0 deletions src/profiles/mappings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
use regex::Regex;

#[derive(Default, Debug, Clone, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
pub enum TargetOS {
Darwin,
Linux,
Windows,
Vec(Vec<TargetOS>),
#[default]
All,
}

impl TargetOS {
#[cfg(target_os = "macos")]
fn current() -> TargetOS {
TargetOS::Darwin
}

#[cfg(target_os = "linux")]
fn current() -> TargetOS {
TargetOS::Linux
}

#[cfg(target_os = "windows")]
fn current() -> TargetOS {
TargetOS::Windows
}

fn is_this_os(target: &TargetOS) -> bool {
match target {
&TargetOS::All => true,
TargetOS::Vec(targets) => targets
.into_iter()
.find(|t| TargetOS::is_this_os(*t))
.is_some(),
desired => desired == &TargetOS::current(),
}
}
}

fn default_off() -> bool {
false
}

#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
pub struct Mapping {
#[serde(rename = "match", with = "serde_regex")]
term: Regex,
#[serde(default = "default_off")]
link_as_dir: bool,
#[serde(default = "default_off")]
skip: bool,
#[serde(default)]
target_os: TargetOS,
#[serde(default)]
dest: String,
#[serde(default)]
target_dir: String,
}

impl Mapping {
fn new(term: &str) -> Mapping {
Mapping {
term: Regex::new(term).expect("Unable to compile regex!"),
link_as_dir: false,
skip: false,
target_os: TargetOS::All,
dest: "".to_string(),
target_dir: "".to_string(),
}
}

fn skip(mut self) -> Mapping {
self.skip = true;
self
}

fn link_as_dir(mut self) -> Mapping {
self.link_as_dir = true;
self
}

fn dest(mut self, new_dest: String) -> Mapping {
self.dest = new_dest;
self
}

fn target_os(mut self, new_target: TargetOS) -> Mapping {
self.target_os = new_target;
self
}

fn does_match(&self, path: &str) -> bool {
if self.term.is_match(path) {
return TargetOS::is_this_os(&self.target_os);
}

false
}
}

pub struct Mapper {
mappings: Vec<Mapping>,
}

pub enum MapAction {
NewDest(String),
NewTargetDir(String),
Skip,
None,
}

impl From<Vec<Mapping>> for Mapper {
fn from(mappings: Vec<Mapping>) -> Mapper {
Mapper { mappings }
}
}

impl From<Option<Vec<Mapping>>> for Mapper {
fn from(mappings: Option<Vec<Mapping>>) -> Mapper {
let configured: Vec<Mapping> = match mappings {
Some(configured) => configured,
None => vec![
Mapping::new("README.*").skip(),
Mapping::new("LICENSE").skip(),
],
};

Mapper::from(configured)
}
}

impl Mapper {
fn get_mapped_action(&self, relative_path: &str) -> MapAction {
for mapping in &self.mappings {
if !mapping.does_match(relative_path) {
continue;
}
}

MapAction::None
}
}
1 change: 1 addition & 0 deletions src/profiles/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod config;
mod hooks;
mod mappings;
mod profile;

pub use profile::Profile;

0 comments on commit 0f053c9

Please sign in to comment.