use std::sync::Arc;
#[derive(Debug, Clone, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub struct SortParam {
pub name: Arc<str>,
pub sort: Arc<str>,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub struct Sort {
pub name: Arc<str>,
pub params: Vec<SortParam>,
}
impl Sort {
#[must_use]
pub fn simple(name: impl Into<Arc<str>>) -> Self {
Self {
name: name.into(),
params: Vec::new(),
}
}
#[must_use]
pub fn dependent(name: impl Into<Arc<str>>, params: Vec<SortParam>) -> Self {
Self {
name: name.into(),
params,
}
}
#[must_use]
pub fn is_simple(&self) -> bool {
self.params.is_empty()
}
#[must_use]
pub fn arity(&self) -> usize {
self.params.len()
}
}
impl SortParam {
#[must_use]
pub fn new(name: impl Into<Arc<str>>, sort: impl Into<Arc<str>>) -> Self {
Self {
name: name.into(),
sort: sort.into(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn simple_sort() {
let s = Sort::simple("Vertex");
assert!(s.is_simple());
assert_eq!(s.arity(), 0);
assert_eq!(&*s.name, "Vertex");
}
#[test]
fn dependent_sort() {
let s = Sort::dependent(
"Hom",
vec![SortParam::new("a", "Ob"), SortParam::new("b", "Ob")],
);
assert!(!s.is_simple());
assert_eq!(s.arity(), 2);
}
}