use std::hash::Hash;
use crate::models::{self, parsed::SymbolRole};
#[derive(Debug, Eq, PartialOrd, Ord)]
pub struct Symbol {
pub kind: models::parsed::SymbolKind,
pub name: String,
pub definition: Option<models::parsed::Occurrence>,
pub occurrences: Vec<models::parsed::Occurrence>,
}
impl Symbol {
#[must_use]
pub fn new(kind: models::parsed::SymbolKind, name: &str) -> Self {
Self {
kind,
name: name.to_string(),
occurrences: Vec::default(),
definition: None,
}
}
pub fn add_occurrence(&mut self, occurrence: models::parsed::Occurrence) {
if self.definition.is_none() && occurrence.roles.contains(&SymbolRole::Definition) {
let _ = self.definition.insert(occurrence);
return;
}
self.occurrences.push(occurrence);
}
}
impl Hash for Symbol {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.name.hash(state);
if let Some(definition) = &self.definition {
definition.hash(state);
} else {
self.occurrences.hash(state);
}
}
}
impl PartialEq for Symbol {
fn eq(&self, other: &Self) -> bool {
if let Some(self_definition) = &self.definition
&& let Some(other_definition) = &other.definition
{
self.name == other.name && self_definition == other_definition
} else {
self.name == other.name && self.occurrences == other.occurrences
}
}
}