use crate::error::NbError;
const NOTEBOOK_FIELD_MESSAGE: &str = "Invalid `notebook`: use a bare notebook name only. Use `folder` for folder paths and `id`/`selector` for note selectors.";
const FOLDER_FIELD_MESSAGE: &str = "Invalid folder path: use `folder` for folder paths only, not notebook-qualified selectors. To choose a notebook, use the separate `notebook` field.";
pub(crate) fn validate_notebook_name(name: &str) -> Result<(), NbError> {
if name.trim().is_empty() || name.contains(':') || name.contains('/') || name.contains('\\') {
return Err(NbError::ValidationError {
reason: NOTEBOOK_FIELD_MESSAGE.to_string(),
location: None,
});
}
Ok(())
}
pub(crate) fn validate_folder_option(folder: Option<&str>) -> Result<(), NbError> {
if let Some(path) = folder {
validate_folder_path(path)?;
}
Ok(())
}
pub(crate) fn validate_folder_path(path: &str) -> Result<(), NbError> {
if path.trim().is_empty() || path.contains(':') {
return Err(NbError::ValidationError {
reason: FOLDER_FIELD_MESSAGE.to_string(),
location: None,
});
}
Ok(())
}
pub(crate) fn validate_destination(destination: &str) -> Result<(), NbError> {
if destination.trim().is_empty() || destination.contains(':') {
return Err(NbError::ValidationError {
reason:
"Invalid destination: use a folder path or filename only, not a notebook-qualified selector."
.to_string(),
location: None,
});
}
Ok(())
}
pub(crate) fn parse_qualified_selector(selector: &str) -> Result<Option<(&str, &str)>, NbError> {
let Some((notebook, path)) = selector.split_once(':') else {
return Ok(None);
};
validate_notebook_name(notebook)?;
if path.trim().is_empty() || path.contains(':') {
return Err(NbError::ValidationError {
reason:
"Invalid selector: use at most one notebook qualifier, as `<notebook>:<folder>/<id>`."
.to_string(),
location: None,
});
}
Ok(Some((notebook, path)))
}
pub(crate) fn detect_duplicate_title_heading(title: &str, content: &str) -> Option<String> {
let trimmed_title = title.trim();
if trimmed_title.is_empty() {
return None;
}
let first_nonblank = content.lines().find(|line| !line.trim().is_empty())?;
let bytes = first_nonblank.as_bytes();
let leading_spaces = bytes.iter().take_while(|&&b| b == b' ').count();
if leading_spaces >= 4 {
return None;
}
let after_indent = &first_nonblank[leading_spaces..];
if !after_indent.starts_with('#') {
return None;
}
if after_indent.starts_with("##") {
return None;
}
let after_hash = &after_indent[1..];
let after_hash_bytes = after_hash.as_bytes();
let rest = match after_hash_bytes.first() {
Some(b' ') | Some(b'\t') => &after_hash[1..],
None => "",
Some(_) => return None,
};
let heading_text = strip_atx_closing_hashes(rest);
if heading_text.trim() == trimmed_title {
Some(first_nonblank.to_string())
} else {
None
}
}
pub(crate) fn strip_atx_closing_hashes(s: &str) -> String {
let bytes = s.as_bytes();
let n = bytes.len();
let mut end = n;
while end > 0 && (bytes[end - 1] == b' ' || bytes[end - 1] == b'\t') {
end -= 1;
}
if end == 0 || bytes[end - 1] != b'#' {
return s.to_string();
}
let mut hash_start = end;
while hash_start > 0 && bytes[hash_start - 1] == b'#' {
hash_start -= 1;
}
if hash_start == 0 {
return s[..0].to_string();
}
let prev = bytes[hash_start - 1];
if prev != b' ' && prev != b'\t' {
return s.to_string();
}
s[..hash_start].to_string()
}