use crate::error::Error;
use crate::platform;
use crate::platform::ffi;
pub struct Reference {
handle: ffi::CzReference,
}
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct NewOptions {
pub domain: Option<String>,
pub tag: Option<String>,
pub digest: Option<String>,
}
impl Reference {
pub fn new(path: &str, options: NewOptions) -> Result<Self, Error> {
Self::made(
ffi::cz_reference_new(path, options.domain, options.tag, options.digest),
format!("make a reference to {path}"),
)
}
pub fn parse(string: &str) -> Result<Self, Error> {
Self::made(
ffi::cz_reference_parse(string),
format!("parse {string:?} as a reference"),
)
}
pub fn with_name(name: &str) -> Result<Self, Error> {
Self::made(
ffi::cz_reference_with_name(name),
format!("make a reference named {name:?}"),
)
}
pub fn resolve_domain(domain: &str) -> Result<String, Error> {
platform::outcome(
ffi::cz_reference_resolve_domain(domain),
format!("resolve the domain {domain:?}"),
)
.map(|outcome| outcome.text())
}
fn made(outcome: ffi::CzOutcome, action: String) -> Result<Self, Error> {
platform::outcome(outcome, action).map(|outcome| Self {
handle: outcome.reference(),
})
}
pub fn domain(&self) -> Option<String> {
self.handle.domain()
}
pub fn resolved_domain(&self) -> Option<String> {
self.handle.resolved_domain()
}
pub fn path(&self) -> String {
self.handle.path()
}
pub fn tag(&self) -> Option<String> {
self.handle.tag()
}
pub fn digest(&self) -> Option<String> {
self.handle.digest()
}
pub fn name(&self) -> String {
self.handle.name()
}
pub fn description(&self) -> String {
self.handle.description()
}
pub fn with_tag(&self, tag: &str) -> Result<Self, Error> {
Self::made(
self.handle.with_tag(tag),
format!("tag {} with {tag:?}", self.description()),
)
}
pub fn with_digest(&self, digest: &str) -> Result<Self, Error> {
Self::made(
self.handle.with_digest(digest),
format!("give {} the digest {digest:?}", self.description()),
)
}
pub fn normalize(&mut self) {
self.handle.normalize();
}
}