use std::fmt;
use shared::error::Result;
use super::{TypedAttribute, string_attribute};
use crate::description::session::SEMANTIC_TOKEN_BUNDLE;
use crate::grammar::{self, syntax};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Group {
pub semantics: String,
pub mids: Vec<String>,
}
impl Group {
pub fn is_bundle(&self) -> bool {
self.semantics == SEMANTIC_TOKEN_BUNDLE
}
pub fn contains(&self, mid: &str) -> bool {
self.mids.iter().any(|m| m == mid)
}
}
impl TypedAttribute for Group {
const NAME: &'static str = "group";
fn parse(value: &str) -> Result<Self> {
let mut fields = grammar::fields("a=group", value)?.into_iter();
let semantics = fields.next().unwrap_or_default();
let mids: Vec<String> = fields.map(str::to_owned).collect();
if !grammar::is_token(semantics) || !mids.iter().all(|m| grammar::is_token(m)) {
return Err(syntax("a=group", value));
}
Ok(Group {
semantics: semantics.to_owned(),
mids,
})
}
}
impl fmt::Display for Group {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.semantics)?;
for mid in &self.mids {
write!(f, " {mid}")?;
}
Ok(())
}
}
string_attribute!(
Mid,
"mid",
"a=mid",
|v| grammar::is_token(v)
);
fn is_msid_token(value: &str) -> bool {
value.len() <= 64 && grammar::is_token(value)
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Msid {
pub id: String,
pub appdata: Option<String>,
}
impl TypedAttribute for Msid {
const NAME: &'static str = "msid";
fn parse(value: &str) -> Result<Self> {
let (id, appdata) = match value.split_once(' ') {
Some((id, appdata)) => (id, Some(appdata)),
None => (value, None),
};
if !is_msid_token(id) || !appdata.is_none_or(is_msid_token) {
return Err(syntax("a=msid", value));
}
Ok(Msid {
id: id.to_owned(),
appdata: appdata.map(str::to_owned),
})
}
}
impl fmt::Display for Msid {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.id)?;
if let Some(appdata) = &self.appdata {
write!(f, " {appdata}")?;
}
Ok(())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MsidSemantic {
pub semantic: String,
pub ids: Vec<String>,
}
impl TypedAttribute for MsidSemantic {
const NAME: &'static str = "msid-semantic";
fn parse(value: &str) -> Result<Self> {
let mut fields =
grammar::fields("a=msid-semantic", value.strip_prefix(' ').unwrap_or(value))?
.into_iter();
let semantic = fields.next().unwrap_or_default();
let ids: Vec<String> = fields.map(str::to_owned).collect();
if !grammar::is_token(semantic) || !ids.iter().all(|id| is_msid_token(id)) {
return Err(syntax("a=msid-semantic", value));
}
Ok(MsidSemantic {
semantic: semantic.to_owned(),
ids,
})
}
}
impl fmt::Display for MsidSemantic {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.semantic)?;
for id in &self.ids {
write!(f, " {id}")?;
}
Ok(())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DependencyType {
Lay,
Mdc,
Other(String),
}
impl fmt::Display for DependencyType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Lay => f.write_str("lay"),
Self::Mdc => f.write_str("mdc"),
Self::Other(token) => f.write_str(token),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Dependency {
pub fmt: String,
pub typ: DependencyType,
pub on: Option<(String, Vec<String>)>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Depend {
pub dependencies: Vec<Dependency>,
}
impl Depend {
pub fn of(&self, fmt: &str) -> Option<&Dependency> {
self.dependencies.iter().find(|d| d.fmt == fmt)
}
}
impl TypedAttribute for Depend {
const NAME: &'static str = "depend";
fn parse(value: &str) -> Result<Self> {
let mut dependencies: Vec<Dependency> = vec![];
for part in value.split("; ") {
let fields = grammar::fields("a=depend", part)?;
let (fmt, typ, on) = match fields.as_slice() {
[fmt, typ] => (*fmt, *typ, None),
[fmt, typ, on] => (*fmt, *typ, Some(*on)),
_ => return Err(syntax("a=depend", value)),
};
if !grammar::is_token(fmt) || !grammar::is_token(typ) {
return Err(syntax("a=depend", value));
}
let on = match on {
None => None,
Some(on) => {
let (mid, fmts) = on
.split_once(':')
.ok_or_else(|| syntax("a=depend", value))?;
let fmts: Vec<String> = fmts.split(',').map(str::to_owned).collect();
if !grammar::is_token(mid) || !fmts.iter().all(|f| grammar::is_token(f)) {
return Err(syntax("a=depend", value));
}
Some((mid.to_owned(), fmts))
}
};
if dependencies.iter().any(|d| d.fmt == fmt) {
return Err(syntax("a=depend", value));
}
dependencies.push(Dependency {
fmt: fmt.to_owned(),
typ: match typ {
"lay" => DependencyType::Lay,
"mdc" => DependencyType::Mdc,
other => DependencyType::Other(other.to_owned()),
},
on,
});
}
Ok(Depend { dependencies })
}
}
impl fmt::Display for Depend {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for (i, d) in self.dependencies.iter().enumerate() {
if i > 0 {
f.write_str("; ")?;
}
write!(f, "{} {}", d.fmt, d.typ)?;
if let Some((mid, fmts)) = &d.on {
write!(f, " {mid}:{}", fmts.join(","))?;
}
}
Ok(())
}
}