#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum EndpointKind {
Port,
Pad,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Endpoint {
pub id: Option<String>,
pub kind: EndpointKind,
pub component_id: Option<String>,
pub name: Option<String>,
pub pin_number: Option<u32>,
pub hints: Vec<String>,
}
impl Endpoint {
pub fn port(id: Option<String>, name: Option<String>) -> Self {
Self::with_details(id, None, name, None, Vec::new())
}
pub fn with_details(
id: Option<String>,
component_id: Option<String>,
name: Option<String>,
pin_number: Option<u32>,
hints: Vec<String>,
) -> Self {
let comp =
component_id.or_else(|| id.as_deref().and_then(parse_comp_id).map(str::to_string));
Self {
id,
kind: EndpointKind::Port,
component_id: comp,
name,
pin_number,
hints,
}
}
pub fn pad(id: Option<String>) -> Self {
let component_id = id.as_deref().and_then(parse_comp_id).map(str::to_string);
Self {
id,
kind: EndpointKind::Pad,
component_id,
name: None,
pin_number: None,
hints: Vec::new(),
}
}
pub fn id(&self) -> Option<&str> {
self.id.as_deref()
}
pub fn component_id(&self) -> Option<&str> {
self.component_id.as_deref()
}
pub fn name(&self) -> Option<&str> {
self.name.as_deref()
}
pub fn pin_number(&self) -> Option<u32> {
self.pin_number
}
pub fn hints(&self) -> &[String] {
&self.hints
}
pub fn is_port(&self) -> bool {
self.kind == EndpointKind::Port
}
pub fn is_pad(&self) -> bool {
self.kind == EndpointKind::Pad
}
}
fn parse_comp_id(id: &str) -> Option<&str> {
id.split_once('.').map(|(comp, _)| comp)
}