summaryrefslogtreecommitdiffhomepage
path: root/packages/tui/internal/attachment/attachment.go
blob: 26bb91d49da6819f41103e62aa0da9861aac0ea6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package attachment

import (
	"github.com/google/uuid"
)

type TextSource struct {
	Value string `toml:"value"`
}

type FileSource struct {
	Path string `toml:"path"`
	Mime string `toml:"mime"`
	Data []byte `toml:"data,omitempty"` // Optional for image data
}

type SymbolSource struct {
	Path  string      `toml:"path"`
	Name  string      `toml:"name"`
	Kind  int         `toml:"kind"`
	Range SymbolRange `toml:"range"`
}

type SymbolRange struct {
	Start Position `toml:"start"`
	End   Position `toml:"end"`
}

type Position struct {
	Line int `toml:"line"`
	Char int `toml:"char"`
}

type Attachment struct {
	ID         string `toml:"id"`
	Type       string `toml:"type"`
	Display    string `toml:"display"`
	URL        string `toml:"url"`
	Filename   string `toml:"filename"`
	MediaType  string `toml:"media_type"`
	StartIndex int    `toml:"start_index"`
	EndIndex   int    `toml:"end_index"`
	Source     any    `toml:"source,omitempty"`
}

// NewAttachment creates a new attachment with a unique ID
func NewAttachment() *Attachment {
	return &Attachment{
		ID: uuid.NewString(),
	}
}

func (a *Attachment) GetTextSource() (*TextSource, bool) {
	if a.Type != "text" {
		return nil, false
	}
	ts, ok := a.Source.(*TextSource)
	return ts, ok
}

// GetFileSource returns the source as FileSource if the attachment is a file type
func (a *Attachment) GetFileSource() (*FileSource, bool) {
	if a.Type != "file" {
		return nil, false
	}
	fs, ok := a.Source.(*FileSource)
	return fs, ok
}

// GetSymbolSource returns the source as SymbolSource if the attachment is a symbol type
func (a *Attachment) GetSymbolSource() (*SymbolSource, bool) {
	if a.Type != "symbol" {
		return nil, false
	}
	ss, ok := a.Source.(*SymbolSource)
	return ss, ok
}