tweego/passagedata.go
2020-02-24 14:28:13 -06:00

42 lines
1.1 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
Copyright © 20142020 Thomas Michael Edwards. All rights reserved.
Use of this source code is governed by a Simplified BSD License which
can be found in the LICENSE file.
*/
package main
import (
"encoding/json"
)
type passageMetadataJSON struct {
Position string `json:"position,omitempty"` // Twine 2 (`position`) & Twine 1 (`twine-position`).
Size string `json:"size,omitempty"` // Twine 2 (`size`).
}
func (p *passage) marshalMetadata() []byte {
marshaled, err := json.Marshal(&passageMetadataJSON{
p.metadata.position,
p.metadata.size,
})
if err != nil {
// NOTE: We should never be able to see an error here. If we do,
// then something truly exceptional—in a bad way—has happened, so
// we get our panic on.
panic(err)
}
return marshaled
}
func (p *passage) unmarshalMetadata(marshaled []byte) error {
metadata := passageMetadataJSON{}
if err := json.Unmarshal(marshaled, &metadata); err != nil {
return err
}
p.metadata = &passageMetadata{
position: metadata.Position,
size: metadata.Size,
}
return nil
}