Skip to content

Commit

Permalink
fix(Documents): Complete implementation of dump method
Browse files Browse the repository at this point in the history
  • Loading branch information
nokome committed Jun 7, 2021
1 parent c76a052 commit b672a95
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
8 changes: 7 additions & 1 deletion node/src/documents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,14 @@ pub fn write(mut cx: FunctionContext) -> JsResult<JsUndefined> {
/// Dump a document
pub fn dump(mut cx: FunctionContext) -> JsResult<JsString> {
let id = &cx.argument::<JsString>(0)?.value(&mut cx);
let format = cx.argument::<JsString>(1)?.value(&mut cx);
let format = if format.is_empty() {
None
} else {
Some(format)
};
let documents = &mut *obtain(&mut cx)?;
let result = documents.dump(id);
let result = RUNTIME.block_on(async { documents.dump(id, format).await });
to_string_or_throw(cx, result)
}

Expand Down
6 changes: 3 additions & 3 deletions node/src/documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ export function write(id: string, content: string): string {
*
* @param id Id of the document
*/
export function dump(id: string): string {
return addon.documentsDump(id)
export function dump(id: string, format?: string): string {
return addon.documentsDump(id, format ?? '')
}

/**
Expand Down Expand Up @@ -140,6 +140,6 @@ export function unsubscribe(id: string, topics: string[]): void {
*
* @param id Id of the document
*/
export function close(id: string): void {
export function close(id: string): void {
addon.documentsClose(id)
}
24 changes: 11 additions & 13 deletions rust/src/documents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,23 +262,21 @@ impl Document {
}

/// Dump the document's content to a string in its current, or
// a different, format
/// a different, format
///
/// # Arguments
///
/// - `format`: the format to dump the content as; if not supplied assumed to be
/// the document's existing format.
pub fn dump(&self, format: Option<String>) -> Result<String> {
let content = if let Some(format) = format {
if format == "json" {
serde_json::to_string(&self.root)?
} else {
"TODO".into()
}
} else {
self.content.clone()
async fn dump(&self, format: Option<String>) -> Result<String> {
let format = match format {
Some(format) => format,
None => return Ok(self.content.clone()),
};
Ok(content)
if let Some(root) = &self.root {
return encode(root, &format).await;
}
bail!("Document has no root node")
}

/// Load content into the document
Expand Down Expand Up @@ -563,8 +561,8 @@ impl Documents {
self.get(&id)?.write(content, None).await
}

pub fn dump(&mut self, id: &str) -> Result<String> {
self.get(&id)?.dump(None)
pub async fn dump(&mut self, id: &str, format: Option<String>) -> Result<String> {
self.get(&id)?.dump(format).await
}

pub async fn load(&mut self, id: &str, content: String) -> Result<()> {
Expand Down

0 comments on commit b672a95

Please sign in to comment.