microcad_std/
lib.rs

1// Copyright © 2025 The µcad authors <info@ucad.xyz>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4//! µcad CLI install command.
5
6use rust_embed::RustEmbed;
7
8/// The µcad standard library asset.
9#[derive(RustEmbed)]
10#[folder = "lib"]
11pub struct Lib;
12
13pub fn get_user_stdlib_path() -> std::path::PathBuf {
14    let mut path = dirs::config_dir().expect("config directory");
15    path.push("microcad");
16    path.push("lib");
17    path
18}
19
20/// Extract the standard library into the standard library path.
21pub fn extract(overwrite: bool) -> std::io::Result<()> {
22    let dst = get_user_stdlib_path();
23    if dst.exists() {
24        if overwrite {
25            println!("Overwriting existing µcad standard library in {:?}", dst);
26        } else {
27            println!(
28                "Found µcad standard library already in {:?} (use -f to force overwrite)",
29                dst
30            );
31            return Ok(());
32        }
33    }
34
35    println!("Installing µcad standard library into {:?}...", dst);
36
37    std::fs::create_dir_all(&dst)?;
38
39    // Extract all embedded files.
40    Lib::iter().try_for_each(|file| {
41        let file_path = dst.join(file.as_ref());
42        if let Some(parent) = file_path.parent() {
43            std::fs::create_dir_all(parent)?;
44        }
45        std::fs::write(
46            file_path,
47            Lib::get(file.as_ref())
48                .expect("embedded folder 'lib' not found")
49                .data,
50        )
51    })?;
52
53    println!("Successfully installed µcad standard library.");
54
55    Ok(())
56}