Skip to content

Commit

Permalink
Include examples of operations handling in docs (#542)
Browse files Browse the repository at this point in the history
  • Loading branch information
djmitche authored Jan 30, 2025
1 parent 8349fda commit 0bcaa4f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
31 changes: 30 additions & 1 deletion src/crate-doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,36 @@ Replica synchronization takes place against a server.
Create a server with [`ServerConfig`].

The [`server`] module defines the interface a server must meet.
Users can define their own server impelementations.
Several server implementations are included, and users can define their own implementations.

# Example

```rust
# use taskchampion::{storage::AccessMode, ServerConfig, Replica, StorageConfig};
# use tempfile::TempDir;
# fn main() -> anyhow::Result<()> {
# let taskdb_dir = TempDir::new()?;
# let taskdb_dir = taskdb_dir.path().to_path_buf();
# let server_dir = TempDir::new()?;
# let server_dir = server_dir.path().to_path_buf();
// Create a new Replica, storing data on disk.
let storage = StorageConfig::OnDisk {
taskdb_dir,
create_if_missing: true,
access_mode: AccessMode::ReadWrite,
}.into_storage()?;
let mut replica = Replica::new(storage);

// Set up a local, on-disk server.
let server_config = ServerConfig::Local { server_dir };
let mut server = server_config.into_server()?;

// Sync to that server.
replica.sync(&mut server, true)?;
#
# Ok(())
# }
```

# Feature Flags

Expand Down
28 changes: 27 additions & 1 deletion src/replica.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,33 @@ use uuid::Uuid;
/// take an argument of type `&mut Operations`, and the necessary operations are added to that
/// sequence. Those changes may be reflected locally, such as in a [`Task`] or [`TaskData`] value, but
/// are not reflected in the Replica's storage until committed with [`Replica::commit_operations`].
///
/**
```rust
# use taskchampion::chrono::{TimeZone, Utc};
# use taskchampion::{storage::AccessMode, Operations, Replica, Status, StorageConfig, Uuid};
# use tempfile::TempDir;
# fn main() -> anyhow::Result<()> {
# let tmp_dir = TempDir::new()?;
# let mut replica = Replica::new(StorageConfig::OnDisk {
# taskdb_dir: tmp_dir.path().to_path_buf(),
# create_if_missing: true,
# access_mode: AccessMode::ReadWrite,
# }.into_storage()?);
// Create a new task, recording the required operations.
let mut ops = Operations::new();
let uuid = Uuid::new_v4();
let mut t = replica.create_task(uuid, &mut ops)?;
t.set_description("my first task".into(), &mut ops)?;
t.set_status(Status::Pending, &mut ops)?;
t.set_entry(Some(Utc::now()), &mut ops)?;
// Commit those operations to storage.
replica.commit_operations(ops)?;
#
# Ok(())
# }
```
**/
/// Undo is supported by producing an [`Operations`] value representing the operations to be
/// undone. These are then committed with [`Replica::commit_reversed_operations`].
///
Expand Down

0 comments on commit 0bcaa4f

Please sign in to comment.