fn validate_refs()

in crates/iceberg/src/spec/table_metadata.rs [513:547]


    fn validate_refs(&self) -> Result<()> {
        for (name, snapshot_ref) in self.refs.iter() {
            if self.snapshot_by_id(snapshot_ref.snapshot_id).is_none() {
                return Err(Error::new(
                    ErrorKind::DataInvalid,
                    format!(
                        "Snapshot for reference {name} does not exist in the existing snapshots list"
                    ),
                ));
            }
        }

        let main_ref = self.refs.get(MAIN_BRANCH);
        if self.current_snapshot_id.is_some() {
            if let Some(main_ref) = main_ref {
                if main_ref.snapshot_id != self.current_snapshot_id.unwrap_or_default() {
                    return Err(Error::new(
                        ErrorKind::DataInvalid,
                        format!(
                            "Current snapshot id does not match main branch ({:?} != {:?})",
                            self.current_snapshot_id.unwrap_or_default(),
                            main_ref.snapshot_id
                        ),
                    ));
                }
            }
        } else if main_ref.is_some() {
            return Err(Error::new(
                ErrorKind::DataInvalid,
                "Current snapshot is not set, but main branch exists",
            ));
        }

        Ok(())
    }