| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | |
| 10 | "reasonix/internal/topicstate" |
| 11 | ) |
| 12 | |
| 13 | func topicTitlesPath(workspaceRoot string) string { |
| 14 | if workspaceRoot == "" { |
| 15 | return filepath.Join(desktopConfigDir(), "global", topicTitlesFile) |
| 16 | } |
| 17 | return filepath.Join(workspaceRoot, ".reasonix", topicTitlesFile) |
| 18 | } |
| 19 | |
| 20 | func topicTitleSourcesPath(workspaceRoot string) string { |
| 21 | if workspaceRoot == "" { |
| 22 | return filepath.Join(desktopConfigDir(), "global", topicTitleSourcesFile) |
| 23 | } |
| 24 | return filepath.Join(workspaceRoot, ".reasonix", topicTitleSourcesFile) |
| 25 | } |
| 26 | |
| 27 | func topicCreatedAtsPath(workspaceRoot string) string { |
| 28 | if workspaceRoot == "" { |
| 29 | return filepath.Join(desktopConfigDir(), "global", topicCreatedAtsFile) |
| 30 | } |
| 31 | return filepath.Join(workspaceRoot, ".reasonix", topicCreatedAtsFile) |
| 32 | } |
| 33 | |
| 34 | func topicAutoTitleMetaPath(workspaceRoot string) string { |
| 35 | if workspaceRoot == "" { |
| 36 | return filepath.Join(desktopConfigDir(), "global", topicAutoTitlesFile) |
| 37 | } |
| 38 | return filepath.Join(workspaceRoot, ".reasonix", topicAutoTitlesFile) |
| 39 | } |
| 40 | |
| 41 | func legacyTopicPaths(workspaceRoot string) [4]string { |
| 42 | return [4]string{ |
| 43 | topicTitlesPath(workspaceRoot), topicTitleSourcesPath(workspaceRoot), |
| 44 | topicCreatedAtsPath(workspaceRoot), topicAutoTitleMetaPath(workspaceRoot), |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | func legacyTopicFilesExist(workspaceRoot string) bool { |
| 49 | for _, path := range legacyTopicPaths(workspaceRoot) { |
| 50 | if _, err := os.Stat(path); err == nil { |
| 51 | return true |
| 52 | } |
| 53 | } |
| 54 | return false |
| 55 | } |
| 56 | |
| 57 | func topicScopeKind(workspaceRoot string) string { |
| 58 | if strings.TrimSpace(workspaceRoot) == "" { |
| 59 | return "global" |
| 60 | } |
| 61 | return "project" |
| 62 | } |
| 63 | |
| 64 | func topicStateErrorType(err error) string { |
| 65 | if err == nil { |
| 66 | return "none" |
| 67 | } |
| 68 | var future *topicstate.FutureSchemaError |
| 69 | if errors.As(err, &future) { |
| 70 | return "future_schema" |
| 71 | } |
| 72 | if topicstate.IsCorruptionError(err) { |
| 73 | return "corruption" |
| 74 | } |
| 75 | if errors.Is(err, context.DeadlineExceeded) { |
| 76 | return "timeout" |
| 77 | } |
| 78 | if errors.Is(err, os.ErrPermission) { |
| 79 | return "permission" |
| 80 | } |
| 81 | return "io" |
| 82 | } |
| 83 |