pub fn card_names_owner(card: &[u8], owner: &DidNostr) -> boolExpand description
Returns true when an existing profile card names owner, for
profile init’s decision to leave it alone.
The check is conservative: it may say false for a card a full Turtle
parser would accept, in which case profile init reports a conflict and
changes nothing without --replace. It never says true for a card
that is not Turtle, or that does not state, about <#me>,
owl:sameAs <did:nostr:<owner>>.
The whole card is read against a subset of the Turtle grammar, and any
byte outside that subset makes the answer false, wherever it is. The
subset covers @prefix and PREFIX directives; triples whose subject
is an IRI, a prefixed name, a blank-node label, a blank-node property
list or a collection; predicate lists with ; (a ; with nothing after
it included) and object lists with ,; and objects that are IRIs,
prefixed names, labels, nested lists and collections, string literals in
all four quotings with an optional language tag or datatype, numbers
and booleans. It returns true only when all of these hold:
- the bytes are UTF-8 and every statement is complete and ends with
.(aPREFIXdirective has none); - whitespace is only space, tab, CR and LF; a comment ends at CR or LF; a short string holds no raw CR or LF, and every escape in a string is one Turtle defines;
- no IRI and no prefixed name contains
\, so no escape can disguise one, and every prefixed name uses a prefix declared before it; - there is no
@baseorBASEdirective and no other@keyword, so<#me>means this document; - some triple has the subject
<#me>and, in its own predicate list, a predicate that isowl:sameAs(written as the full IRI, or as a prefixed name whose expansion, with the prefixes declared at that point, is exactly that IRI) with the object<did:nostr:<owner hex>>; - that triple contains no blank-node or collection brackets;
- no IRI in the card begins with
did:(in any case) other than the owner’s exact one, and no prefix declaration’s IRI begins withdid:at all, the owner’s included, so no other identifier can be named directly or built from a prefix.
An identifier in a comment or a string literal does not count, nor does one stated about another subject.
§Examples
use stratum_node_core::{card_names_owner, DidNostr};
let owner = DidNostr::from_hex("79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798")?;
let custom = format!(
"@prefix foaf: <http://xmlns.com/foaf/0.1/> .\n\
@prefix owl: <http://www.w3.org/2002/07/owl#> .\n\
<#me> a foaf:Person ; foaf:name \"Ada\" ; owl:sameAs <{owner}> .\n"
);
assert!(card_names_owner(custom.as_bytes(), &owner));
let comment_only = format!("<#me> a <http://xmlns.com/foaf/0.1/Person> . # owl:sameAs <{owner}>\n");
assert!(!card_names_owner(comment_only.as_bytes(), &owner));
// A comment ends at CR, so this base directive is live.
let hidden_base = format!(
"# note\r@base <https://elsewhere.example/> .\n\
<#me> <http://www.w3.org/2002/07/owl#sameAs> <{owner}> .\n"
);
assert!(!card_names_owner(hidden_base.as_bytes(), &owner));
// A statement must be complete.
let unfinished = format!("<#me> <http://www.w3.org/2002/07/owl#sameAs> <{owner}>\n");
assert!(!card_names_owner(unfinished.as_bytes(), &owner));