(self, os: &Os, session: &mut ChatSession)
| 35 | |
| 36 | impl PersistSubcommand { |
| 37 | pub async fn execute(self, os: &Os, session: &mut ChatSession) -> Result<ChatState, ChatError> { |
| 38 | macro_rules! tri { |
| 39 | ($v:expr, $name:expr, $path:expr) => { |
| 40 | match $v { |
| 41 | Ok(v) => v, |
| 42 | Err(err) => { |
| 43 | execute!( |
| 44 | session.stderr, |
| 45 | StyledText::error_fg(), |
| 46 | style::Print(format!("\nFailed to {} {}: {}\n\n", $name, $path, &err)), |
| 47 | StyledText::reset_attributes() |
| 48 | )?; |
| 49 | |
| 50 | return Ok(ChatState::PromptUser { |
| 51 | skip_printing_tools: true, |
| 52 | }); |
| 53 | }, |
| 54 | } |
| 55 | }; |
| 56 | } |
| 57 | |
| 58 | match self { |
| 59 | Self::Save { path, force } => { |
| 60 | let contents = tri!(serde_json::to_string_pretty(&session.conversation), "export to", &path); |
| 61 | if os.fs.exists(&path) && !force { |
| 62 | execute!( |
| 63 | session.stderr, |
| 64 | StyledText::error_fg(), |
| 65 | style::Print(format!( |
| 66 | "\nFile at {} already exists. To overwrite, use -f or --force\n\n", |
| 67 | &path |
| 68 | )), |
| 69 | StyledText::reset_attributes() |
| 70 | )?; |
| 71 | return Ok(ChatState::PromptUser { |
| 72 | skip_printing_tools: true, |
| 73 | }); |
| 74 | } |
| 75 | tri!(os.fs.write(&path, contents).await, "export to", &path); |
| 76 | |
| 77 | execute!( |
| 78 | session.stderr, |
| 79 | StyledText::success_fg(), |
| 80 | style::Print(format!("\n✔ Exported conversation state to {}\n\n", &path)), |
| 81 | StyledText::reset_attributes() |
| 82 | )?; |
| 83 | }, |
| 84 | Self::Load { path } => { |
| 85 | // Try the original path first |
| 86 | let original_result = os.fs.read_to_string(&path).await; |
| 87 | |
| 88 | // If the original path fails and doesn't end with .json, try with .json appended |
| 89 | let contents = if original_result.is_err() && !path.ends_with(".json") { |
| 90 | let json_path = format!("{}.json", path); |
| 91 | match os.fs.read_to_string(&json_path).await { |
| 92 | Ok(content) => content, |
| 93 | Err(_) => { |
| 94 | // If both paths fail, return the original error for better user experience |
nothing calls this directly
no test coverage detected