use std::collections::{BTreeMap, BTreeSet};
use nibkeep_base::{EntityId, Hash, SpaceId, StatementId};
use nibkeep_log::ChangeId;
use nibkeep_model::storage::Statements;
use nibkeep_model::{FieldName, Operation, State, Value, fields};
use nibkeep_std::{
COMMITTED_IN, CONTENT, EXECUTABLE, IN_TREE, LINK_TARGET, MESSAGE, PARTS, REVISION,
SECRET_CONTENT, SIZE, TREE_PATH,
};
use nibkeep_time::Timestamp;
use super::{Change, Changes, TreeChange, TreeState, read_tree_state, tree_entities};
use crate::NodeError;
use crate::library::{Author, Library, Space};
pub(super) const READ_LIMIT: u64 = 64 * 1024 * 1024;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TreePoint {
Now,
Revision(EntityId),
AsOf(u64),
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TreeLogEntry {
pub change: String,
pub at: u64,
pub author: Option<Author>,
pub revision: Option<EntityId>,
pub message: Option<String>,
pub part: Option<(u64, u64)>,
pub changes: Vec<TreeChange>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct BlameLine {
pub text: String,
pub revision: Option<EntityId>,
pub author: Option<Author>,
pub at: u64,
}
#[derive(Clone, Debug, Default)]
struct Said {
fields: BTreeMap<FieldName, Value>,
}
#[derive(Clone, Debug)]
struct Revision {
message: Option<String>,
parts: Option<u64>,
}
#[derive(Clone, Copy, Debug)]
pub(super) struct PartHere {
statement: StatementId,
at: Timestamp,
change: Option<Hash>,
restated: bool,
}
impl Library {
pub(super) fn tree_state_at(
&self,
space: SpaceId,
tree: EntityId,
point: TreePoint,
) -> Result<TreeState, NodeError> {
let tree = self.check_tree(space, tree)?;
match point {
TreePoint::Now => self.tree_state(space, tree),
TreePoint::AsOf(millis) => {
let open = self.space(space)?;
open.stored.reading(|stored| {
let state = stored.log().state();
let entities = tree_entities(state, tree)?;
let millis = before_commits_under_way(stored, state, tree, millis)?;
let log = stored.log();
let changes = Changes::of(log);
let readable = self.secrets_unlocked(space);
let then = log.state_as_of(millis);
read_tree_state(&then, tree, &entities, (Some(&changes), readable))
})
}
TreePoint::Revision(revision) => {
let changes = self.revision_changes(space, tree, revision)?;
let last = *changes
.last()
.ok_or_else(|| NodeError::from(NoRevision(revision)))?;
Ok(self
.tree_states_of_changes(space, tree, last, &BTreeSet::new())?
.0)
}
}
}
pub(super) fn tree_states_of_changes(
&self,
space: SpaceId,
tree: EntityId,
last: ChangeId,
without: &BTreeSet<ChangeId>,
) -> Result<(TreeState, TreeState), NodeError> {
self.tree_states_from(space, tree, &[last], without)
}
pub(super) fn tree_states_from(
&self,
space: SpaceId,
tree: EntityId,
heads: &[ChangeId],
without: &BTreeSet<ChangeId>,
) -> Result<(TreeState, TreeState), NodeError> {
let open = self.space(space)?;
let log = open.stored.log();
let entities: BTreeSet<EntityId> = open
.stored
.reading(|stored| tree_entities(stored.log().state(), tree))?
.into_iter()
.collect();
let mut ancestors = Vec::new();
let mut seen = BTreeSet::new();
let mut waiting = heads.to_vec();
while let Some(id) = waiting.pop() {
if !seen.insert(id) {
continue;
}
let Some(change) = log.change(id)? else {
continue;
};
waiting.extend(change.parents().iter().copied());
ancestors.push((change.timestamp(), id));
}
ancestors.sort();
let mut restated: Option<BTreeMap<ChangeId, Vec<Operation>>> = None;
let mut with: State = State::new();
let mut apart: State = State::new();
for (at, id) in ancestors {
let status = nibkeep_log::History::status(log.history(), id)?;
let operations = match status {
Some(nibkeep_log::Status::Applied) => {
log.payload(id, &open.keys)?.map_or_default(|payload| {
let mut operations = payload.operations;
operations.extend(opened(log, payload.sealed.keys()));
operations
})
}
Some(nibkeep_log::Status::Ignored) => {
let restated = match &mut restated {
Some(restated) => restated,
None => restated.insert(restated_by_purges(open)?),
};
restated.get(&id).cloned().unwrap_or_default()
}
_ => continue,
};
for operation in &operations {
let about_tree = match operation {
Operation::Assert { statement, .. } => entities.contains(&statement.entity),
Operation::Retract { .. } | Operation::RetractSecret { .. } => true,
Operation::Unknown(_) => false,
};
if about_tree {
let Ok(()) = with.apply_in(operation, at, Some(*id.hash()));
if !without.contains(&id) {
let Ok(()) = apart.apply_in(operation, at, Some(*id.hash()));
}
}
}
}
let entities: Vec<EntityId> = entities.into_iter().collect();
let changes = Changes::of(log);
let readable = self.secrets_unlocked(space);
Ok((
read_tree_state(&with, tree, &entities, (Some(&changes), readable))?,
read_tree_state(&apart, tree, &entities, (Some(&changes), readable))?,
))
}
pub(super) fn tree_state_without(
&self,
space: SpaceId,
tree: EntityId,
without: &BTreeSet<ChangeId>,
) -> Result<TreeState, NodeError> {
let heads: Vec<ChangeId> = self
.space(space)?
.stored
.log()
.heads()?
.into_iter()
.collect();
Ok(self.tree_states_from(space, tree, &heads, without)?.1)
}
pub(super) fn tree_of_revision(
&self,
space: SpaceId,
revision: EntityId,
) -> Result<Option<EntityId>, NodeError> {
let open = self.space(space)?;
open.stored.reading(|stored| {
let state = stored.log().state();
let revision = state.canonical(revision)?;
if !state.types(revision)?.contains(&REVISION) {
return Ok(None);
}
for statement in state.current(revision, &IN_TREE)? {
if let Value::Reference(tree) = statement.value {
return Ok::<_, NodeError>(Some(state.canonical(tree)?));
}
}
Ok(None)
})
}
pub(super) fn revision_changes(
&self,
space: SpaceId,
tree: EntityId,
revision: EntityId,
) -> Result<Vec<ChangeId>, NodeError> {
let open = self.space(space)?;
let parts = open.stored.reading(|stored| {
let state = stored.log().state();
let revision = state.canonical(revision)?;
if !is_revision_of(state, revision, tree)? {
return Err(NoRevision(revision).into());
}
let mut parts: Vec<PartHere> = first_part(state, revision)?.into_iter().collect();
parts.extend(parts_here(state, revision)?);
Ok::<_, NodeError>(parts)
})?;
let mut restated = None;
let mut changes = Vec::new();
for part in parts {
let change = if part.restated {
let restated = match &mut restated {
Some(restated) => restated,
None => restated.insert(restated_by_purges(open)?),
};
restated
.iter()
.find(|(_, operations)| {
operations.iter().any(|operation| {
matches!(operation, Operation::Assert { statement, .. }
if statement.id == part.statement)
})
})
.map(|(source, _)| *source)
} else {
part.change.map(ChangeId::from_hash)
};
changes.extend(change.map(|change| (part.at, change)));
}
changes.sort();
changes.dedup();
Ok(changes.into_iter().map(|(_, change)| change).collect())
}
pub fn tree_log(
&self,
space: SpaceId,
tree: EntityId,
path: Option<&str>,
) -> Result<Vec<TreeLogEntry>, NodeError> {
let tree = self.check_tree(space, tree)?;
let chosen = match path {
Some(path) => {
let state = self.tree_state(space, tree)?;
let current = state
.by_path
.get(path)
.or_else(|| state.deleted.get(path))
.ok_or_else(|| NodeError::NotInTree(path.to_owned()))?;
Some(current.entity)
}
None => None,
};
let open = self.space(space)?;
open.stored.reading(|stored| {
let state = stored.log().state();
let entities = tree_entities(state, tree)?;
let revisions = revisions_of(stored, state, tree)?;
let told = told_by_changes(state, tree, &entities, &revisions)?;
let Commits {
by_change: by_change_revision,
made,
} = commits(state, &revisions, &told.parts)?;
let paths = &told.paths;
let what_changed = |about: &BTreeMap<EntityId, Said>, at: Timestamp| {
about
.iter()
.filter_map(|(entity, said)| {
let path_then = |before: bool| {
paths.get(entity).and_then(|list| {
list.iter()
.rev()
.find(|(when, _)| if before { *when < at } else { *when <= at })
.map(|(_, path)| path.clone())
})
};
what_it_did(said, path_then(false), path_then(true))
})
.collect::<Vec<TreeChange>>()
};
let mut grouped: Vec<Grouped> = Vec::new();
let mut at_entry: BTreeMap<Result<EntityId, Hash>, usize> = BTreeMap::new();
for ((change, recorded), about) in told.by_change {
let about_chosen = chosen.is_none_or(|chosen| about.contains_key(&chosen));
let commit = by_change_revision.get(&(change, recorded)).copied();
let changes = what_changed(&about, recorded);
match at_entry.entry(commit.ok_or(change)) {
std::collections::btree_map::Entry::Occupied(entry) => {
let entry = &mut grouped[*entry.get()];
entry.changes.extend(changes);
entry.chosen |= about_chosen;
}
std::collections::btree_map::Entry::Vacant(entry) => {
entry.insert(grouped.len());
grouped.push(Grouped {
change,
commit,
changes,
chosen: about_chosen,
});
}
}
}
let mut entries: Vec<(Timestamp, TreeLogEntry)> = Vec::new();
for grouped in grouped.into_iter().filter(|grouped| grouped.chosen) {
let reading = (open, stored, state);
if let Some(entry) = self.log_entry(reading, grouped, (&revisions, &made))? {
entries.push(entry);
}
}
entries.sort_by(|a, b| b.0.cmp(&a.0).then(b.1.change.cmp(&a.1.change)));
Ok(entries.into_iter().map(|(_, entry)| entry).collect())
})
}
#[allow(clippy::type_complexity, reason = "what the log read, passed along")]
fn log_entry<S: Statements>(
&self,
(open, stored, state): (&Space, &nibkeep_store::StoredLog, &State<S>),
grouped: Grouped,
(revisions, made): (&BTreeMap<EntityId, Revision>, &BTreeMap<EntityId, u64>),
) -> Result<Option<(Timestamp, TreeLogEntry)>, NodeError>
where
NodeError: From<S::Error>,
{
let mut changes = grouped.changes;
changes.sort_by(|a, b| a.path.cmp(&b.path));
changes.dedup();
let first = match grouped.commit {
Some(commit) => type_change(state, commit)?,
None => None,
};
let change = first.unwrap_or_else(|| ChangeId::from_hash(grouped.change));
let Some(header) = stored.log().change(change)? else {
return Ok(None);
};
let revision = grouped.commit.and_then(|commit| revisions.get(&commit));
let part = grouped.commit.and_then(|commit| {
let made = made.get(&commit).copied().unwrap_or(0);
let of = revision.and_then(|revision| revision.parts).unwrap_or(made);
(made < of).then_some((made, of))
});
let at = header.timestamp();
Ok(Some((
at,
TreeLogEntry {
change: change.to_string(),
at: at.millis(),
author: self.author_with_key(open, stored, header.author())?,
revision: grouped.commit,
message: revision.and_then(|revision| revision.message.clone()),
part,
changes,
},
)))
}
pub fn tree_blame(
&self,
space: SpaceId,
tree: EntityId,
path: &str,
) -> Result<Vec<BlameLine>, NodeError> {
self.tree_blame_with(space, tree, path, false)
}
pub fn tree_blame_with(
&self,
space: SpaceId,
tree: EntityId,
path: &str,
secrets: bool,
) -> Result<Vec<BlameLine>, NodeError> {
let tree = self.check_tree(space, tree)?;
let state = self.tree_state(space, tree)?;
let entity = match (state.by_path.get(path), state.sealed.get(path)) {
(Some(current), _) => current.entity,
(None, Some(_)) if !secrets => return Err(NodeError::SecretLines(path.to_owned())),
(None, Some(_)) => return Err(NodeError::SecretsLocked),
(None, None) => return Err(NodeError::NotInTree(path.to_owned())),
};
let secret = state.is_secret(entity);
if secret && !secrets {
return Err(NodeError::SecretLines(path.to_owned()));
}
if secret && !self.secrets_unlocked(space) {
return Err(NodeError::SecretsLocked);
}
let field = if secret { SECRET_CONTENT } else { CONTENT };
let open = self.space(space)?;
let versions: Vec<(Hash, Hash, Timestamp)> = open.stored.reading(|stored| {
let state = stored.log().state();
let storage = state.storage();
let mut versions = Vec::new();
for id in storage.of_field(entity, &field)? {
if let Some(recorded) = storage.get(id)?
&& let (Value::Blob(content), Some(change)) =
(&recorded.statement.value, recorded.change)
{
versions.push((recorded.at, id, *content, change));
}
}
versions.sort();
Ok::<_, NodeError>(
versions
.into_iter()
.map(|(at, _, content, change)| (content, change, at))
.collect(),
)
})?;
let mut lines: Vec<(String, Hash, u64)> = Vec::new();
let versions: Vec<(Hash, Hash, u64)> = versions
.into_iter()
.map(|(content, change, at)| (content, change, at.millis()))
.collect();
let mut previous: Option<String> = None;
for (content, change, at) in versions {
let bytes = self
.version_bytes(space, &content, secret)?
.ok_or_else(|| NodeError::NoCopyHere(path.to_owned()))?;
if !super::diff::is_text(&bytes) {
return Err(NodeError::NotText(path.to_owned()));
}
let text = String::from_utf8_lossy(&bytes).into_owned();
lines = match &previous {
None => split_lines(&text)
.into_iter()
.map(|line| (line, change, at))
.collect(),
Some(before) => carried(before, &text, &lines, (change, at)),
};
previous = Some(text);
}
let revisions = open.stored.reading(|stored| {
let storage = stored.log().state().storage();
let mut by_change = BTreeMap::new();
for id in storage.of_field(entity, &COMMITTED_IN)? {
if let Some(recorded) = storage.get(id)?
&& let (Value::Reference(revision), Some(change)) =
(&recorded.statement.value, recorded.change)
{
by_change.insert(change, *revision);
}
}
Ok::<_, NodeError>(by_change)
})?;
let mut authors: BTreeMap<Hash, Option<Author>> = BTreeMap::new();
let mut blamed = Vec::with_capacity(lines.len());
for (text, change, at) in lines {
let author = if let Some(author) = authors.get(&change) {
author.clone()
} else {
let author = open.stored.reading(|stored| {
match stored.log().change(ChangeId::from_hash(change))? {
Some(header) => self.author_with_key(open, stored, header.author()),
None => Ok(None),
}
})?;
authors.insert(change, author.clone());
author
};
blamed.push(BlameLine {
text,
revision: revisions.get(&change).copied(),
author,
at,
});
}
Ok(blamed)
}
}
struct Grouped {
change: Hash,
commit: Option<EntityId>,
changes: Vec<TreeChange>,
chosen: bool,
}
struct Commits {
by_change: BTreeMap<(Hash, Timestamp), EntityId>,
made: BTreeMap<EntityId, u64>,
}
fn commits<S: Statements>(
state: &State<S>,
revisions: &BTreeMap<EntityId, Revision>,
parts: &[(EntityId, Option<(Hash, Timestamp)>)],
) -> Result<Commits, NodeError>
where
NodeError: From<S::Error>,
{
let mut commits = Commits {
by_change: BTreeMap::new(),
made: BTreeMap::new(),
};
let mut first_parts = Vec::with_capacity(revisions.len());
for &revision in revisions.keys() {
if let Some(part) = first_part(state, revision)? {
first_parts.push((revision, part.change.map(|change| (change, part.at))));
}
}
for &(revision, change) in first_parts.iter().chain(parts) {
*commits.made.entry(revision).or_default() += 1;
if let Some(change) = change {
commits.by_change.insert(change, revision);
}
}
Ok(commits)
}
struct Told {
by_change: BTreeMap<(Hash, Timestamp), BTreeMap<EntityId, Said>>,
paths: BTreeMap<EntityId, Vec<(Timestamp, String)>>,
parts: Vec<(EntityId, Option<(Hash, Timestamp)>)>,
}
fn told_by_changes<S: Statements>(
state: &State<S>,
tree: EntityId,
entities: &[EntityId],
revisions: &BTreeMap<EntityId, Revision>,
) -> Result<Told, NodeError>
where
NodeError: From<S::Error>,
{
let storage = state.storage();
let mut told = Told {
by_change: BTreeMap::new(),
paths: BTreeMap::new(),
parts: Vec::new(),
};
let mut part = |recorded: &nibkeep_model::storage::Recorded<'_>| {
if recorded.statement.field == COMMITTED_IN
&& let Value::Reference(revision) = recorded.statement.value
{
let change = recorded.change.map(|change| (change, recorded.at));
told.parts.push((revision, change));
}
};
for id in storage.of_field(tree, &COMMITTED_IN)? {
if let Some(recorded) = storage.get(id)? {
part(&recorded);
}
}
for &entity in entities {
if revisions.contains_key(&entity) {
continue;
}
for (field, ids) in storage.of_entity(entity)? {
for id in ids {
let Some(recorded) = storage.get(id)? else {
continue;
};
part(&recorded);
if field == TREE_PATH
&& let Value::Text(path) = &recorded.statement.value
{
told.paths
.entry(entity)
.or_default()
.push((recorded.at, path.clone()));
}
let Some(change) = recorded.change else {
continue;
};
told.by_change
.entry((change, recorded.at))
.or_default()
.entry(entity)
.or_default()
.fields
.insert(field.clone(), recorded.statement.value.clone());
}
}
}
for list in told.paths.values_mut() {
list.sort();
}
Ok(told)
}
struct NoRevision(EntityId);
impl From<NoRevision> for NodeError {
fn from(NoRevision(revision): NoRevision) -> Self {
Self::NoRevision(revision.to_string())
}
}
fn what_it_did(said: &Said, now: Option<String>, before: Option<String>) -> Option<TreeChange> {
let has = |field: &FieldName| said.fields.contains_key(field);
let path = now?;
let change = if has(&fields::TYPE) {
Change::New
} else if said.fields.get(&fields::DELETED) == Some(&Value::Boolean(true)) {
Change::Deleted
} else if has(&TREE_PATH) {
match before {
Some(from) if from != path => Change::Moved { from },
_ => Change::Edited,
}
} else if has(&CONTENT) || has(&SECRET_CONTENT) || has(&SIZE) || has(&LINK_TARGET) {
Change::Edited
} else if let Some(Value::Boolean(executable)) = said.fields.get(&EXECUTABLE) {
Change::Executable(*executable)
} else {
return None;
};
Some(TreeChange { path, change })
}
fn revisions_of<S: Statements>(
stored: &nibkeep_store::StoredLog,
state: &State<S>,
tree: EntityId,
) -> Result<BTreeMap<EntityId, Revision>, NodeError>
where
NodeError: From<S::Error>,
{
let tree = state.canonical(tree)?;
let mut revisions = BTreeMap::new();
for entity in stored.find(&IN_TREE, &Value::Reference(tree))? {
let entity = state.canonical(entity)?;
if !state.types(entity)?.contains(&REVISION) {
continue;
}
let first_value = |field: &FieldName| -> Result<Option<Value>, NodeError> {
let mut current = state.current(entity, field)?;
current.sort_by_key(|statement| statement.id);
Ok(current.first().map(|statement| statement.value.clone()))
};
let message = match first_value(&MESSAGE)? {
Some(Value::Text(message)) => Some(message),
_ => None,
};
let parts = match first_value(&PARTS)? {
Some(Value::Integer(parts)) => u64::try_from(parts).ok(),
_ => None,
};
revisions.insert(entity, Revision { message, parts });
}
Ok(revisions)
}
fn first_part<S: Statements>(
state: &State<S>,
revision: EntityId,
) -> Result<Option<PartHere>, NodeError>
where
NodeError: From<S::Error>,
{
let Some(id) = state
.storage()
.of_field(revision, &fields::TYPE)?
.into_iter()
.min()
else {
return Ok(None);
};
Ok(state.storage().get(id)?.map(|recorded| PartHere {
statement: id,
at: recorded.at,
change: recorded.change,
restated: recorded.restated.is_some(),
}))
}
pub(super) fn parts_here<S: Statements>(
state: &State<S>,
revision: EntityId,
) -> Result<Vec<PartHere>, NodeError>
where
NodeError: From<S::Error>,
{
let storage = state.storage();
let mut parts = Vec::new();
for id in storage.referring_to(revision)? {
if let Some(recorded) = storage.get(id)?
&& recorded.statement.field == COMMITTED_IN
{
parts.push(PartHere {
statement: id,
at: recorded.at,
change: recorded.change,
restated: recorded.restated.is_some(),
});
}
}
Ok(parts)
}
fn before_commits_under_way<S: Statements>(
stored: &nibkeep_store::StoredLog,
state: &State<S>,
tree: EntityId,
millis: u64,
) -> Result<u64, NodeError>
where
NodeError: From<S::Error>,
{
let mut commits: Vec<Vec<u64>> = Vec::new();
for &revision in revisions_of(stored, state, tree)?.keys() {
let parts: Vec<u64> = first_part(state, revision)?
.into_iter()
.chain(parts_here(state, revision)?)
.map(|part| part.at.millis())
.collect();
commits.push(parts);
}
let mut moment = millis;
loop {
let under_way = commits
.iter()
.filter(|parts| {
parts.iter().any(|&at| at <= moment) && parts.iter().any(|&at| at > moment)
})
.filter_map(|parts| parts.iter().min().copied())
.min();
match under_way {
Some(first) if first > 0 => moment = first - 1,
Some(_) => return Ok(0),
None => return Ok(moment),
}
}
}
fn is_revision_of<S: Statements>(
state: &State<S>,
revision: EntityId,
tree: EntityId,
) -> Result<bool, NodeError>
where
NodeError: From<S::Error>,
{
let tree = state.canonical(tree)?;
if !state.types(revision)?.contains(&REVISION) {
return Ok(false);
}
for statement in state.current(revision, &IN_TREE)? {
if let Value::Reference(of) = statement.value
&& state.canonical(of)? == tree
{
return Ok(true);
}
}
Ok(false)
}
fn type_change<S: Statements>(
state: &State<S>,
entity: EntityId,
) -> Result<Option<ChangeId>, NodeError>
where
NodeError: From<S::Error>,
{
Ok(first_part(state, entity)?.and_then(|part| part.change.map(ChangeId::from_hash)))
}
fn restated_by_purges(open: &Space) -> Result<BTreeMap<ChangeId, Vec<Operation>>, NodeError> {
let log = open.stored.log();
let mut restated: BTreeMap<ChangeId, Vec<Operation>> = BTreeMap::new();
for change in log.changes()? {
if change.purges().is_empty() {
continue;
}
if let Ok(Some(payload)) = log.payload(change.id(), &open.keys) {
for (of, again) in payload.restated {
let sealed = opened(log, again.sealed.keys());
let operations = restated.entry(of).or_default();
operations.extend(again.operations);
operations.extend(sealed);
}
}
}
Ok(restated)
}
fn opened<'a>(
log: &nibkeep_log::Log<nibkeep_store::StateFile, nibkeep_store::LogFile>,
sealed: impl Iterator<Item = &'a StatementId>,
) -> Vec<Operation> {
let storage = log.state().storage();
sealed
.filter_map(|id| storage.get(*id).ok().flatten())
.map(|recorded| Operation::assert(recorded.statement.into_owned()))
.collect()
}
pub(super) fn split_lines(text: &str) -> Vec<String> {
text.lines().map(str::to_owned).collect()
}
fn carried(
before: &str,
after: &str,
blamed: &[(String, Hash, u64)],
made: (Hash, u64),
) -> Vec<(String, Hash, u64)> {
let patch = diffy::DiffOptions::new()
.set_context_len(usize::MAX)
.create_patch(before, after);
let Some(hunk) = patch.hunks().first() else {
return blamed.to_vec();
};
let mut old = blamed.iter();
let mut lines = Vec::new();
for line in hunk.lines() {
match line {
diffy::Line::Context(text) => {
let (_, change, at) =
old.next()
.cloned()
.unwrap_or((String::new(), made.0, made.1));
lines.push((trimmed(text), change, at));
}
diffy::Line::Delete(_) => {
old.next();
}
diffy::Line::Insert(text) => lines.push((trimmed(text), made.0, made.1)),
}
}
lines
}
fn trimmed(line: &str) -> String {
line.trim_end_matches(['\n', '\r']).to_owned()
}