Simply source filtering and started config
This commit is contained in:
21
Cargo.lock
generated
21
Cargo.lock
generated
@@ -213,6 +213,26 @@ dependencies = [
|
|||||||
"unicode-ident",
|
"unicode-ident",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "thiserror"
|
||||||
|
version = "2.0.18"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "4288b5bcbc7920c07a1149a35cf9590a2aa808e0bc1eafaade0b80947865fbc4"
|
||||||
|
dependencies = [
|
||||||
|
"thiserror-impl",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "thiserror-impl"
|
||||||
|
version = "2.0.18"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ebc4ee7f67670e9b64d05fa4253e753e016c6c95ff35b89b7941d6b856dec1d5"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "toml"
|
name = "toml"
|
||||||
version = "1.0.6+spec-1.1.0"
|
version = "1.0.6+spec-1.1.0"
|
||||||
@@ -264,6 +284,7 @@ version = "0.1.0"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"notify",
|
"notify",
|
||||||
"serde",
|
"serde",
|
||||||
|
"thiserror",
|
||||||
"toml",
|
"toml",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|||||||
@@ -6,4 +6,5 @@ edition = "2024"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
notify = "8.2.0"
|
notify = "8.2.0"
|
||||||
serde = { version = "1.0.228", features = ["derive"] }
|
serde = { version = "1.0.228", features = ["derive"] }
|
||||||
|
thiserror = "2.0.18"
|
||||||
toml = "1.0.6"
|
toml = "1.0.6"
|
||||||
|
|||||||
116
src/main.rs
116
src/main.rs
@@ -1,34 +1,94 @@
|
|||||||
use std::{
|
use std::{
|
||||||
env,
|
env, fs,
|
||||||
path::{Path, PathBuf},
|
path::{self, Path, PathBuf},
|
||||||
sync::mpsc,
|
sync::mpsc,
|
||||||
};
|
};
|
||||||
|
|
||||||
use notify::{Event, RecursiveMode, Result, Watcher};
|
use notify::{Event, EventKind, RecursiveMode, Watcher, event::CreateKind};
|
||||||
use serde::Deserialize;
|
use serde::{Deserialize, Serialize};
|
||||||
|
use thiserror::Error;
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize, Serialize)]
|
||||||
struct Config {
|
struct Config {
|
||||||
libraries: LibConfig,
|
libraries: Option<LibConfig>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Config {}
|
impl Config {
|
||||||
|
fn load_or_create() -> Result<Self, ConfigError> {
|
||||||
|
let config_path =
|
||||||
|
PathBuf::from(env::var("HOME").unwrap()).join(".config/uniloader/config.toml");
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
if config_path.exists() {
|
||||||
|
let contents = fs::read_to_string(&config_path).map_err(|_| ConfigError::ConfigErr)?;
|
||||||
|
let config: Config = toml::from_str(&contents).map_err(|_| ConfigError::ConfigErr)?;
|
||||||
|
Ok(config)
|
||||||
|
} else {
|
||||||
|
fs::create_dir_all(config_path.parent().unwrap())
|
||||||
|
.map_err(|_| ConfigError::ConfigErr)?;
|
||||||
|
let config = Config { libraries: None };
|
||||||
|
let contents = toml::to_string_pretty(&config).map_err(|_| ConfigError::ConfigErr)?;
|
||||||
|
fs::write(&config_path, contents).map_err(|_| ConfigError::ConfigErr)?;
|
||||||
|
Ok(config)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, Serialize, Clone)]
|
||||||
struct LibConfig {
|
struct LibConfig {
|
||||||
component_search_engine_symbols: PathBuf,
|
symacsys_symbols: Option<PathBuf>,
|
||||||
component_serach_engine_footprints: PathBuf,
|
symacsys_footprints: Option<PathBuf>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
enum SourceType {
|
||||||
let (tx, rx) = mpsc::channel::<Result<Event>>();
|
SymacSys,
|
||||||
|
UltraLibrarian,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Error, Debug)]
|
||||||
|
enum ConfigError {
|
||||||
|
#[error("Config error")]
|
||||||
|
ConfigErr,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Error, Debug)]
|
||||||
|
enum SourceError {
|
||||||
|
#[error("Unknown Source")]
|
||||||
|
SourceUnknown,
|
||||||
|
#[error("Filename Error")]
|
||||||
|
FilenameError,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() -> notify::Result<()> {
|
||||||
|
let config = Config::load_or_create().unwrap();
|
||||||
|
if config.libraries.is_some() {
|
||||||
|
if config.libraries.clone().unwrap().symacsys_symbols.is_some() {
|
||||||
|
println!("{:?}", config.libraries.unwrap().symacsys_symbols.unwrap());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let (tx, rx) = mpsc::channel::<notify::Result<Event>>();
|
||||||
let mut watcher = notify::recommended_watcher(tx)?;
|
let mut watcher = notify::recommended_watcher(tx)?;
|
||||||
let downloads = PathBuf::from(env::var("HOME").unwrap()).join("Downloads");
|
let downloads = PathBuf::from(env::var("HOME").unwrap()).join("Downloads");
|
||||||
watcher.watch(&downloads, RecursiveMode::Recursive)?;
|
watcher.watch(&downloads, RecursiveMode::NonRecursive)?;
|
||||||
|
|
||||||
for res in rx {
|
for res in rx {
|
||||||
match res {
|
match res {
|
||||||
Ok(event) => println!("event: {:?}", event.paths[0]),
|
Ok(event) => {
|
||||||
|
if event.kind != EventKind::Create(CreateKind::File) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if !validate_zip(&event.paths[0]) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let Ok(source_type) = find_source_type(&event.paths[0]) else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
|
||||||
|
match source_type {
|
||||||
|
SourceType::SymacSys => println!("SymacSys detected"),
|
||||||
|
SourceType::UltraLibrarian => println!("UltraLibrarian detected"),
|
||||||
|
}
|
||||||
|
}
|
||||||
Err(e) => println!("watch error: {:?}", e),
|
Err(e) => println!("watch error: {:?}", e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -36,9 +96,27 @@ fn main() -> Result<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn validate_lib(path: PathBuf) -> bool {
|
fn validate_zip(path: &Path) -> bool {
|
||||||
if path.is_file() {
|
path.is_file()
|
||||||
let filename = path.file_name();
|
&& path
|
||||||
}
|
.extension()
|
||||||
return false;
|
.and_then(|e| e.to_str())
|
||||||
|
.map(|e| e.eq_ignore_ascii_case("zip"))
|
||||||
|
.unwrap_or(false)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn find_source_type(path: &Path) -> Result<SourceType, SourceError> {
|
||||||
|
let name = path
|
||||||
|
.file_name()
|
||||||
|
.ok_or(SourceError::FilenameError)?
|
||||||
|
.to_str()
|
||||||
|
.ok_or(SourceError::FilenameError)?;
|
||||||
|
|
||||||
|
if name.starts_with("LIB_") {
|
||||||
|
Ok(SourceType::SymacSys)
|
||||||
|
} else if name.starts_with("ul_") {
|
||||||
|
Ok(SourceType::UltraLibrarian)
|
||||||
|
} else {
|
||||||
|
Err(SourceError::SourceUnknown)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user