1
0
Files
wfassoc/wfassoc-exec/src/manifest.rs

106 lines
2.8 KiB
Rust
Raw Normal View History

2025-10-17 14:19:26 +08:00
use serde::Deserialize;
use std::collections::HashMap;
2025-12-30 23:21:01 +08:00
use std::path::Path;
2025-10-17 14:19:26 +08:00
use thiserror::Error as TeError;
use toml;
2026-04-17 15:24:22 +08:00
use wfassoc;
2025-10-17 14:19:26 +08:00
2026-04-17 15:24:22 +08:00
// region: Manifest
2025-12-30 23:21:01 +08:00
/// Raw user input manifest.
///
/// This manifest is the raw input of user.
/// Some fields may not be checked due to user invalid input.
2025-10-17 14:19:26 +08:00
#[derive(Debug, Deserialize)]
2026-04-17 15:24:22 +08:00
pub struct Manifest {
2025-12-30 23:21:01 +08:00
identifier: String,
path: String,
clsid: String,
2026-04-17 15:24:22 +08:00
name: Option<String>,
icon: Option<String>,
behavior: Option<String>,
strs: HashMap<String, String>,
2025-12-30 23:21:01 +08:00
icons: HashMap<String, String>,
behaviors: HashMap<String, String>,
2026-04-17 15:24:22 +08:00
exts: HashMap<String, ManifestExt>,
2025-12-30 23:21:01 +08:00
}
/// The sub-type in raw user input manifest.
#[derive(Debug, Deserialize)]
2026-04-17 15:24:22 +08:00
pub struct ManifestExt {
2025-12-30 23:21:01 +08:00
name: String,
icon: String,
behavior: String,
}
// endregion
2026-04-17 15:24:22 +08:00
// region: Manifest Operation
2025-12-30 23:21:01 +08:00
2026-04-17 15:24:22 +08:00
/// Error occurs when parsing manifest TOML file.
#[derive(Debug, TeError)]
pub enum ParseManifestError {
/// Io error
#[error("IO error when reading manifest file")]
Io(#[from] std::io::Error),
/// Toml deserialization error
#[error("given TOML manifest file has bad format")]
Toml(#[from] toml::de::Error),
2025-12-30 23:21:01 +08:00
}
impl Manifest {
2026-04-17 15:24:22 +08:00
/// Read user manifest.
pub fn from_file(path: &Path) -> Result<Manifest, ParseManifestError> {
let contents = std::fs::read_to_string(path)?;
let config: Manifest = toml::from_str(&contents)?;
Ok(config)
}
2025-12-30 23:21:01 +08:00
}
2026-04-17 15:24:22 +08:00
/// Error occurs when parsing manifest into schema.
#[derive(Debug, TeError)]
pub enum ParseSchemaError {
2026-04-17 15:43:39 +08:00
/// Error when operating with schema.
#[error("{0}")]
Schema(#[from] wfassoc::highlevel::SchemaError)
2025-12-30 23:21:01 +08:00
}
2026-04-17 15:24:22 +08:00
impl TryFrom<Manifest> for wfassoc::Schema {
type Error = ParseSchemaError;
2025-12-30 23:21:01 +08:00
2026-04-17 15:43:39 +08:00
fn try_from(value: Manifest) -> Result<Self, Self::Error> {
let mut schema = wfassoc::Schema::new();
schema.set_identifier(&value.identifier);
schema.set_path(&value.path);
schema.set_clsid(&value.clsid);
schema.set_name(value.name.as_ref().map(|x| x.as_str()));
schema.set_icon(value.icon.as_ref().map(|x| x.as_str()));
schema.set_behavior(value.behavior.as_ref().map(|x| x.as_str()));
for (key, value) in value.strs {
schema.add_str(&key, &value)?;
}
for (key, value) in value.icons {
schema.add_icon(&key, &value)?;
}
for (key, value) in value.behaviors {
schema.add_behavior(&key, &value)?;
}
for (key, value) in value.exts {
schema.add_ext(&key, &value.name, &value.icon, &value.behavior)?;
}
Ok(schema)
2026-04-17 15:24:22 +08:00
}
2025-12-30 23:21:01 +08:00
}
2026-04-21 16:30:25 +08:00
impl Manifest {
pub fn into_schema(self) -> Result<wfassoc::Schema, ParseSchemaError> {
wfassoc::Schema::try_from(self)
}
}
2025-12-30 23:21:01 +08:00
// endregion