atom/inlineOtherContent.go

51 lines
1.5 KiB
Go
Raw Permalink Normal View History

2024-10-13 17:19:40 +02:00
package atomfeed
2024-10-15 21:01:40 +02:00
import (
"errors"
"fmt"
"mime"
)
2024-10-13 17:19:40 +02:00
type InlineOtherContent struct {
*CommonAttributes
2024-10-15 21:01:40 +02:00
AnyElement any `xml:"anyelement,omitempty"`
2024-10-13 17:19:40 +02:00
Type MediaType `xml:"type,attr,omitempty"`
2024-10-15 21:01:40 +02:00
}
2024-10-16 19:59:28 +02:00
// newInlineOtherContent creates a new InlineOtherContent. It returns a
// *InlineOtherContent and an error.
2024-10-15 21:01:40 +02:00
func newInlineOtherContent(mediaType string, content any) (*InlineOtherContent, error) {
if mediaType, _, err := mime.ParseMediaType(mediaType); err != nil {
return nil, fmt.Errorf("media type %v incompatible with inline other content", mediaType)
}
return &InlineOtherContent{Type: MediaType(mediaType), AnyElement: content}, nil
2024-10-13 17:19:40 +02:00
}
2024-10-16 19:59:28 +02:00
// isContent checks whether the InlineOtherContent is a Content. It returns a
// bool.
2024-10-15 19:32:14 +02:00
func (i *InlineOtherContent) isContent() bool { return true }
2024-10-16 19:59:28 +02:00
// hasSRC checks whether the InlineOtherContent has a SRC attribute. It returns
// a bool.
2024-10-15 19:32:14 +02:00
func (i *InlineOtherContent) hasSRC() bool { return false }
2024-10-16 19:59:28 +02:00
// getType returns the Type of the InlineOtherContent as a string.
2024-10-15 19:32:14 +02:00
func (i *InlineOtherContent) getType() string { return string(i.Type) }
2024-10-13 17:19:40 +02:00
2024-10-16 19:59:28 +02:00
// Check checks the InlineOtherContent for incompatibilities with RFC4287. It
// returns an error.
func (i *InlineOtherContent) Check() error {
mediaType := i.getType()
if mediaType, _, err := mime.ParseMediaType(mediaType); err != nil {
return fmt.Errorf("type attribute %v incompatible with inline other content", mediaType)
}
if isCompositeMediaType(mediaType) {
return errors.New("type attribute of inline other content must not be a composite type")
}
return nil
}