-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: load extension manifests in main
- Loading branch information
1 parent
7326306
commit afc4fb6
Showing
8 changed files
with
135 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
use crate::error::extension::ConvertExtensionIntoClapCommandError; | ||
use crate::extension::manifest::ExtensionManifest; | ||
use crate::extension::ExtensionName; | ||
use clap::Command; | ||
use std::collections::HashMap; | ||
|
||
pub type InstalledExtensionList = Vec<ExtensionName>; | ||
pub struct InstalledExtensionManifests(pub HashMap<ExtensionName, ExtensionManifest>); | ||
|
||
impl InstalledExtensionManifests { | ||
pub fn as_clap_commands(&self) -> Result<Vec<Command>, ConvertExtensionIntoClapCommandError> { | ||
let commands = self | ||
.0 | ||
.values() | ||
.map(|manifest| { | ||
manifest.into_clap_commands().map(|subcommands| { | ||
Command::new(&manifest.name) | ||
.allow_missing_positional(false) // don't accept unknown options | ||
.allow_external_subcommands(false) // don't accept unknown subcommands | ||
.about(&manifest.summary) | ||
.subcommands(subcommands) | ||
}) | ||
}) | ||
.collect::<Result<Vec<_>, _>>()?; | ||
Ok(commands) | ||
} | ||
|
||
pub fn contains(&self, extension: &str) -> bool { | ||
self.0.contains_key(extension) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,7 @@ | ||
pub mod catalog; | ||
pub mod installed; | ||
pub mod manager; | ||
pub mod manifest; | ||
pub mod url; | ||
|
||
use crate::error::extension::ConvertExtensionIntoClapCommandError; | ||
use crate::extension::{manager::ExtensionManager, manifest::ExtensionManifest}; | ||
use clap::Command; | ||
use std::{ | ||
fmt::{Display, Formatter}, | ||
fs::DirEntry, | ||
}; | ||
|
||
#[derive(Debug, Default)] | ||
pub struct Extension { | ||
pub name: String, | ||
} | ||
|
||
impl From<DirEntry> for Extension { | ||
fn from(entry: DirEntry) -> Self { | ||
let name = entry.file_name().to_string_lossy().to_string(); | ||
Extension { name } | ||
} | ||
} | ||
|
||
impl Display for Extension { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "{}", self.name) | ||
} | ||
} | ||
|
||
impl Extension { | ||
pub fn into_clap_command( | ||
self, | ||
manager: &ExtensionManager, | ||
) -> Result<Command, ConvertExtensionIntoClapCommandError> { | ||
let manifest = ExtensionManifest::load(&self.name, &manager.dir)?; | ||
let cmd = Command::new(&self.name) | ||
// don't accept unknown options | ||
.allow_missing_positional(false) | ||
// don't accept unknown subcommands | ||
.allow_external_subcommands(false) | ||
.about(&manifest.summary) | ||
.subcommands(manifest.into_clap_commands()?); | ||
Ok(cmd) | ||
} | ||
} | ||
type ExtensionName = String; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters