Skip to main content

ass_editor/core/document/
metadata.rs

1//! Document metadata accessors and format conversion entry points
2//!
3//! Covers the identity, file-path, modification, and cursor-tracking
4//! accessors along with the optional subtitle-format import/export bridges.
5
6use super::EditorDocument;
7use crate::core::position::Position;
8
9#[cfg(feature = "formats")]
10use crate::core::errors::Result;
11
12#[cfg(not(feature = "std"))]
13use alloc::string::String;
14
15impl EditorDocument {
16    /// Get document ID
17    #[must_use]
18    pub fn id(&self) -> &str {
19        &self.id
20    }
21
22    /// Get file path if document is associated with a file
23    #[must_use]
24    pub fn file_path(&self) -> Option<&str> {
25        self.file_path.as_deref()
26    }
27
28    /// Set file path for the document
29    pub fn set_file_path(&mut self, path: Option<String>) {
30        self.file_path = path;
31    }
32
33    /// Import content from another subtitle format
34    #[cfg(feature = "formats")]
35    pub fn import_format(
36        content: &str,
37        format: Option<crate::utils::formats::SubtitleFormat>,
38    ) -> Result<Self> {
39        let ass_content = crate::utils::formats::FormatConverter::import(content, format)?;
40        Self::from_content(&ass_content)
41    }
42
43    /// Export document to another subtitle format
44    #[cfg(feature = "formats")]
45    pub fn export_format(
46        &self,
47        format: crate::utils::formats::SubtitleFormat,
48        options: &crate::utils::formats::ConversionOptions,
49    ) -> Result<String> {
50        crate::utils::formats::FormatConverter::export(self, format, options)
51    }
52
53    /// Check if document has unsaved changes
54    #[must_use]
55    pub const fn is_modified(&self) -> bool {
56        self.modified
57    }
58
59    /// Get current cursor position (if tracked)
60    #[must_use]
61    pub fn cursor_position(&self) -> Option<Position> {
62        self.history.cursor_position()
63    }
64
65    /// Set current cursor position for tracking
66    pub fn set_cursor_position(&mut self, position: Option<Position>) {
67        self.history.set_cursor(position);
68    }
69
70    /// Mark document as modified
71    pub fn set_modified(&mut self, modified: bool) {
72        self.modified = modified;
73    }
74}