Skip to content

Commit 6fede37

Browse files
committed
feat: Add Change::from_file_name_and_content for when you don't want to load directly from the file system.
1 parent 279600f commit 6fede37

1 file changed

Lines changed: 20 additions & 6 deletions

File tree

src/change.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,36 @@ impl Change {
4040
///
4141
/// # Errors
4242
///
43-
/// - If the file cannot be read
44-
/// - If the file does not have a valid name (i.e. it does not end in `.md`)
45-
/// - If the file does not have a valid front matter
46-
/// - If the file does not have a valid versioning info in the front matter
43+
/// - If the file can't be read
44+
/// - If the file doesn't have a valid name (it doesn't end in `.md`)
45+
/// - If the file doesn't have a valid front matter
46+
/// - If the file doesn't have valid versioning info in the front matter
4747
pub fn from_file<T: AsRef<Path>>(path: T) -> Result<Self, LoadingError> {
4848
let path = path.as_ref();
4949
let file_name = path
5050
.file_name()
5151
.ok_or(LoadingError::InvalidFileName)?
5252
.to_string_lossy();
53+
let contents = std::fs::read_to_string(path)?;
54+
Self::from_file_name_and_content(file_name.as_ref(), &contents).map_err(LoadingError::from)
55+
}
56+
57+
/// Given the name of a file and its content, create a [`Change`].
58+
///
59+
/// # Errors
60+
///
61+
/// - If the file doesn't have a valid name (it doesn't end in `.md`)
62+
/// - If the file doesn't have a valid front matter
63+
/// - If the file doesn't have valid versioning info in the front matter
64+
pub fn from_file_name_and_content(
65+
file_name: &str,
66+
content: &str,
67+
) -> Result<Self, LoadingError> {
5368
let unique_id = file_name
5469
.strip_suffix(".md")
5570
.ok_or(LoadingError::InvalidFileName)?
5671
.into();
57-
let contents = std::fs::read_to_string(path)?;
58-
Self::from_str(unique_id, &contents).map_err(LoadingError::from)
72+
Self::from_str(unique_id, content).map_err(LoadingError::from)
5973
}
6074

6175
fn from_str(unique_id: UniqueId, content: &str) -> Result<Self, ParsingError> {

0 commit comments

Comments
 (0)