-
Notifications
You must be signed in to change notification settings - Fork 471
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: multiple openers for a single rule (#154)
- Loading branch information
Showing
5 changed files
with
77 additions
and
15 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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
mod open; | ||
mod opener; | ||
mod rule; | ||
|
||
pub use open::*; | ||
pub use opener::*; | ||
use rule::*; |
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,58 @@ | ||
use std::fmt; | ||
|
||
use serde::{de::{self, Visitor}, Deserialize, Deserializer}; | ||
|
||
use crate::pattern::Pattern; | ||
|
||
#[derive(Debug, Deserialize)] | ||
pub(super) struct OpenRule { | ||
pub(super) name: Option<Pattern>, | ||
pub(super) mime: Option<Pattern>, | ||
#[serde(rename = "use")] | ||
#[serde(deserialize_with = "OpenRule::deserialize")] | ||
pub(super) use_: Vec<String>, | ||
} | ||
|
||
impl OpenRule { | ||
fn deserialize<'de, D>(deserializer: D) -> Result<Vec<String>, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
struct UseVisitor; | ||
|
||
impl<'de> Visitor<'de> for UseVisitor { | ||
type Value = Vec<String>; | ||
|
||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | ||
formatter.write_str("a string, or array of strings") | ||
} | ||
|
||
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error> | ||
where | ||
A: de::SeqAccess<'de>, | ||
{ | ||
let mut uses = Vec::new(); | ||
while let Some(use_) = seq.next_element::<String>()? { | ||
uses.push(use_); | ||
} | ||
Ok(uses) | ||
} | ||
|
||
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> | ||
where | ||
E: de::Error, | ||
{ | ||
Ok(vec![value.to_owned()]) | ||
} | ||
|
||
fn visit_string<E>(self, v: String) -> Result<Self::Value, E> | ||
where | ||
E: de::Error, | ||
{ | ||
Ok(vec![v]) | ||
} | ||
} | ||
|
||
deserializer.deserialize_any(UseVisitor) | ||
} | ||
} |
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