feat(file): Expose API to inspect iKv file format and storage kind
Some checks failed
Build / cmake-build (push) Failing after 20s
Build / unit-tests (push) Failing after 24s

This commit is contained in:
2026-06-19 16:50:55 -05:00
parent b5c13725de
commit 5500cd6984
2 changed files with 35 additions and 0 deletions

View File

@@ -25,6 +25,19 @@ enum ValueType {
/// On-disk iKv format versions supported for explicit writes. /// On-disk iKv format versions supported for explicit writes.
enum class Version : unsigned { v1 = 1, v2 = 2 }; enum class Version : unsigned { v1 = 1, v2 = 2 };
/// Serialized storage kind of an iKv document.
enum class StorageKind : unsigned {
unknown = 0,
text,
binary
};
/// File format information reported by the library.
struct FileFormat {
StorageKind storage = StorageKind::unknown;
Version version = Version::v2;
};
/// Error raised for invalid types, unsupported operations, parse failures, /// Error raised for invalid types, unsupported operations, parse failures,
/// and I/O failures. /// and I/O failures.
class Error : public std::runtime_error { class Error : public std::runtime_error {
@@ -145,4 +158,8 @@ private:
bool read_only_ = false; bool read_only_ = false;
}; };
/// Inspects an iKv file and reports whether it is text or binary plus its version.
/// Throws Error when the file cannot be identified.
FileFormat inspectFile(const std::string& path);
} // namespace ikv } // namespace ikv

View File

@@ -39,6 +39,10 @@ ValueType publicType(ikv_type_t type) noexcept {
} }
} }
Version publicVersion(ikv_version_t version) {
return version == IKV_VERSION_1 ? Version::v1 : Version::v2;
}
} // namespace } // namespace
void Value::Deleter::operator()(ikv_node_t* node) const noexcept { ikv_free(node); } void Value::Deleter::operator()(ikv_node_t* node) const noexcept { ikv_free(node); }
@@ -251,4 +255,18 @@ void Value::refresh(const std::string& path) {
if (!ikv_refresh_from_path(node_, path.c_str())) throw Error("failed to refresh iKv root from: " + path); if (!ikv_refresh_from_path(node_, path.c_str())) throw Error("failed to refresh iKv root from: " + path);
} }
FileFormat inspectFile(const std::string& path) {
if (ikv_version_t version = ikv_detect_file_version(path.c_str(), true);
version != IKV_VERSION_UNKNOWN) {
return {StorageKind::binary, publicVersion(version)};
}
if (ikv_version_t version = ikv_detect_file_version(path.c_str(), false);
version != IKV_VERSION_UNKNOWN) {
return {StorageKind::text, publicVersion(version)};
}
throw Error("failed to identify iKv file format: " + path);
}
} // namespace ikv } // namespace ikv