feat(render): add echo extension

This commit is contained in:
Federico Terzi 2021-03-19 21:43:22 +01:00
parent b2422bac55
commit ea1479366e
2 changed files with 86 additions and 1 deletions

View File

@ -0,0 +1,84 @@
/*
* This file is part of espanso.
*
* Copyright (C) 2019-2021 Federico Terzi
*
* espanso is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* espanso is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
*/
use crate::{Extension, ExtensionOutput, ExtensionResult, Params, Value};
use thiserror::Error;
pub struct EchoExtension {}
#[allow(clippy::new_without_default)]
impl EchoExtension {
pub fn new() -> Self {
Self {}
}
}
impl Extension for EchoExtension {
fn name(&self) -> &str {
"echo"
}
fn calculate(
&self,
_: &crate::Context,
_: &crate::Scope,
params: &Params,
) -> crate::ExtensionResult {
if let Some(Value::String(echo)) = params.get("echo") {
ExtensionResult::Success(ExtensionOutput::Single(echo.clone()))
} else {
ExtensionResult::Error(EchoExtensionError::MissingEchoParameter.into())
}
}
}
#[derive(Error, Debug)]
pub enum EchoExtensionError {
#[error("missing 'echo' parameter")]
MissingEchoParameter,
}
#[cfg(test)]
mod tests {
use super::*;
use std::iter::FromIterator;
#[test]
fn echo_works_correctly() {
let extension = EchoExtension::new();
let param =
Params::from_iter(vec![("echo".to_string(), Value::String("test".to_string()))].into_iter());
assert_eq!(
extension
.calculate(&Default::default(), &Default::default(), &param)
.into_success()
.unwrap(),
ExtensionOutput::Single("test".to_string())
);
}
#[test]
fn missing_echo_parameter() {
let extension = EchoExtension::new();
let param = Params::new();
assert!(matches!(extension.calculate(&Default::default(), &Default::default(), &param), ExtensionResult::Error(_)));
}
}

View File

@ -17,4 +17,5 @@
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
*/
pub mod date;
pub mod date;
pub mod echo;