Skip to content

Commit bb9ee36

Browse files
CaptainMasosamuel-mason-novecom
authored andcommitted
Changes
1 parent bf5d4cf commit bb9ee36

File tree

9 files changed

+247
-18
lines changed

9 files changed

+247
-18
lines changed

ll-core/src/cse.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ use super::{
22
config::Config,
33
format::{
44
Files,
5-
ECAD
5+
ECAD,
6+
Output
67
},
78
epw::Epw,
89
error::LLResult,
910
cse_result::CSEResult,
11+
cse_result::CSEUnprocessedResult,
1012
consts::COMPONENT_SEARCH_ENGINE_URL,
1113
new_err
1214
};
@@ -104,13 +106,13 @@ impl CSE {
104106
false => filename.replace(".zip", "")
105107
};
106108

107-
self.unzip(lib_name, body)
108-
109+
let mut v = self.unzip(lib_name, body)?;
110+
self.process(&mut v)
109111
}
110112

111113
}
112114

113-
fn unzip(&self, lib_name: String, data: Vec<u8>) -> LLResult<CSEResult> {
115+
fn unzip(&self, lib_name: String, data: Vec<u8>) -> LLResult<CSEUnprocessedResult> {
114116

115117
let reader = std::io::Cursor::new(&data);
116118
let mut archive = zip::ZipArchive::new(reader)?;
@@ -130,11 +132,27 @@ impl CSE {
130132
false => PathBuf::from(&self.config.settings.output_path)
131133
};
132134

133-
Ok(CSEResult {
135+
Ok(CSEUnprocessedResult {
134136
output_path: path.to_string_lossy().to_string(),
135137
files: files
136138
})
137139

138140
}
139141

142+
fn process(&self,rawres : &mut CSEUnprocessedResult) -> LLResult<CSEResult>
143+
{
144+
let mut output_files = Files::new();
145+
146+
for (filename, mut file) in rawres.files.iter_mut()
147+
{
148+
self.config.settings.format.process(rawres.output_path.clone(), &mut output_files, filename.clone(), &mut file)?;
149+
}
150+
151+
Ok(CSEResult
152+
{
153+
output_path : rawres.output_path.clone(),
154+
files : output_files,
155+
})
156+
}
157+
140158
}

ll-core/src/cse_result.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ use std::{
1515
// pub data: Vec<u8>
1616
// }
1717

18+
pub struct CSEUnprocessedResult
19+
{
20+
pub output_path: String,
21+
pub files: HashMap<String, Vec<u8>>
22+
}
23+
1824
pub struct CSEResult {
1925
pub output_path: String,
2026
pub files: HashMap<String, Vec<u8>>
@@ -28,12 +34,15 @@ impl CSEResult {
2834

2935
if &self.files.len() > &0 {
3036

31-
if !save_dir.exists() {
32-
fs::create_dir_all(save_dir)?;
33-
}
34-
3537
for (filename, data) in &self.files {
3638
let path = save_dir.join(filename);
39+
let dir = path.parent().unwrap();
40+
41+
if !dir.exists()
42+
{
43+
fs::create_dir_all(dir)?;
44+
}
45+
3746
Self::write(path, data.to_vec())?;
3847
}
3948

@@ -51,10 +60,6 @@ impl CSEResult {
5160

5261
let p = path.to_str().unwrap().to_string();
5362

54-
if path.exists() {
55-
return Err(new_err!(format!("{} already exists!", p)));
56-
}
57-
5863
match fs::write(&path, &data) {
5964
Ok(_) => Ok(p),
6065
Err(e) => Err(new_err!(e))

ll-core/src/format/mod.rs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
mod extractors;
2+
mod processors;
23
use zip::read::ZipFile;
34
use super::error::LLResult;
45

@@ -14,12 +15,20 @@ pub enum ECAD {
1415
ZIP
1516
}
1617

18+
#[derive(PartialEq,Debug, Clone)]
19+
pub enum Output
20+
{
21+
File(&'static str),
22+
Folder(&'static str)
23+
}
24+
1725
#[derive(Debug, Clone, PartialEq)]
1826
pub struct Format {
1927
pub name: String,
2028
pub ecad: ECAD,
2129
pub create_folder: bool,
2230
match_path: Vec<&'static str>,
31+
output : Vec<Output>,
2332
ignore: Vec<&'static str>
2433
}
2534

@@ -35,35 +44,40 @@ impl Format {
3544
name: f,
3645
ecad: ECAD::D3,
3746
create_folder: true,
38-
match_path: "3D",
47+
match_path: vec!["3D"],
48+
output: vec![],
3949
ignore: vec![]
4050
},
4151
"eagle" => Self {
4252
name: f,
4353
ecad: ECAD::EAGLE,
4454
create_folder: false,
4555
match_path: vec!["EAGLE/", "/3D/"],
56+
output: vec![],
4657
ignore: vec!["Readme.html"]
4758
},
4859
"easyeda" => Self {
4960
name: f,
5061
ecad: ECAD::EASYEDA,
5162
create_folder: false,
5263
match_path: vec!["EasyEDA/", "/3D/"],
64+
output: vec![],
5365
ignore: vec!["Readme.html"]
5466
},
5567
"kicad" => Self {
5668
name: f,
5769
ecad: ECAD::KICAD,
58-
create_folder: true,
70+
create_folder: false,
5971
match_path: vec!["KiCad/", "/3D/"],
72+
output: vec![Output::File("LibraryLoader.lib"), Output::File("LibraryLoader.dcm"), Output::Folder("LibraryLoader.pretty")],
6073
ignore: vec![]
6174
},
6275
"zip" => Self {
6376
name: f,
6477
ecad: ECAD::ZIP,
6578
create_folder: false,
6679
match_path: vec![""],
80+
output: vec![],
6781
ignore: vec![]
6882
},
6983
_ => {
@@ -90,4 +104,18 @@ impl Format {
90104
Ok(())
91105
}
92106

107+
pub fn process(&self, output_path : String, output_files : &mut Files, file_path : String, item : &mut Vec<u8>) -> LLResult<()>
108+
{
109+
match &self.ecad {
110+
// * Keep these in alphabetical order
111+
ECAD::D3 => processors::d3::process(&self, output_path, output_files, file_path, item)?,
112+
ECAD::EAGLE => processors::eagle::process(&self, output_path, output_files, file_path, item)?,
113+
ECAD::EASYEDA => processors::easyeda::process(&self, output_path, output_files, file_path, item)?,
114+
ECAD::KICAD => processors::kicad::process(&self, output_path, output_files, file_path, item)?,
115+
ECAD::ZIP => unreachable!("ZIP not handled!")
116+
// ! NOTE: DO NOT ADD A _ => {} CATCHER HERE!
117+
};
118+
119+
Ok(())
120+
}
93121
}

ll-core/src/format/processors/d3.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use super::*;
2+
3+
pub fn process(format: &Format, output_path : String, output_files : &mut Files, file_path : String, item : &mut Vec<u8>) -> LLResult<()> {
4+
5+
generic_processor(format, output_path, output_files, file_path, item)
6+
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use super::*;
2+
3+
pub fn process(format: &Format, output_path : String, output_files : &mut Files, file_path : String, item : &mut Vec<u8>) -> LLResult<()> {
4+
5+
generic_processor(format, output_path, output_files, file_path, item)
6+
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use super::*;
2+
3+
pub fn process(format: &Format, output_path : String, output_files : &mut Files, file_path : String, item : &mut Vec<u8>) -> LLResult<()> {
4+
5+
generic_processor(format, output_path, output_files, file_path, item)
6+
7+
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
use super::*;
2+
3+
const LIBRARY : &str = "LibraryLoader.lib";
4+
const DOCLIB : &str = "LibraryLoader.dcm";
5+
const FP_FOLDER : &str = "LibraryLoader.pretty";
6+
const U3D_FOLDER : &str = "LibraryLoader-3dmodels";
7+
8+
pub fn process(format: &Format, output_path : String, output_files : &mut Files, file_name : String, item : &mut Vec<u8>) -> LLResult<()> {
9+
10+
let file_path = PathBuf::from(output_path.clone()).join(file_name.clone());
11+
if let Some(ext) = &file_path.extension()
12+
{
13+
match ext.to_str()
14+
{
15+
Some("kicad_mod") =>
16+
{
17+
output_files.insert(format!("{}/{}", FP_FOLDER, file_name), item.clone());
18+
},
19+
Some("lib") =>
20+
{
21+
let chars = match std::str::from_utf8(&item[..])
22+
{
23+
Ok(v) => v,
24+
Err(e) => return Err(
25+
LLError::new(format!("Could not convert file to valid utf8: {} ({})", e, file_name), "ll-core/src/format/processors/kicad.rs", 24)
26+
)
27+
};
28+
29+
let mut d_out = Vec::new();
30+
31+
let enddefs : Vec<_> = chars.match_indices("ENDDEF").collect();
32+
let mut start = 0;
33+
for (idx,_) in enddefs
34+
{
35+
let matching_def = chars[start..idx].match_indices("DEF").collect::<Vec<_>>()[0].0;
36+
d_out.extend_from_slice(&chars[matching_def..idx+6].as_bytes());
37+
start = idx+7;
38+
}
39+
40+
if let Some(f) = output_files.get_mut(LIBRARY) {
41+
f.append(&mut d_out);
42+
}
43+
else {
44+
// Create entry in output_files for library
45+
let mut file_data = Vec::new();
46+
47+
// Load in from possibly existing file
48+
let fn_lib = PathBuf::from(output_path).join(LIBRARY);
49+
50+
if fn_lib.exists()
51+
{
52+
let mut f_lib = std::fs::File::open(fn_lib)?;
53+
54+
f_lib.read_to_end(&mut file_data)?;
55+
}
56+
else {
57+
file_data.extend_from_slice(b"EESchema-LIBRARY Version 2.3\n#encoding utf-8");
58+
}
59+
file_data.push(b'\n');
60+
file_data.append(&mut d_out);
61+
62+
output_files.insert(LIBRARY.to_string(), file_data);
63+
}
64+
},
65+
Some("dcm") =>
66+
{
67+
let chars = match std::str::from_utf8(&item[..])
68+
{
69+
Ok(v) => v,
70+
Err(e) => return Err(
71+
LLError::new(format!("Could not convert file to valid utf8: {} ({})", e, file_name), "ll-core/src/format/processors/kicad.rs", 68)
72+
)
73+
};
74+
75+
let mut d_out = Vec::new();
76+
77+
let endcmps : Vec<_> = chars.match_indices("$ENDCMP").collect();
78+
let mut start = 0;
79+
for (idx,_) in endcmps
80+
{
81+
let matching_cmp = chars[start..idx].match_indices("$CMP").collect::<Vec<_>>()[0].0;
82+
d_out.extend_from_slice(&chars[matching_cmp..idx+7].as_bytes());
83+
d_out.push(b'\n');
84+
start = idx+8;
85+
}
86+
87+
if let Some(f) = output_files.get_mut(DOCLIB) {
88+
f.append(&mut d_out);
89+
}
90+
else {
91+
// Create entry in output_files for library
92+
let mut file_data = Vec::new();
93+
94+
// Load in from possibly existing file
95+
let fn_lib = PathBuf::from(output_path).join(DOCLIB);
96+
97+
if fn_lib.exists()
98+
{
99+
let mut f_lib = std::fs::File::open(fn_lib)?;
100+
101+
f_lib.read_to_end(&mut file_data)?;
102+
}
103+
else {
104+
file_data.extend_from_slice(b"EESchema-DOCLIB Version 2.0\n");
105+
}
106+
107+
file_data.append(&mut d_out);
108+
109+
output_files.insert(DOCLIB.to_string(), file_data);
110+
}
111+
},
112+
Some("stl") | Some("stp") | Some("wrl") =>
113+
{
114+
// 3D Files
115+
let mut folder = PathBuf::from(file_name.clone());
116+
folder.set_extension("3dshapes");
117+
output_files.insert(format!("{}/{}/{}",U3D_FOLDER,folder.to_string_lossy(), file_name), item.clone());
118+
},
119+
Some("mod") =>
120+
{
121+
//Obsolete footprint module file
122+
}
123+
_ => println!("Unknown file type: {}", file_name)
124+
}
125+
}
126+
127+
Ok(())
128+
/*generic_processor(format, output_path, output_files, file_name, item)*/
129+
}
130+

ll-core/src/format/processors/mod.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// * Keep these in alphabetical order
2+
pub mod eagle;
3+
pub mod easyeda;
4+
pub mod kicad;
5+
pub mod d3;
6+
7+
use std::collections::HashMap;
8+
9+
pub(super) use zip::read::ZipFile;
10+
pub(super) use std::{
11+
io::Read,
12+
path::PathBuf
13+
};
14+
pub(super) use super::{
15+
Format,
16+
super::LLResult,
17+
super::LLError
18+
};
19+
20+
pub type Files = HashMap::<String, Vec<u8>>;
21+
22+
pub(super) fn generic_processor(format: &Format, output_path : String, output_files : &mut Files, file_path : String, item : &mut Vec<u8>) -> LLResult<()> {
23+
24+
output_files.insert(file_path, item.clone());
25+
26+
Ok(())
27+
}

0 commit comments

Comments
 (0)