use serde::{Deserialize, Serialize};
use vantage_core::{Result, error};
use crate::vista::Vista;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[non_exhaustive]
pub struct JoinKey {
pub from: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub to: Option<String>,
}
impl JoinKey {
pub fn new(from: impl Into<String>, to: impl Into<String>) -> Self {
Self {
from: from.into(),
to: Some(to.into()),
}
}
pub fn to_target_id(from: impl Into<String>) -> Self {
Self {
from: from.into(),
to: None,
}
}
pub fn target_column<'a>(&'a self, target: &'a Vista) -> Result<&'a str> {
match &self.to {
Some(to) => Ok(to),
None => target.id_column().ok_or_else(|| {
error!(
"join target has no id column to constrain",
target = target.name(),
from = self.from.as_str()
)
}),
}
}
}