Create a new view. # Arguments `name` - The name of the view to create # Errors Returns an error if: - The view already exists - The database operation fails
(&mut self, name: &str)
| 83 | /// - The view already exists |
| 84 | /// - The database operation fails |
| 85 | pub fn create_view(&mut self, name: &str) -> Result<(), RepositoryError> { |
| 86 | // Create the workspace directory for this view. |
| 87 | ensure_workspace_dir(&self.dot_dir, name)?; |
| 88 | |
| 89 | // Create a **Draft** view parented on the nearest Shared |
| 90 | // ancestor of the current view. The change log starts EMPTY — |
| 91 | // no changes are inherited automatically. |
| 92 | // |
| 93 | // The parent link gives the view read-access to the shared |
| 94 | // graph content (via the overlay chain) so that `record` can |
| 95 | // compute diffs against the existing state. But no files are |
| 96 | // *materialised* on disk until changes are explicitly inserted |
| 97 | // into this view (which copies them into the view's change log). |
| 98 | // |
| 99 | // This means: |
| 100 | // `view new feature` → empty workspace, no files |
| 101 | // `insert from-view dev feature` → inherits dev's files |
| 102 | // |
| 103 | // Using the nearest Shared ancestor (instead of the current |
| 104 | // view directly) prevents sibling Draft views from seeing |
| 105 | // each other's edges through the overlay chain. |
| 106 | let parent_name = self.nearest_shared_ancestor(&self.current_view.clone())?; |
| 107 | |
| 108 | let mut txn = self |
| 109 | .pristine |
| 110 | .write_txn() |
| 111 | .map_err(|e| RepositoryError::Database(e.to_string()))?; |
| 112 | |
| 113 | if txn |
| 114 | .get_view(name) |
| 115 | .map_err(|e| RepositoryError::Database(e.to_string()))? |
| 116 | .is_some() |
| 117 | { |
| 118 | return Err(RepositoryError::ViewAlreadyExists { |
| 119 | name: name.to_string(), |
| 120 | }); |
| 121 | } |
| 122 | |
| 123 | let parent_view = txn |
| 124 | .get_view(&parent_name) |
| 125 | .map_err(|e| RepositoryError::Database(e.to_string()))? |
| 126 | .ok_or_else(|| RepositoryError::ViewNotFound { |
| 127 | name: parent_name.clone(), |
| 128 | })?; |
| 129 | |
| 130 | txn.create_view(name, ViewScope::Draft, Some(parent_view.id)) |
| 131 | .map_err(|e| RepositoryError::Database(e.to_string()))?; |
| 132 | |
| 133 | txn.commit() |
| 134 | .map_err(|e| RepositoryError::Database(e.to_string()))?; |
| 135 | |
| 136 | Ok(()) |
| 137 | } |
| 138 | |
| 139 | /// Create a new Shared view with no parent. |
| 140 | /// |