2021-03-19 20:43:22 +00:00
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
2021-06-05 13:31:55 +00:00
|
|
|
pub struct EchoExtension {
|
|
|
|
alias: String
|
|
|
|
}
|
2021-03-19 20:43:22 +00:00
|
|
|
|
|
|
|
#[allow(clippy::new_without_default)]
|
|
|
|
impl EchoExtension {
|
|
|
|
pub fn new() -> Self {
|
2021-06-05 13:31:55 +00:00
|
|
|
Self {
|
|
|
|
alias: "echo".to_string(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_with_alias(alias: &str) -> Self {
|
|
|
|
Self {
|
|
|
|
alias: alias.to_string(),
|
|
|
|
}
|
2021-03-19 20:43:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Extension for EchoExtension {
|
|
|
|
fn name(&self) -> &str {
|
2021-06-05 13:31:55 +00:00
|
|
|
self.alias.as_str()
|
2021-03-19 20:43:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn echo_works_correctly() {
|
|
|
|
let extension = EchoExtension::new();
|
|
|
|
|
|
|
|
let param =
|
2021-10-05 21:18:39 +00:00
|
|
|
vec![("echo".to_string(), Value::String("test".to_string()))].into_iter().collect::<Params>();
|
2021-03-19 20:43:22 +00:00
|
|
|
assert_eq!(
|
|
|
|
extension
|
|
|
|
.calculate(&Default::default(), &Default::default(), ¶m)
|
|
|
|
.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(), ¶m), ExtensionResult::Error(_)));
|
|
|
|
}
|
2021-06-05 13:31:55 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn alias() {
|
|
|
|
let extension_with_alias = EchoExtension::new_with_alias("dummy");
|
|
|
|
let extension = EchoExtension::new();
|
|
|
|
|
|
|
|
assert_eq!(extension.name(), "echo");
|
|
|
|
assert_eq!(extension_with_alias.name(), "dummy");
|
|
|
|
}
|
2021-03-19 20:43:22 +00:00
|
|
|
}
|