diff --git a/wfassoc-exec/src/manifest.rs b/wfassoc-exec/src/manifest.rs index c206b8a..28f0496 100644 --- a/wfassoc-exec/src/manifest.rs +++ b/wfassoc-exec/src/manifest.rs @@ -62,14 +62,37 @@ impl Manifest { /// Error occurs when parsing manifest into schema. #[derive(Debug, TeError)] pub enum ParseSchemaError { - + /// Error when operating with schema. + #[error("{0}")] + Schema(#[from] wfassoc::highlevel::SchemaError) } impl TryFrom for wfassoc::Schema { type Error = ParseSchemaError; - fn try_from(value: Manifest) -> std::result::Result { - todo!() + fn try_from(value: Manifest) -> Result { + 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) } } diff --git a/wfassoc/src/highlevel.rs b/wfassoc/src/highlevel.rs index dc0e7aa..5f6493d 100644 --- a/wfassoc/src/highlevel.rs +++ b/wfassoc/src/highlevel.rs @@ -8,7 +8,10 @@ pub use lowlevel::{Scope, View}; /// Error occurs when operating with `Schema`. #[derive(Debug, TeError)] -pub enum SchemaError {} +pub enum SchemaError { + #[error("duplicate key: {0}")] + DuplicateKey(String), +} /// Error occurs when trying converting `Schema` into `Program`. #[derive(Debug, TeError)] @@ -40,14 +43,6 @@ pub struct Schema { exts: HashMap, } -/// Internal used struct as the Schema file extensions hashmap value type. -#[derive(Debug)] -struct SchemaExt { - name: String, - icon: String, - behavior: String, -} - impl Schema { pub fn new() -> Self { Self { @@ -64,24 +59,49 @@ impl Schema { } } - pub fn set_identifier(&mut self, identifier: &str) -> Result<(), SchemaError> { - todo!() + pub fn set_identifier(&mut self, identifier: &str) -> () { + self.identifier = identifier.to_string(); } - pub fn set_path(&mut self, exe_path: &str) -> Result<(), SchemaError> { - todo!() + pub fn set_path(&mut self, exe_path: &str) -> () { + self.path = exe_path.to_string(); } - pub fn set_clsid(&mut self, clsid: &str) -> Result<(), SchemaError> { - todo!() + pub fn set_clsid(&mut self, clsid: &str) -> () { + self.clsid = clsid.to_string(); + } + + pub fn set_name(&mut self, name: Option<&str>) -> () { + self.name = name.map(|n| n.to_string()); + } + + pub fn set_icon(&mut self, icon: Option<&str>) -> () { + self.icon = icon.map(|i| i.to_string()); + } + + pub fn set_behavior(&mut self, behavior: Option<&str>) -> () { + self.behavior = behavior.map(|b| b.to_string()); + } + + pub fn add_str(&mut self, name: &str, value: &str) -> Result<(), SchemaError> { + match self.strs.insert(name.to_string(), value.to_string()) { + Some(_) => Err(SchemaError::DuplicateKey(name.to_string())), + None => Ok(()), + } } pub fn add_icon(&mut self, name: &str, value: &str) -> Result<(), SchemaError> { - todo!() + match self.icons.insert(name.to_string(), value.to_string()) { + Some(_) => Err(SchemaError::DuplicateKey(name.to_string())), + None => Ok(()), + } } pub fn add_behavior(&mut self, name: &str, value: &str) -> Result<(), SchemaError> { - todo!() + match self.behaviors.insert(name.to_string(), value.to_string()) { + Some(_) => Err(SchemaError::DuplicateKey(name.to_string())), + None => Ok(()), + } } pub fn add_ext( @@ -91,7 +111,13 @@ impl Schema { ext_icon: &str, ext_behavior: &str, ) -> Result<(), SchemaError> { - todo!() + match self.exts.insert( + ext.to_string(), + SchemaExt::new(ext_name, ext_icon, ext_behavior), + ) { + Some(_) => Err(SchemaError::DuplicateKey(ext.to_string())), + None => Ok(()), + } } pub fn into_program(self) -> Result { @@ -99,6 +125,24 @@ impl Schema { } } +/// Internal used struct as the Schema file extensions hashmap value type. +#[derive(Debug)] +struct SchemaExt { + name: String, + icon: String, + behavior: String, +} + +impl SchemaExt { + fn new(name: &str, icon: &str, behavior: &str) -> Self { + Self { + name: name.to_string(), + icon: icon.to_string(), + behavior: behavior.to_string(), + } + } +} + /// Program is a complete and immutable program representer pub struct Program {}