use std::collections::BTreeSet;
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum SubjectKind {
Type,
Trait,
}
impl std::str::FromStr for SubjectKind {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"type" => Ok(Self::Type),
"trait" => Ok(Self::Trait),
_ => Err(format!("unknown subject kind: {s}")),
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct DecompositionContext {
subject_name: String,
subject_kind: SubjectKind,
}
impl DecompositionContext {
#[must_use]
pub fn new(subject_name: impl Into<String>, subject_kind: SubjectKind) -> Self {
Self {
subject_name: subject_name.into(),
subject_kind,
}
}
#[must_use]
pub fn subject_name(&self) -> &str {
&self.subject_name
}
#[must_use]
pub fn subject_kind(&self) -> SubjectKind {
self.subject_kind
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct MethodProfile {
name: String,
accessed_fields: BTreeSet<String>,
signature_types: BTreeSet<String>,
local_types: BTreeSet<String>,
external_domains: BTreeSet<String>,
}
impl MethodProfile {
#[must_use]
pub fn name(&self) -> &str {
&self.name
}
#[must_use]
pub fn accessed_fields(&self) -> &BTreeSet<String> {
&self.accessed_fields
}
#[must_use]
pub fn signature_types(&self) -> &BTreeSet<String> {
&self.signature_types
}
#[must_use]
pub fn local_types(&self) -> &BTreeSet<String> {
&self.local_types
}
#[must_use]
pub fn external_domains(&self) -> &BTreeSet<String> {
&self.external_domains
}
}
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct MethodProfileBuilder {
name: String,
accessed_fields: BTreeSet<String>,
signature_types: BTreeSet<String>,
local_types: BTreeSet<String>,
external_domains: BTreeSet<String>,
}
impl MethodProfileBuilder {
#[must_use]
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
..Self::default()
}
}
pub fn record_accessed_field(&mut self, field_name: impl Into<String>) -> &mut Self {
self.accessed_fields.insert(field_name.into());
self
}
pub fn record_signature_type(&mut self, type_name: impl Into<String>) -> &mut Self {
self.signature_types.insert(type_name.into());
self
}
pub fn record_local_type(&mut self, type_name: impl Into<String>) -> &mut Self {
self.local_types.insert(type_name.into());
self
}
pub fn record_external_domain(&mut self, domain: impl Into<String>) -> &mut Self {
self.external_domains.insert(domain.into());
self
}
#[must_use]
pub fn build(self) -> MethodProfile {
MethodProfile {
name: self.name,
accessed_fields: self.accessed_fields,
signature_types: self.signature_types,
local_types: self.local_types,
external_domains: self.external_domains,
}
}
}