Skip to main content

max / supernote-push

1.7 KB · 85 lines History Blame Raw
1 use std::time::Duration;
2
3 use crate::{Result, browse};
4
5 #[derive(Debug, Clone)]
6 pub struct Supernote {
7 host: String,
8 port: u16,
9 passcode: Option<String>,
10 timeout: Duration,
11 }
12
13 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
14 pub enum Reachability {
15 Up,
16 WifiTransferOff,
17 Unreachable,
18 }
19
20 #[derive(Debug, Clone)]
21 pub struct Entry {
22 pub name: String,
23 pub is_dir: bool,
24 pub size: u64,
25 }
26
27 impl Supernote {
28 pub fn at(host: impl Into<String>) -> Self {
29 Self {
30 host: host.into(),
31 port: 8089,
32 passcode: None,
33 timeout: Duration::from_secs(5),
34 }
35 }
36
37 #[must_use]
38 pub fn with_port(mut self, port: u16) -> Self {
39 self.port = port;
40 self
41 }
42
43 #[must_use]
44 pub fn with_passcode(mut self, passcode: impl Into<String>) -> Self {
45 self.passcode = Some(passcode.into());
46 self
47 }
48
49 #[must_use]
50 pub fn with_timeout(mut self, timeout: Duration) -> Self {
51 self.timeout = timeout;
52 self
53 }
54
55 pub fn host(&self) -> &str {
56 &self.host
57 }
58
59 pub fn reachable(&self) -> Reachability {
60 browse::probe(&self.host, self.port, self.timeout)
61 }
62
63 pub fn push_pdf(&self, remote_dir: &str, filename: &str, bytes: &[u8]) -> Result<()> {
64 browse::upload(
65 &self.host,
66 self.port,
67 self.passcode.as_deref(),
68 remote_dir,
69 filename,
70 bytes,
71 self.timeout,
72 )
73 }
74
75 pub fn list(&self, remote_dir: &str) -> Result<Vec<Entry>> {
76 browse::list(
77 &self.host,
78 self.port,
79 self.passcode.as_deref(),
80 remote_dir,
81 self.timeout,
82 )
83 }
84 }
85