pub enum Ref {
Local {
name: String,
},
Remote {
remote: String,
name: String,
},
Collection {
id: String,
name: String,
},
}Expand description
One ref of a repository.
A ref has three cases, and each case has a text form:
NAMEis the local refRef::Local, atrefs/heads/NAME.REMOTE:NAMEis the remote refRef::Remote, atrefs/remotes/REMOTE/NAME.@ID:NAMEis the collection refRef::Collection, atrefs/mirrors/ID/NAME. A repository whose[core] collection-idisIDkeeps the ref atrefs/heads/NAME, so@ID:NAMEandNAMEname the same ref there.canonicalgives the local form.
No valid ref name holds @ or :, so the three forms do not overlap.
The constructors do not check the value. validate
checks it, and a ref store checks it before it reads or writes a file.
FromStr reads the text and checks it. Display gives
the same text back.
§Examples
use ostrya_core::Ref;
let r: Ref = "@org.example.Os:os/x86_64".parse()?;
assert_eq!(r, Ref::collection("org.example.Os", "os/x86_64"));
assert_eq!(r.name(), "os/x86_64");
assert_eq!(r.to_string(), "@org.example.Os:os/x86_64");
assert_eq!("origin:main".parse::<Ref>()?, Ref::remote("origin", "main"));
assert!("@org.example.Os:a:b".parse::<Ref>().is_err());Variants§
Local
A local ref, at refs/heads/<name>.
Remote
A remote ref, at refs/remotes/<remote>/<name>.
Collection
A collection ref, at refs/mirrors/<id>/<name>, or at
refs/heads/<name> when id is the [core] collection-id of the
repository.
Implementations§
Source§impl Ref
impl Ref
Sourcepub fn local(name: impl Into<String>) -> Ref
pub fn local(name: impl Into<String>) -> Ref
Creates a local ref. The call does not check the name.
Sourcepub fn remote(remote: impl Into<String>, name: impl Into<String>) -> Ref
pub fn remote(remote: impl Into<String>, name: impl Into<String>) -> Ref
Creates a remote ref. The call does not check the values.
Sourcepub fn collection(id: impl Into<String>, name: impl Into<String>) -> Ref
pub fn collection(id: impl Into<String>, name: impl Into<String>) -> Ref
Creates a collection ref. The call does not check the values.
Sourcepub fn canonical(self, own: Option<&str>) -> Ref
pub fn canonical(self, own: Option<&str>) -> Ref
Returns the canonical form of the ref in a repository whose
[core] collection-id is own.
A collection ref of own is the local ref of the same name, because
the repository keeps it at refs/heads/<name>. Each other ref is
returned unchanged, and so is each ref if own is None. The call
does not check the ref or own.
Sourcepub fn in_collection(self, id: &str) -> Result<Ref, Ref>
pub fn in_collection(self, id: &str) -> Result<Ref, Ref>
Puts a local ref in the collection id.
A local ref NAME becomes @ID:NAME. A collection ref of id stays,
and so does a remote ref. The call does not check the ref or id.
§Errors
Gives back the ref unchanged if it is a collection ref of another id.
Sourcepub fn namespace(&self) -> RefNamespace<'_>
pub fn namespace(&self) -> RefNamespace<'_>
Returns the namespace of the ref: the local refs, the refs of one remote, or the refs of one collection id.
The call does not canonicalize the ref. A collection ref of the own
id of a repository is in the namespace of its local refs only after
canonical.
Sourcepub fn same_namespace(&self, other: &Ref) -> bool
pub fn same_namespace(&self, other: &Ref) -> bool
Returns true if the refs self and other are in one namespace:
both local, both of one remote, or both of one collection id.
Sourcepub fn collection_conflict<'a>(
refs: impl IntoIterator<Item = &'a Ref>,
) -> Option<(&'a Ref, &'a Ref)>
pub fn collection_conflict<'a>( refs: impl IntoIterator<Item = &'a Ref>, ) -> Option<(&'a Ref, &'a Ref)>
Returns the first ref of refs that is in another collection than
an earlier ref of refs, with that earlier ref.
The refs that are not collection refs are ignored. The call returns
None if the collection refs of refs hold one collection id.
Sourcepub fn validate(&self) -> Result<(), ParseRefError>
pub fn validate(&self) -> Result<(), ParseRefError>
Checks the ref against the ref-name rule.
The ref name must pass is_ref_name in each case. The remote of
Ref::Remote must pass is_ref_name_component. The id of
Ref::Collection must pass is_collection_id and be at most
MAX_COLLECTION_ID_LEN bytes. So the path of a valid ref stays
inside the refs/ tree.
§Errors
ParseRefError if a part fails its rule. The error holds the text
of the ref.
Trait Implementations§
impl Eq for Ref
Source§impl FromStr for Ref
impl FromStr for Ref
Source§fn from_str(text: &str) -> Result<Ref, ParseRefError>
fn from_str(text: &str) -> Result<Ref, ParseRefError>
Reads the text form of a ref and checks it with
validate.
Text that starts with @ is a collection ref, split at the first :
after the @. Other text with a : is a remote ref, split at the
first :. Text with no : is a local ref.
§Errors
ParseRefError if the text has no form or a part fails its rule.
The error holds the text as given.