feat: update program and cdylib exposed interface
This commit is contained in:
1 parent
18a55272a3
commit
2c811503a2
4 files changed
+174
-75
No files matched your search
@@ -27,7 +27,7 @@ mod schema;
|
||||
mod program;
|
||||
|
||||
pub use schema::{Schema, SchemaError};
|
||||
pub use program::{Program, ParseProgramError, ProgramError, ProgramExtStatus};
|
||||
pub use program::{Program, ParseProgramError, ProgramError, ProgramSelfExtStatus, ProgramExtStatus};
|
||||
pub use lowlevel::{Scope, View};
|
||||
|
||||
// endregion
|
||||
@@ -269,6 +269,9 @@ impl Program {
|
||||
|
||||
impl Program {
|
||||
pub fn resolve_name(&self) -> Result<Option<String>, ProgramError> {
|
||||
// TODO:
|
||||
// Add fallback: fetch it from executable manifest file.
|
||||
// Add fallback: use executable name directly.
|
||||
Ok(self
|
||||
.name
|
||||
.as_ref()
|
||||
@@ -277,6 +280,9 @@ impl Program {
|
||||
}
|
||||
|
||||
pub fn resolve_icon(&self) -> Result<Option<concept::IconRc>, ProgramError> {
|
||||
// TODO:
|
||||
// Add fallback: fetch it from the first icon of executable.
|
||||
// Add fallback: use system default executable icon.
|
||||
Ok(self
|
||||
.icon
|
||||
.as_ref()
|
||||
@@ -288,19 +294,35 @@ impl Program {
|
||||
self.ext_keys.len()
|
||||
}
|
||||
|
||||
pub fn get_ext(&self, index: usize) -> Result<&concept::Ext, ProgramError> {
|
||||
match self.ext_keys.get(index) {
|
||||
Some(program_key) => {
|
||||
let ext_key = &program_key.ext_key;
|
||||
Ok(ext_key.inner())
|
||||
}
|
||||
None => Err(ProgramError::BadIndex),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn find_ext(&self, body: &str) -> Option<usize> {
|
||||
self.ext_keys_map.get(body).copied()
|
||||
}
|
||||
|
||||
pub fn resolve_ext(&self, index: usize) -> Result<ProgramSelfExtStatus, ProgramError> {
|
||||
// Fetch data
|
||||
let progid_ext_key = self.ext_keys.get(index).ok_or(ProgramError::BadIndex)?;
|
||||
|
||||
// Try resolving name with string resource first,
|
||||
// and fallback to ProgId verbatim.
|
||||
let name = progid_ext_key
|
||||
.name
|
||||
.inner
|
||||
.extract()
|
||||
.unwrap_or(progid_ext_key.progid_key.inner().to_string());
|
||||
// Try to fetch icon
|
||||
let icon = progid_ext_key
|
||||
.icon
|
||||
.inner
|
||||
.extract(concept::IconSizeKind::Small)
|
||||
.ok();
|
||||
|
||||
// Okey, return it
|
||||
Ok(ProgramSelfExtStatus::new(
|
||||
progid_ext_key.ext_key.inner().clone(),
|
||||
name,
|
||||
icon,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
impl Program {
|
||||
@@ -589,6 +611,46 @@ struct ProgramProgIdExtKey {
|
||||
|
||||
// region: Exposed Stuff
|
||||
|
||||
/// Exposed struct representing this program provided method for opening specific file extension.
|
||||
///
|
||||
/// The data including the extension name, diaplay name and icon.
|
||||
pub struct ProgramSelfExtStatus {
|
||||
ext: concept::Ext,
|
||||
name: String,
|
||||
icon: Option<concept::IconRc>,
|
||||
}
|
||||
|
||||
impl ProgramSelfExtStatus {
|
||||
fn new(ext: concept::Ext, name: String, icon: Option<concept::IconRc>) -> Self {
|
||||
Self { ext, name, icon }
|
||||
}
|
||||
|
||||
/// Get the extension name without leading dot like `jpg`.
|
||||
pub fn get_ext(&self) -> &str {
|
||||
self.ext.inner()
|
||||
}
|
||||
|
||||
/// Get the extension name with leading dot like `.jpg`.
|
||||
pub fn get_dotted_ext(&self) -> String {
|
||||
self.ext.dotted_inner()
|
||||
}
|
||||
|
||||
/// Get the display name of this program.
|
||||
///
|
||||
/// The program provided display name will be used firstly.
|
||||
/// If this program has no display name, the stringified ProgId will be used instead.
|
||||
pub fn get_name(&self) -> &str {
|
||||
self.name.as_str()
|
||||
}
|
||||
|
||||
/// Get the icon of this program.
|
||||
///
|
||||
/// Due to the icon is optional, if there is no icon, return None.
|
||||
pub fn get_icon(&self) -> Option<&concept::IconRc> {
|
||||
self.icon.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
/// Exposed struct representing the default associated program of specific file extension.
|
||||
///
|
||||
/// The data including the diaplay name and icon.
|
||||
|
||||
Reference in new issue
Block a user