openbutler-protocol 0.0.1

The wire contract between the OpenButler daemon and its clients: frame encoding, action envelopes, and the shared state types.
Documentation
//! The client-side backend record: where a client points, and who it signs in
//! as.
//!
//! This file never holds a credential. The session token lives in a separate
//! private file, so this record can be read, copied, diffed, and printed
//! without exposing anything. The split is the point: a target is
//! shareable, a token is not.
//!
//! Every client (CLI, web, mobile) stores its own copy of this shape: a phone
//! cannot read a laptop's file and must not need to. What is shared is the
//! shape itself, defined here so the clients cannot drift apart. The daemon
//! never reads it.

use serde::{Deserialize, Serialize};

/// The record's current `version` value.
pub const BACKEND_TARGET_VERSION: u32 = 1;

/// One stored backend: the address the client dials and the account it signs
/// in as.
///
/// Ranges the writer is responsible for: `host` is an address or name of
/// 1..=255 bytes, `port` is 1..=65535, `username` and `device` are 1..=64 JS
/// string length units. `saved_at` is the client's own stamp (ISO-8601,
/// millisecond, `Z`) and is informational.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BackendTarget {
    /// [`BACKEND_TARGET_VERSION`]; a newer client may write a higher one.
    pub version: u32,
    /// The daemon's address (a tailnet IP, a loopback IP, or a name).
    pub host: String,
    /// The daemon's TCP port.
    pub port: u16,
    /// The account username the client signs in with.
    pub username: String,
    /// The client's own name for itself, sent as `login`'s `device` label.
    pub device: String,
    /// When the client wrote the record.
    pub saved_at: String,
}

impl BackendTarget {
    /// Builds a record at the current version.
    pub fn new(
        host: impl Into<String>,
        port: u16,
        username: impl Into<String>,
        device: impl Into<String>,
        saved_at: impl Into<String>,
    ) -> Self {
        Self {
            version: BACKEND_TARGET_VERSION,
            host: host.into(),
            port,
            username: username.into(),
            device: device.into(),
            saved_at: saved_at.into(),
        }
    }
}