1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

//! This module contains the trait [`TransformField`] as well as all types that implement it
//! And [`Field`] enum that can be used to refer to a [`Message`](`crate::sink::Message`)'s field

pub mod caps;
pub mod set;
pub mod shorten;
pub mod trim;

use self::{caps::Caps, set::Set, shorten::Shorten, trim::Trim};
use super::result::TransformResult;
use crate::{
	action::regex::{action::Extract, action::Replace, Regex},
	error::transform::Kind as TransformErrorKind,
};

use derive_more::From;

/// A helper trait for transforms that transform a single field of an entry
pub trait TransformField {
	/// Error return type. May be [`Infallible`](`std::convert::Infallible`)
	type Error: Into<TransformErrorKind>;

	/// Transform the `field` into a new field or `None` specifying what happens if `None` is returned
	#[allow(clippy::missing_errors_doc)]
	fn transform_field(&self, field: Option<&str>) -> Result<TransformResult<String>, Self::Error>;
}

/// Type that includes all available transforms that implement the [`TransformField`] trait
#[allow(missing_docs)]
#[derive(From, Debug)]
pub enum Kind {
	RegexExtract(Regex<Extract>),
	RegexReplace(Regex<Replace>),
	Set(Set),
	Caps(Caps),
	Trim(Trim),
	Shorten(Shorten),
}

/// List of all available fields for transformations
#[derive(Debug)]
pub enum Field {
	/// [`Message.title`] field
	Title,
	/// [`Message.body`] field
	Body,
	/// [`Message.link`] field
	Link,
}

impl TransformField for Kind {
	type Error = TransformErrorKind;

	fn transform_field(
		&self,
		field: Option<&str>,
	) -> Result<TransformResult<String>, TransformErrorKind> {
		macro_rules! delegate {
		    ($($k:tt),+) => {
				match self {
					$(Self::$k(x) => x.transform_field(field).map_err(Into::into),)+
				}
		    };
		}

		delegate!(RegexExtract, RegexReplace, Set, Caps, Trim, Shorten)
	}
}