use std::fmt;
use crate::types::name::OptionName;
use crate::types::name::PackName;
use crate::types::tokens::error::TokenError;
use crate::types::tokens::kind::TokenKind;
use crate::types::tokens::reference::ItemRef;
use crate::types::tokens::var::Var;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum TokenTarget {
Skill(ItemRef),
Agent(ItemRef),
SkillDir(ItemRef),
Root(Option<PackName>),
Var(Var),
Option(OptionName),
}
impl TokenTarget {
pub fn new(kind: TokenKind, words: &[&str]) -> Result<Self, TokenError> {
let word = match words {
[] => {
return match kind {
TokenKind::Root => Ok(Self::Root(None)),
_ => Err(TokenError::NeedsTarget { kind }),
};
}
[word] => *word,
_ => return Err(TokenError::TakesOneTarget { kind }),
};
match kind {
TokenKind::Skill => Ok(Self::Skill(ItemRef::new(word)?)),
TokenKind::Agent => Ok(Self::Agent(ItemRef::new(word)?)),
TokenKind::SkillDir => Ok(Self::SkillDir(ItemRef::new(word)?)),
TokenKind::Root if word.contains('/') => Err(TokenError::RootTakesPackName {
text: word.to_owned(),
}),
TokenKind::Root => Ok(Self::Root(Some(PackName::new(word)?))),
TokenKind::Var => Ok(Self::Var(Var::new(word)?)),
TokenKind::Option if word.contains('/') => Err(TokenError::OptionNotThisPack),
TokenKind::Option => Ok(Self::Option(OptionName::new(word)?)),
}
}
#[must_use]
pub const fn kind(&self) -> TokenKind {
match self {
Self::Skill(_) => TokenKind::Skill,
Self::Agent(_) => TokenKind::Agent,
Self::SkillDir(_) => TokenKind::SkillDir,
Self::Root(_) => TokenKind::Root,
Self::Var(_) => TokenKind::Var,
Self::Option(_) => TokenKind::Option,
}
}
}
impl fmt::Display for TokenTarget {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Skill(item) | Self::Agent(item) | Self::SkillDir(item) => item.fmt(f),
Self::Root(Some(pack)) => pack.fmt(f),
Self::Root(None) => Ok(()),
Self::Var(var) => var.fmt(f),
Self::Option(option) => option.fmt(f),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn parse_shown(kind: TokenKind, words: &[&str]) -> Result<String, String> {
TokenTarget::new(kind, words)
.map(|t| t.to_string())
.map_err(|e| e.to_string())
}
#[test]
fn token_target_parses_per_kind() {
let cases: [(TokenKind, &[&str], &str); 7] = [
(TokenKind::Skill, &["sdlc/status"], "sdlc/status"),
(TokenKind::Agent, &["coder"], "coder"),
(TokenKind::SkillDir, &["discuss"], "discuss"),
(TokenKind::Root, &[], ""),
(TokenKind::Root, &["core"], "core"),
(TokenKind::Var, &["tmpdir"], "tmpdir"),
(TokenKind::Option, &["archive_keep"], "archive_keep"),
];
for (kind, words, shown) in cases {
assert_eq!(parse_shown(kind, words), Ok(shown.to_owned()), "{kind}");
assert_eq!(
TokenTarget::new(kind, words).map(|t| t.kind()),
Ok(kind),
"{kind}"
);
}
assert!(matches!(
TokenTarget::new(TokenKind::Root, &[]),
Ok(TokenTarget::Root(None))
));
assert!(matches!(
TokenTarget::new(TokenKind::Root, &["core"]),
Ok(TokenTarget::Root(Some(_)))
));
}
#[test]
fn token_target_refuses_missing_or_extra_targets() {
for kind in [
TokenKind::Skill,
TokenKind::Agent,
TokenKind::SkillDir,
TokenKind::Var,
TokenKind::Option,
] {
assert_eq!(
parse_shown(kind, &[]),
Err(format!("token \"{kind}\" needs a target"))
);
}
assert_eq!(
parse_shown(TokenKind::Root, &["core", "extra"]),
Err("token \"root\" takes one target".to_owned())
);
let extra: [(TokenKind, &[&str]); 5] = [
(TokenKind::Skill, &["a", "b"]),
(TokenKind::Agent, &["a", "b"]),
(TokenKind::SkillDir, &["a", "b"]),
(TokenKind::Var, &["tmpdir", "home"]),
(TokenKind::Option, &["a", "b"]),
];
for (kind, words) in extra {
assert_eq!(
parse_shown(kind, words),
Err(format!("token \"{kind}\" takes one target"))
);
}
}
#[test]
fn token_target_refuses_root_var_and_option_shapes() {
let known = "session_id, arguments, tmpdir, home, airsstack_home, project_dir";
let cases: [(TokenKind, &str, String); 5] = [
(
TokenKind::Root,
"core/x",
"token \"root\" takes a pack name, not \"core/x\"".to_owned(),
),
(
TokenKind::Root,
"Core",
"\"Core\" is not a valid name".to_owned(),
),
(
TokenKind::Var,
"core/tmpdir",
format!("unknown var \"core/tmpdir\" (known: {known})"),
),
(
TokenKind::Option,
"a-b",
"\"a-b\" is not a valid option name".to_owned(),
),
(
TokenKind::Option,
"core/archive_keep",
"option tokens name this pack's options only".to_owned(),
),
];
for (kind, word, message) in cases {
assert_eq!(parse_shown(kind, &[word]), Err(message), "{kind} {word}");
}
}
}