mirror of
https://github.com/tmedwards/tweego.git
synced 2025-07-04 21:50:33 -04:00
Initial Bitbucket→GitHub migration commit, based on release v2.0.0.
This commit is contained in:
commit
57e1aa52ff
36 changed files with 5026 additions and 0 deletions
42
passagedata.go
Normal file
42
passagedata.go
Normal file
|
@ -0,0 +1,42 @@
|
|||
/*
|
||||
Copyright © 2014–2019 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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue