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
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use crate::{action_result, Command, Context, Vector};

#[derive(Clone, Default)]
/// Hook is struct for action needs a context before sub.run.

pub struct Hook {
	/// name

	pub name: String,
	/// action

	pub action: Option<HookAction>,
	/// authors

	pub authors: String,
	/// copyright

	pub copyright: String,
	/// license

	pub license: Option<(String, String)>,
	/// description

	pub description: Option<String>,
	/// usage

	pub usage: String,
	/// alias

	pub alias: Vector<String>,
}

/// Type for action of hook.

pub type HookAction = fn(&mut Command, Context) -> action_result!();

impl Hook {
	/// Creates new hook.

	pub fn new() -> Hook {
		Hook::default()
	}

	/// Creates new hook with name

	pub fn with_name<T: Into<String>>(name: T) -> Hook {
		Hook {
			name: name.into(),
			action: None,
			authors: String::default(),
			copyright: String::default(),
			license: None,
			description: None,
			usage: String::default(),
			alias: Vector::default(),
		}
	}

	/// Creates new hook with all field

	pub fn with_all_field(
		name: String,
		action: Option<HookAction>,
		authors: String,
		copyright: String,
		license: Option<(String, String)>,
		description: Option<String>,
		usage: String,
		alias: Vector<String>,
	) -> Hook {
		Hook {
			name,
			action,
			authors,
			copyright,
			license,
			description,
			usage,
			alias,
		}
	}
}

macro_rules! gen_doc {
	($field:ident) => {
		concat!("Sets ", stringify!($field), ".")
	};
}

macro_rules! setter {
	($field:ident)=>{
		setter!($field, gen_doc!($field));
	};
	($field:ident, $fn_doc:expr) => {
		#[doc=$fn_doc]
		pub fn $field<T: Into<String>>(mut self, $field: T) -> Self {
			self.$field = $field.into();
			self
		}
	};
	($field:ident :Option String)=>{
		setter!($field,gen_doc!($field));
	};
	($field:ident :Option String, $fn_doc:expr)=>{
		#[doc=$fn_doc]
		pub fn $field<T:Into<String>>(mut self, $field: T)->Self{
			self.$field = Some($field.into());
			self
		}
	};
	($field:ident :Option $t:ty)=>{
		setter!($field :Option $t, gen_doc!($field));
	};
	($field:ident :Option $t:ty, $fn_doc:expr)=>{
		#[doc=$fn_doc]
		pub fn $field(mut self, $field: $t)->Self{
			self.$field = Some($field);
			self
		}
	};
	($($field:ident),+)=>{
		$(setter!($field);)+
	};
	($($field:ident :Option $t:ty),+)=>{
		$(setter!($field :Option $t);)+
	}
}

impl Hook {
	setter!(name, authors, copyright, usage);
	setter!(action:Option HookAction,license: Option (String, String),description:Option String);

	/// Add alias to this hook.

	pub fn alias<T: Into<String>>(mut self, alias: T) -> Self {
		self.alias.push(alias.into());
		self
	}

	/// Beturns true if name_or_alias matches command's name or one of alias at leaast.

	/// name_or_aliasがコマンド名がエイリアスのうち少なくとも一つにマッチした場合trueを返す

	pub fn is(&self, name_or_alias: &str) -> bool {
		if name_or_alias == self.name {
			true
		} else {
			match self.alias.inner() {
				None => false,
				Some(inner) => inner.iter().any(|a| a == name_or_alias),
			}
		}
	}
}