mirror of
git://jb55.com/damus
synced 2024-09-19 19:46:51 +00:00
43 lines
1.2 KiB
Swift
43 lines
1.2 KiB
Swift
|
//
|
||
|
// NdbNote.swift
|
||
|
// damus
|
||
|
//
|
||
|
// Created by William Casarin on 2023-07-21.
|
||
|
//
|
||
|
|
||
|
import Foundation
|
||
|
|
||
|
struct NdbNote {
|
||
|
private var owned: Data?
|
||
|
let note: UnsafeMutablePointer<ndb_note>
|
||
|
|
||
|
init(notePointer: UnsafeMutablePointer<ndb_note>, data: Data?) {
|
||
|
self.note = notePointer
|
||
|
self.owned = data
|
||
|
}
|
||
|
|
||
|
var id: Data {
|
||
|
Data(buffer: UnsafeBufferPointer(start: ndb_note_id(note), count: 32))
|
||
|
}
|
||
|
|
||
|
func tags() -> TagsSequence {
|
||
|
return .init(note: note)
|
||
|
}
|
||
|
|
||
|
static func owned_from_json(json: String, bufsize: Int = 2 << 18) -> NdbNote? {
|
||
|
var data = Data(capacity: bufsize)
|
||
|
guard var json_cstr = json.cString(using: .utf8) else { return nil }
|
||
|
|
||
|
var note: UnsafeMutablePointer<ndb_note>?
|
||
|
|
||
|
let len = data.withUnsafeMutableBytes { (bytes: UnsafeMutableRawBufferPointer) -> Int in
|
||
|
return Int(ndb_note_from_json(&json_cstr, Int32(json_cstr.count), ¬e, bytes.baseAddress, Int32(bufsize)))
|
||
|
}
|
||
|
|
||
|
guard let note else { return nil }
|
||
|
|
||
|
// Create new Data with just the valid bytes
|
||
|
let validData = Data(bytes: ¬e.pointee, count: len)
|
||
|
return NdbNote(notePointer: note, data: validData)
|
||
|
}}
|