Skip to main content

WorksheetFile

Struct WorksheetFile 

Source
pub struct WorksheetFile;

Implementations§

Source§

impl WorksheetFile

Source

pub fn read<P: AsRef<Path>>(path: P) -> Result<WorksheetDocument, Error>

Examples found in repository?
examples/worksheet_roundtrip.rs (line 15)
10fn main() -> Result<(), String> {
11    let mut args = env::args().skip(1);
12    let in_path = args.next().map(PathBuf::from).ok_or_else(usage)?;
13    let out_path = args.next().map(PathBuf::from).ok_or_else(usage)?;
14
15    let mut doc = WorksheetFile::read(&in_path).map_err(|e| e.to_string())?;
16    doc.set_version(20260101)
17        .set_generator("kiutils")
18        .set_generator_version("roundtrip-demo")
19        .set_setup_line_width(0.2);
20    doc.write(&out_path).map_err(|e| e.to_string())?;
21
22    let reread = WorksheetFile::read(&out_path).map_err(|e| e.to_string())?;
23    println!("input: {}", in_path.display());
24    println!("output: {}", out_path.display());
25    println!("line_count: {}", reread.ast().line_count);
26    println!("rect_count: {}", reread.ast().rect_count);
27    println!("tbtext_count: {}", reread.ast().tbtext_count);
28    println!("unknown_nodes: {}", reread.ast().unknown_nodes.len());
29    println!("diagnostics: {}", reread.diagnostics().len());
30
31    Ok(())
32}
More examples
Hide additional examples
examples/read_all.rs (line 40)
8fn main() -> Result<(), Box<dyn std::error::Error>> {
9    let base = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
10        .join("examples")
11        .join("data");
12
13    let pcb = PcbFile::read(base.join("sample.kicad_pcb"))?;
14    println!("pcb version: {:?}", pcb.ast().version);
15    println!("pcb unknown nodes: {}", pcb.ast().unknown_nodes.len());
16
17    let footprint = FootprintFile::read(base.join("sample.kicad_mod"))?;
18    println!("footprint version: {:?}", footprint.ast().version);
19
20    let schematic = SchematicFile::read(base.join("sample.kicad_sch"))?;
21    println!("schematic version: {:?}", schematic.ast().version);
22    println!(
23        "schematic unknown nodes: {}",
24        schematic.ast().unknown_nodes.len()
25    );
26
27    let table = FpLibTableFile::read(base.join("fp-lib-table"))?;
28    println!("fp libs: {}", table.ast().library_count);
29
30    let sym_table = SymLibTableFile::read(base.join("sym-lib-table"))?;
31    println!("sym libs: {}", sym_table.ast().library_count);
32
33    let dru = DesignRulesFile::read(base.join("sample.kicad_dru"))?;
34    println!("rules: {}", dru.ast().rule_count);
35    println!("rule constraints: {}", dru.ast().total_constraint_count);
36
37    let project = ProjectFile::read(base.join("sample.kicad_pro"))?;
38    println!("project meta version: {:?}", project.ast().meta_version);
39
40    let worksheet = WorksheetFile::read(base.join("sample.kicad_wks"))?;
41    println!("worksheet version: {:?}", worksheet.ast().version);
42    println!("worksheet tbtext count: {}", worksheet.ast().tbtext_count);
43
44    pcb.write_mode("/tmp/out.kicad_pcb", WriteMode::Lossless)?;
45    Ok(())
46}
examples/worksheet_corpus_roundtrip.rs (line 53)
24fn main() -> Result<(), String> {
25    let mut args = std::env::args().skip(1);
26    let input_dir = args.next().map(PathBuf::from).ok_or_else(usage)?;
27    let output_dir = args.next().map(PathBuf::from).ok_or_else(usage)?;
28
29    let mut files = Vec::new();
30    collect_worksheet_files(&input_dir, &mut files)?;
31    files.sort();
32
33    if files.is_empty() {
34        return Err(format!(
35            "no .kicad_wks files found under {}",
36            input_dir.display()
37        ));
38    }
39
40    let mut ok = 0usize;
41    let mut failed = 0usize;
42    for path in files {
43        let rel = path
44            .strip_prefix(&input_dir)
45            .map_err(|e| format!("strip_prefix {}: {e}", path.display()))?;
46        let out_path = output_dir.join(rel);
47        if let Some(parent) = out_path.parent() {
48            fs::create_dir_all(parent)
49                .map_err(|e| format!("create_dir_all {}: {e}", parent.display()))?;
50        }
51
52        let result = (|| -> Result<(), String> {
53            let doc = WorksheetFile::read(&path).map_err(|e| format!("read: {e}"))?;
54            doc.write(&out_path).map_err(|e| format!("write: {e}"))?;
55            let _ = WorksheetFile::read(&out_path).map_err(|e| format!("reread: {e}"))?;
56            Ok(())
57        })();
58
59        match result {
60            Ok(()) => {
61                ok += 1;
62                println!("ok: {}", path.display());
63            }
64            Err(err) => {
65                failed += 1;
66                eprintln!("fail: {} -> {}", path.display(), err);
67            }
68        }
69    }
70
71    println!("summary: ok={ok} failed={failed}");
72    if failed > 0 {
73        return Err(format!("{failed} files failed"));
74    }
75    Ok(())
76}

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.