Skip to content

Commit 17cedc0

Browse files
author
Evan Andersen
committed
Merge branch 'kicad_6_integration' of github.com:evantandersen/library-loader into kicad_6_integration
2 parents 965ebe4 + 0790345 commit 17cedc0

File tree

4 files changed

+44
-56
lines changed

4 files changed

+44
-56
lines changed

ll-core/src/config/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ impl Config {
5050

5151
pub(crate) fn formats(&self) -> Result<Vec<format::Format>> {
5252
let mut formats_vec = Vec::with_capacity(self.formats.len());
53-
for (_, f) in &self.formats {
53+
for (name, f) in &self.formats {
5454
formats_vec.push(format::Format::from_ecad(
55+
name,
5556
f.format.clone(),
5657
PathBuf::from(shellexpand::full(&f.output_path)?.as_ref()),
5758
))

ll-core/src/cse/mod.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,6 @@ impl CSE {
9696
}
9797

9898
fn unzip(&self, zip_filename: String, data: Vec<u8>) -> error::Result<Vec<Result>> {
99-
let reader = std::io::Cursor::new(&data);
100-
let mut archive = zip::ZipArchive::new(reader)?;
10199
let mut vec_results = Vec::with_capacity(self.formats.len());
102100

103101
for format in &*self.formats {
@@ -114,11 +112,10 @@ impl CSE {
114112
false => zip_filename.replace(".zip", ""),
115113
};
116114
let mut files = Files::new();
117-
for i in 0..archive.len() {
118-
let mut item = archive.by_index(i)?;
119-
let filename = item.name().to_string();
120-
format.extract(&mut files, filename, &mut item)?;
121-
}
115+
let reader = std::io::Cursor::new(&data);
116+
let mut archive = zip::ZipArchive::new(reader)?;
117+
format.extract(&mut archive)?;
118+
122119
let output_path = match format.create_folder {
123120
true => PathBuf::from(&format.output_path).join(lib_name),
124121
false => PathBuf::from(&format.output_path),

ll-core/src/format/extractors/kicad.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ use std::fs::File;
44
use std::io::{BufRead, BufReader, Seek, SeekFrom, Write};
55
use crate::Error;
66

7-
const FP_FOLDER : &str = "LibraryLoader.pretty";
8-
const SYM_LIB : &str = "LibraryLoader.kicad_sym";
97

108
pub fn extract(
119
format: &Format,
@@ -17,17 +15,19 @@ pub fn extract(
1715
let path = PathBuf::from(file_path);
1816
let base_name = path.file_name().unwrap().to_string_lossy().to_string();
1917

18+
let fp_folder_str = format!("{}.pretty", format.name);
19+
2020
//ensure we have the footprint library folder
21-
let footprint_folder = PathBuf::from(&format.output_path).join(FP_FOLDER);
21+
let footprint_folder = PathBuf::from(&format.output_path).join(fp_folder_str.clone());
2222
if !footprint_folder.exists() {
2323
fs::create_dir_all(footprint_folder)?;
2424
}
2525

2626
//ensure the symbol library exists
27-
let fn_lib = PathBuf::from(&format.output_path).join(SYM_LIB);
27+
let fn_lib = PathBuf::from(&format.output_path).join(format!("{}.kicad_sym", format.name));
2828

2929
if !fn_lib.exists() {
30-
fs::write(&fn_lib, "(kicad_symbol_lib (version 20211014) (generator Library-Loader)\n)\n").expect("Unable to create symbol library file");
30+
fs::write(&fn_lib, "(kicad_symbol_lib (version 20211014) (generator library-loader)\n)\n").expect("Unable to create symbol library file");
3131
}
3232

3333
if let Some(ext) = &path.extension() {
@@ -37,7 +37,7 @@ pub fn extract(
3737
{
3838
let mut f_data = Vec::<u8>::new();
3939
item.read_to_end(&mut f_data)?;
40-
files.insert(format!("{}/{}", FP_FOLDER, base_name), f_data);
40+
files.insert(format!("{}/{}", fp_folder_str.clone(), base_name), f_data);
4141
},
4242
Some("kicad_sym") =>
4343
{

ll-core/src/format/mod.rs

Lines changed: 32 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::io::Cursor;
12
use {
23
crate::error::{Error, Result},
34
serde::{Deserialize, Serialize},
@@ -60,70 +61,59 @@ ecad!([
6061
#[derive(Debug, Clone, PartialEq)]
6162
pub struct Format {
6263
pub output_path: PathBuf,
63-
// pub name: String,
64+
pub name: String,
6465
pub ecad: ECAD,
6566
pub create_folder: bool,
6667
match_path: &'static str,
6768
ignore: Vec<&'static str>,
6869
}
6970

7071
impl Format {
71-
pub fn from_ecad<P: Into<PathBuf>>(ecad: ECAD, output_path: P) -> Self {
72-
// * Keep these in alphabetical order
73-
match ecad {
74-
ECAD::D3 => Self {
75-
output_path: output_path.into(),
76-
ecad,
77-
create_folder: true,
78-
match_path: "3D",
79-
ignore: vec![],
72+
pub fn from_ecad<P: Into<PathBuf>>(name: &String, ecad: ECAD, output_path: P) -> Self {
73+
//defaults
74+
let mut fmt = Self {
75+
output_path: output_path.into(),
76+
name: (*name).clone(),
77+
ecad,
78+
create_folder: false,
79+
match_path: "",
80+
ignore: vec![],
81+
};
82+
// specific overrides, keep these in alphabetical order
83+
match fmt.ecad {
84+
ECAD::D3 => {
85+
fmt.create_folder = true;
86+
fmt.match_path = "3D";
8087
},
81-
ECAD::DesignSpark => Self {
82-
output_path: output_path.into(),
83-
ecad,
84-
create_folder: false,
85-
match_path: "DesignSpark PCB",
86-
ignore: vec![],
88+
ECAD::DesignSpark => {
89+
fmt.match_path = "DesignSpark PCB";
8790
},
88-
ECAD::Eagle => Self {
89-
output_path: output_path.into(),
90-
ecad,
91-
create_folder: false,
92-
match_path: "EAGLE",
93-
ignore: vec!["Readme.html"],
91+
ECAD::Eagle => {
92+
fmt.match_path = "EAGLE";
93+
fmt.ignore = vec!["Readme.html"];
9494
},
95-
ECAD::EasyEDA => Self {
96-
output_path: output_path.into(),
97-
ecad,
98-
create_folder: false,
99-
match_path: "EasyEDA",
100-
ignore: vec!["Readme.html"],
95+
ECAD::EasyEDA => {
96+
fmt.match_path = "EasyEDA";
97+
fmt.ignore = vec!["Readme.html"];
10198
},
102-
ECAD::KiCad => Self {
103-
output_path: output_path.into(),
104-
ecad,
105-
create_folder: false,
106-
match_path: "KiCad",
107-
ignore: vec![],
99+
ECAD::KiCad => {
100+
fmt.match_path = "KiCad";
108101
},
109-
ECAD::Zip => Self {
110-
output_path: output_path.into(),
111-
ecad,
112-
create_folder: false,
113-
match_path: "",
114-
ignore: vec![],
102+
ECAD::Zip => {
103+
//no changes
115104
},
116105
}
106+
return fmt
117107
}
118108

119-
pub fn extract(&self, files: &mut Files, file_path: String, item: &mut ZipFile) -> Result<()> {
109+
pub fn extract(&self, archive: &mut zip::ZipArchive<Cursor<&Vec<u8>>>) -> Result<()> {
120110
match &self.ecad {
121111
// * Keep these in alphabetical order
122112
ECAD::D3 => extractors::d3::extract(&self, files, file_path, item)?,
123113
ECAD::DesignSpark => extractors::designspark::extract(&self, files, file_path, item)?,
124114
ECAD::Eagle => extractors::eagle::extract(&self, files, file_path, item)?,
125115
ECAD::EasyEDA => extractors::easyeda::extract(&self, files, file_path, item)?,
126-
ECAD::KiCad => extractors::kicad::extract(&self, files, file_path, item)?,
116+
ECAD::KiCad => extractors::kicad::extract(&self, archive)?,
127117
ECAD::Zip => unreachable!("ZIP not handled!"),
128118
// ! NOTE: DO NOT ADD A _ => {} CATCHER HERE!
129119
};

0 commit comments

Comments
 (0)