use jiff::Timestamp;
use l10n_embed::Localize;
use l10n_embed_derive::localize;
use pijul_extension::FileSystemRepository;
use pijul_extension::file_system::changes::CreditSource;
use pijul_extension::file_system::open_file::OpenFile;
#[localize("l10n/**/inline_credit/line_annotation.ftl")]
enum AuthorKind<'change_header> {
Local,
Remote {
username: &'change_header str,
},
Unknown {
public_key_signature: &'change_header str,
},
}
#[localize("l10n/**/inline_credit/line_annotation.ftl")]
enum LineAnnotation<'change_header> {
Tracked {
authors: l10n_embed::list::AndList<AuthorKind<'change_header>>,
timestamp: Timestamp,
message: String,
},
Untracked {
timestamp: Timestamp,
},
}
pub fn render(
credit_source: &CreditSource,
open_file: &OpenFile,
text_document_is_dirty: bool,
repository: &FileSystemRepository,
localization_context: &l10n_embed::Context,
) -> Result<String, napi::Error> {
let annotation = match credit_source {
CreditSource::Tracked { vertex } => {
let change = repository.get_change(vertex.change).map_err(|error| {
napi::Error::from_reason(format!(
"Unable to get change for vertex {vertex:#?}: {error}"
))
})?;
let authors = repository
.authors_for_change(&change.header)
.into_iter()
.map(|author_source| match author_source {
pijul_extension::author::AuthorSource::Local(_identity) => AuthorKind::Local,
pijul_extension::author::AuthorSource::Remote(identity) => AuthorKind::Remote {
username: &identity.config.author.username,
},
pijul_extension::author::AuthorSource::Unknown(public_key_signature) => {
AuthorKind::Unknown {
public_key_signature,
}
}
})
.collect();
LineAnnotation::Tracked {
authors: l10n_embed::list::AndList::new(authors),
timestamp: change.header.timestamp,
message: change.hashed.header.message,
}
}
CreditSource::Untracked { .. } => {
let timestamp = if text_document_is_dirty {
Timestamp::now()
} else {
open_file.contents.modified_time
};
LineAnnotation::Untracked { timestamp }
}
};
let mut localized_annotation = String::new();
annotation.localize(localization_context, &mut localized_annotation);
Ok(localized_annotation)
}