-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added parsing logic for plugin manifests
- Loading branch information
1 parent
c1251d5
commit 1e1f402
Showing
5 changed files
with
513 additions
and
92 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
mod download_manifest; | ||
mod manager; | ||
mod parser; | ||
mod types; | ||
|
||
use crate::plugin::manager::*; | ||
|
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,77 @@ | ||
mod download_manifest; | ||
mod plugin_manifest; | ||
|
||
use kdl::KdlNode; | ||
|
||
// Helper trait to make it easier to parse KdlNodes into our own types | ||
trait ParseKdlNode | ||
where | ||
Self: Sized, | ||
{ | ||
/// Return the name of the attribute used to identify the node pertaining to this struct | ||
fn kdl_key() -> &'static str; | ||
|
||
/// Attempt to convert a `kdl::KdlNode` into Self | ||
fn parse_node(node: &KdlNode) -> Option<Self>; | ||
} | ||
|
||
/// Returns the first successful node that can be parsed into T, if there is one | ||
fn extract_data<T>(nodes: &[KdlNode]) -> Option<T> | ||
where | ||
T: ParseKdlNode, | ||
{ | ||
for node in nodes { | ||
if let Some(val) = T::parse_node(node) { | ||
return Some(val); | ||
} | ||
} | ||
None | ||
} | ||
|
||
/// Use this macro to generate the code needed to parse a KDL node that is a single string, as the | ||
/// code is quite repetitive for this simple task. | ||
/// | ||
/// As a bonus, the following code is also generated: | ||
/// - AsRef<String> | ||
/// - new(value: String) -> Self | ||
/// | ||
/// NOTE: This only works with newtype wrappers around String! | ||
/// | ||
/// Example: | ||
/// publisher "mitre" can be generated by this macro! | ||
/// | ||
/// ```rust | ||
/// struct Publisher(pub String) | ||
/// ``` | ||
#[macro_export] | ||
macro_rules! string_newtype_parse_kdl_node { | ||
($type:ty, $identifier:expr) => { | ||
impl $type { | ||
pub fn new(value: String) -> Self { | ||
Self(value) | ||
} | ||
} | ||
|
||
impl ParseKdlNode for $type { | ||
fn kdl_key() -> &'static str { | ||
$identifier | ||
} | ||
|
||
fn parse_node(node: &KdlNode) -> Option<Self> { | ||
if node.name().to_string().as_str() != Self::kdl_key() { | ||
return None; | ||
} | ||
// NOTE: this macro currently assumes that the first positional argument is the | ||
// correct value to be parsing, which is true for newtype String wrappers! | ||
let entry = node.entries().first()?.value().as_string()?.to_string(); | ||
Some(Self(entry)) | ||
} | ||
} | ||
|
||
impl AsRef<String> for $type { | ||
fn as_ref(&self) -> &String { | ||
&self.0 | ||
} | ||
} | ||
}; | ||
} |
Oops, something went wrong.