aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/sisudoc/ocda/abstraction/ssp_in.d
diff options
context:
space:
mode:
Diffstat (limited to 'src/sisudoc/ocda/abstraction/ssp_in.d')
-rw-r--r--src/sisudoc/ocda/abstraction/ssp_in.d77
1 files changed, 67 insertions, 10 deletions
diff --git a/src/sisudoc/ocda/abstraction/ssp_in.d b/src/sisudoc/ocda/abstraction/ssp_in.d
index 220bfe3..71daf50 100644
--- a/src/sisudoc/ocda/abstraction/ssp_in.d
+++ b/src/sisudoc/ocda/abstraction/ssp_in.d
@@ -76,8 +76,11 @@ template spineAbstractionRead() {
object sections in the order the file gives them
+/
struct SSPdocument {
- string format; // "% SiSU Document Abstraction v0.1"
+ string format; // "% SiSU Document Abstraction v1.1"
string source; // "% Source: ..."
+ string[string] source_info; // the @source block
+ string[] source_info_order;
+ string ssp_digest; // sha256 of the .ssp read, hex
string[string] meta;
string[] meta_order;
string[string] make;
@@ -152,7 +155,7 @@ template spineAbstractionRead() {
_closeObject();
string _name = line[1 .. $-2];
switch (_name) {
- case "meta": case "make": case "doc_has":
+ case "source": case "meta": case "make": case "doc_has":
block = _name; section = ""; break;
default:
block = ""; section = _name;
@@ -167,6 +170,10 @@ template spineAbstractionRead() {
auto _kv = line.stripLeft.findSplit(": "); // leading only: a value may end in a space
if (_kv[1].length > 0) {
final switch (block) {
+ case "source":
+ doc.source_info[_kv[0].to!string] = _kv[2].to!string;
+ doc.source_info_order ~= _kv[0].to!string;
+ break;
case "meta":
doc.meta[_kv[0].to!string] = _kv[2].to!string;
doc.meta_order ~= _kv[0].to!string;
@@ -332,24 +339,74 @@ template spineAbstractionRead() {
}
return doc;
}
- /+ ↓ the abstraction as it survives a trip through the .ssp text: emitted
- as lines by the writer, read straight back. what a consumer built
- from this cannot see, the .ssp does not carry, which is what makes a
- second artefact built from it unable to drift
+ /+ ↓ the whole document as it survives a trip through the .ssp text:
+ emitted as lines by the writer, read straight back. what a consumer
+ built from this cannot see, the .ssp does not carry, which is what
+ makes a second artefact built from it unable to drift. the header
+ blocks come back with it, so the database is filled from these and
+ not from a second list read off doc_matters
+/
- auto sspRoundTripAbstraction(D)(D doc) {
+ SSPdocument sspRoundTripDocument(D)(D doc) {
import sisudoc.ocda.abstraction.ssp;
mixin spineAbstractionTxt;
- return sspRead(sspDocumentLines(doc)).abstraction;
+ string[] _lines = sspDocumentLines(doc);
+ auto _out = sspRead(_lines);
+ /+ ↓ the writer emits one line per entry, each with its newline, so this
+ is the digest of the file that is written beside the pod +/
+ _out.ssp_digest = sspDigestOfText(_lines.join("\n") ~ "\n");
+ return _out;
+ }
+ /+ ↓ sha256 of a .ssp, upper case hex, as digests.txt gives it +/
+ string sspDigestOfText(string _text) {
+ import std.digest : toHexString;
+ import std.digest.sha : sha256Of;
+ return _text.sha256Of.toHexString.to!string;
+ }
+ auto sspRoundTripAbstraction(D)(D doc) {
+ return sspRoundTripDocument(doc).abstraction;
+ }
+ /+ ↓ the version a reader will accept.
+
+ the major part must match: a format whose properties have changed
+ meaning is not readable by guesswork, and loading it half populated in
+ silence is the failure this check exists to stop. a newer minor
+ version is accepted, since a minor bump only adds properties and an
+ unknown property line is ignored.
+ +/
+ bool sspFormatVersionOk(string _format_line, string _what) {
+ import sisudoc.ocda.abstraction.ssp : ssp_format_version, ssp_format_version_major;
+ enum _prefix = "% SiSU Document Abstraction v";
+ if (_format_line.length == 0) {
+ writeln("ERROR: ", _what, ": no format line, not a .ssp");
+ return false;
+ }
+ if (!_format_line.startsWith(_prefix)) {
+ writeln("ERROR: ", _what, ": unrecognised format line: ", _format_line);
+ return false;
+ }
+ string _ver = _format_line[_prefix.length .. $].strip;
+ string _major = _ver.findSplit(".")[0];
+ if (_major != ssp_format_version_major) {
+ writeln("ERROR: ", _what, ": .ssp format v", _ver,
+ ", this spine reads v", ssp_format_version_major, ".x (v",
+ ssp_format_version, "). Regenerate the abstraction from its markup.");
+ return false;
+ }
+ return true;
}
SSPdocument sspReadFile(string file_path) {
+ string _text;
string[] _lines;
try {
- _lines = (cast(char[]) file_path.read).to!string.split("\n");
+ _text = (cast(char[]) file_path.read).to!string;
+ _lines = _text.split("\n");
} catch (Exception ex) {
writeln("ERROR: could not read ", file_path, ": ", ex.msg);
return SSPdocument();
}
- return sspRead(_lines);
+ auto _doc = sspRead(_lines);
+ if (!sspFormatVersionOk(_doc.format, file_path)) { return SSPdocument(); }
+ _doc.ssp_digest = sspDigestOfText(_text);
+ return _doc;
}
}