use crate::document_core::DocumentCore;
use crate::error::HwpError;
use crate::model::raw_provenance::SectionSeal;
#[derive(Debug, Clone)]
pub struct SectionRawCapture {
pub section_idx: usize,
pub raw_stream: Option<Vec<u8>>,
pub raw_provenance: Option<SectionSeal>,
}
impl DocumentCore {
pub fn capture_section_raw_native(&mut self, section_idx: usize) -> Result<u32, HwpError> {
let section = self.document.sections.get(section_idx).ok_or_else(|| {
HwpError::RenderError(format!(
"구역 {} 범위 초과 (총 {}개)",
section_idx,
self.document.sections.len()
))
})?;
let capture = SectionRawCapture {
section_idx,
raw_stream: section.raw_stream.clone(),
raw_provenance: section.raw_provenance.clone(),
};
let id = self.next_section_raw_id;
self.next_section_raw_id += 1;
self.section_raw_store.push((id, capture));
Ok(id)
}
pub fn restore_section_raw_native(&mut self, capture_id: u32) -> Result<String, HwpError> {
let pos = self
.section_raw_store
.iter()
.position(|(id, _)| *id == capture_id);
let capture = match pos {
Some(pos) => self.section_raw_store[pos].1.clone(),
None => {
return Err(HwpError::RenderError(format!(
"구역 raw 캡처 {} 없음",
capture_id
)))
}
};
let section = self
.document
.sections
.get_mut(capture.section_idx)
.ok_or_else(|| {
HwpError::RenderError(format!(
"캡처 {} 의 구역 {} 이 현재 문서에 없습니다",
capture_id, capture.section_idx
))
})?;
if section.raw_stream.is_some() {
return Err(HwpError::RenderError(format!(
"구역 raw 캡처 {} 의 전제가 어긋났습니다 — passthrough 가 이미 살아있다",
capture_id
)));
}
section.raw_stream = capture.raw_stream;
section.raw_provenance = capture.raw_provenance;
self.section_raw_store
.remove(pos.expect("존재를 확인한 캡처 위치"));
Ok("{\"ok\":true}".to_string())
}
pub fn discard_section_raw_native(&mut self, capture_id: u32) {
self.section_raw_store.retain(|(id, _)| *id != capture_id);
}
}