ligen_core/generator/context/
arguments.rs

1//! Arguments definition module.
2
3use crate::prelude::*;
4use crate::generator::context::BuildType;
5
6use serde::{Deserialize, Serialize};
7use std::path::PathBuf;
8
9/// Arguments passed from `cargo-ligen`.
10#[derive(Debug, Default, Clone, Serialize, Deserialize)]
11pub struct Arguments {
12    /// The name of the crate
13    pub crate_name: String,
14    /// The build type.
15    pub build_type: BuildType,
16    /// The build target directory.
17    pub target_dir: PathBuf,
18    /// The Cargo.toml manifest path passed with `--target-dir`.
19    pub manifest_path: PathBuf,
20    /// The Cargo.toml workspace manifest passed with `--manifest-path`.
21    pub workspace_path: Option<PathBuf>,
22    /// Workspace member to build passed with `--package` or `-p`.
23    pub workspace_member_package_id: Option<String>,
24}
25
26impl Arguments {
27    /// Generates a JSON representation of Arguments in CARGO_LIGEN_ARGUMENTS.
28    pub fn to_env(&self) {
29        let json = serde_json::to_string(self).expect("Couldn't serialize.");
30        std::env::set_var("CARGO_LIGEN_ARGUMENTS", json);
31    }
32
33    /// Parses the JSON representation from CARGO_LIGEN_ARGUMENTS.
34    pub fn from_env() -> Result<Self> {
35        match std::env::var("CARGO_LIGEN_ARGUMENTS") {
36            Ok(json_string) => match serde_json::from_str(&json_string) {
37                Ok(arguments) => Ok(arguments),
38                Err(err) => Err(err.into()),
39            },
40            Err(_) => Err("Couldn't find CARGO_LIGEN_ARGUMENTS env var".into()),
41        }
42    }
43}