Skip to content

Commit

Permalink
add from string option
Browse files Browse the repository at this point in the history
  • Loading branch information
JR-1991 committed Jun 6, 2024
1 parent 86a5ff4 commit 51eae1d
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/datamodel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,27 @@ impl DataModel {
parse_markdown(&content)
}

/// Parse a markdown file and create a data model
///
/// * `path` - Path to the markdown file
///
/// # Examples
///
/// ```
/// use std::path::Path;
/// use std::fs;
/// use mdmodels::datamodel::DataModel;
///
/// let path = Path::new("tests/data/model.md");
/// let content = fs::read_to_string(path).unwrap();
/// let model = DataModel::from_markdown_string(content.as_str());
/// ```
/// # Returns
/// A data model
pub fn from_markdown_string(content: &str) -> Result<Self, Box<dyn Error>> {
parse_markdown(content)
}

/// Parse a JSON schema and create a data model
///
/// * `path` - Path to the JSON schema file
Expand Down Expand Up @@ -381,4 +402,19 @@ mod tests {
assert_eq!(model.objects.len(), 2);
assert_eq!(model.enums.len(), 1);
}

#[test]
fn test_from_markdown_string() {
// Arrange
let path = Path::new("tests/data/model.md");
let content = fs::read_to_string(path).unwrap();

// Act
let model =
DataModel::from_markdown_string(content.as_str()).expect("Failed to parse markdown");

// Assert
assert_eq!(model.objects.len(), 2);
assert_eq!(model.enums.len(), 1);
}
}

0 comments on commit 51eae1d

Please sign in to comment.