Fix missing return type
[tt.git] / tt.rkt
diff --git a/tt.rkt b/tt.rkt
index b99a45e..1cb90cf 100644 (file)
--- a/tt.rkt
+++ b/tt.rkt
   (U 'old->new
      'new->old))
 
-(struct msg
+(struct Msg
         ([ts-epoch   : Integer]
          [ts-orig    : String]
          [nick       : (Option String)]
          [uri        : Url]
          [text       : String]
-         [mentions   : (Listof Feed)])
-        #:type-name Msg)
+         [mentions   : (Listof Peer)]))
 
-(struct feed
+(struct Peer
         ([nick : (Option String)]
-         [uri  : Url])
-        #:type-name Feed)
+         [uri  : Url]))
 
 (: tt-home-dir Path-String)
 (define tt-home-dir (build-path (expand-user-path "~") ".tt"))
 
-(: concurrent-filter-map (∀ (α β) (-> Natural (-> α β) (Listof α))))
+(: concurrent-filter-map (∀ (α β) (-> Natural (-> α β) (Listof α) (Listof β))))
 (define (concurrent-filter-map num-workers f xs)
   ; TODO preserve order of elements OR communicate that reorder is expected
   ; TODO switch from mailboxes to channels
          [n      (vector-length colors)])
     (λ (out-format color-i msg)
        (let ([color (vector-ref colors (modulo color-i n))]
-             [nick  (msg-nick msg)]
-             [uri   (url->string (msg-uri msg))]
-             [text  (msg-text msg)]
-             [mentions (msg-mentions msg)])
+             [nick  (Msg-nick msg)]
+             [uri   (url->string (Msg-uri msg))]
+             [text  (Msg-text msg)]
+             [mentions (Msg-mentions msg)])
          (match out-format
            ['single-line
             (let ([nick (if nick nick uri)])
               (printf "~a  \033[1;37m<~a>\033[0m  \033[0;~am~a\033[0m~n"
                       (parameterize
                         ([date-display-format 'iso-8601])
-                        (date->string (seconds->date [msg-ts-epoch msg]) #t))
+                        (date->string (seconds->date (Msg-ts-epoch msg)) #t))
                       nick color text))]
            ['multi-line
             (let ([nick (if nick (string-append nick " ") "")])
               (printf "~a (~a)~n\033[1;37m<~a~a>\033[0m~n\033[0;~am~a\033[0m~n~n"
                       (parameterize
                         ([date-display-format 'rfc2822])
-                        (date->string (seconds->date [msg-ts-epoch msg]) #t))
-                      (msg-ts-orig msg)
+                        (date->string (seconds->date (Msg-ts-epoch msg)) #t))
+                      (Msg-ts-orig msg)
                       nick uri color text))])))))
 
 (: rfc3339->epoch (-> String (Option Nonnegative-Integer)))
                           (filter-map
                             (λ (m) (match (regexp-match #px"@<([^>]+)>" m)
                                      [(list _wholething nick-uri)
-                                      (str->feed nick-uri)]))
+                                      (str->peer nick-uri)]))
                             (regexp-match* #px"@<[^\\s]+([\\s]+)?[^>]+>" text))])
-                    (msg ts-epoch ts-orig nick uri text mentions))
+                    (Msg ts-epoch ts-orig nick uri text mentions))
                   (begin
                     (log-error
                       "Msg rejected due to invalid timestamp: ~v, nick:~v, uri:~v"
                                     z)]
                  [m  (str->msg n u (string-append ts sep txt))])
             (check-not-false m)
-            (check-equal? (msg-nick m) n)
-            (check-equal? (msg-uri m) u)
-            (check-equal? (msg-text m) txt)
-            (check-equal? (msg-ts-orig m) ts (format "Given: ~v" ts))
+            (check-equal? (Msg-nick m) n)
+            (check-equal? (Msg-uri m) u)
+            (check-equal? (Msg-text m) txt)
+            (check-equal? (Msg-ts-orig m) ts (format "Given: ~v" ts))
             )))
 
   (let* ([ts       "2020-11-18T22:22:09-0500"]
          [nick     "foo"]
          [uri      "bar"]
          [actual   (str->msg nick uri (string-append ts tab text))]
-         [expected (msg 1605756129 ts nick uri text)])
+         [expected (Msg 1605756129 ts nick uri text)])
     (check-equal?
-      (msg-ts-epoch actual)
-      (msg-ts-epoch expected)
+      (Msg-ts-epoch actual)
+      (Msg-ts-epoch expected)
       "str->msg ts-epoch")
     (check-equal?
-      (msg-ts-orig actual)
-      (msg-ts-orig expected)
+      (Msg-ts-orig actual)
+      (Msg-ts-orig expected)
       "str->msg ts-orig")
     (check-equal?
-      (msg-nick actual)
-      (msg-nick expected)
+      (Msg-nick actual)
+      (Msg-nick expected)
       "str->msg nick")
     (check-equal?
-      (msg-uri actual)
-      (msg-uri expected)
+      (Msg-uri actual)
+      (Msg-uri expected)
       "str->msg uri")
     (check-equal?
-      (msg-text actual)
-      (msg-text expected)
+      (Msg-text actual)
+      (Msg-text expected)
       "str->msg text")))
 
 (: str->lines (-> String (Listof String)))
         (log-warning "Cache file not found for URI: ~a" (url->string uri))
         "")))
 
-(: str->feed (String (Option Feed)))
-(define (str->feed str)
-  (log-debug "Parsing feed string: ~v" str)
+(: str->peer (String (Option Peer)))
+(define (str->peer str)
+  (log-debug "Parsing peer string: ~v" str)
   (with-handlers*
     ([exn:fail?
        (λ (e)
           (log-error "Invalid URI in string: ~v, exn: ~v" str e)
           #f)])
     (match (string-split str)
-      [(list u)   (feed #f  (string->url u))]
-      [(list n u) (feed  n  (string->url u))]
+      [(list u)   (Peer #f  (string->url u))]
+      [(list n u) (Peer  n  (string->url u))]
       [_
-        (log-error "Invalid feed string: ~v" str)
+        (log-error "Invalid peer string: ~v" str)
         #f])))
 
 
 (define (filter-comments lines)
   (filter-not (λ (line) (string-prefix? line "#")) lines))
 
-(: str->feeds (-> String (Listof Feed)))
-(define (str->feeds str)
-  (filter-map str->feed (filter-comments (str->lines str))))
+(: str->peers (-> String (Listof Peer)))
+(define (str->peers str)
+  (filter-map str->peer (filter-comments (str->lines str))))
 
-(: file->feeds (-> Path-String (Listof Feed)))
-(define (file->feeds filename)
-  (str->feeds (file->string filename)))
+(: file->peers (-> Path-String (Listof Peer)))
+(define (file->peers filename)
+  (str->peers (file->string filename)))
 
 (: user-agent String)
 (define user-agent
     ([prog-name      "tt"]
      [prog-version   (info:#%info-lookup 'version)]
      [prog-uri       "https://github.com/xandkar/tt"]
-     [user-feed-file (build-path tt-home-dir "me")]
+     [user-peer-file (build-path tt-home-dir "me")]
      [user
-       (if (file-exists? user-feed-file)
-           (match (first (file->feeds user-feed-file))
-             [(feed #f u) (format "+~a"      (url->string u)  )]
-             [(feed  n u) (format "+~a; @~a" (url->string u) n)])
+       (if (file-exists? user-peer-file)
+           (match (first (file->peers user-peer-file))
+             [(Peer #f u) (format "+~a"      (url->string u)  )]
+             [(Peer  n u) (format "+~a; @~a" (url->string u) n)])
            (format "+~a" prog-uri))])
     (format "~a/~a (~a)" prog-name prog-version user)))
 
 (: timeline-print (-> Out-Format (Listof Msg) Void))
 (define (timeline-print out-format timeline)
   (void (foldl (match-lambda**
-                 [((and m (msg _ _ nick _ _ _)) (cons prev-nick i))
+                 [((and m (Msg _ _ nick _ _ _)) (cons prev-nick i))
                   (let ([i (if (equal? prev-nick nick) i (+ 1 i))])
                     (msg-print out-format i m)
                     (cons nick i))])
                (cons "" 0)
                timeline)))
 
-(: feed->msgs (-> Feed (Listof Msg)))
-(define (feed->msgs f)
-  (match-define (feed nick uri) f)
-  (log-info "Reading feed nick:~v uri:~v" nick (url->string uri))
+(: peer->msgs (-> Peer (Listof Msg)))
+(define (peer->msgs f)
+  (match-define (Peer nick uri) f)
+  (log-info "Reading peer nick:~v uri:~v" nick (url->string uri))
   (str->msgs nick uri (uri-read-cached uri)))
 
-(: feed-download (-> Feed Void))
-(define (feed-download f)
-  (match-define (feed nick uri) f)
+(: peer-download (-> Peer Void))
+(define (peer-download f)
+  (match-define (Peer nick uri) f)
   (define u (url->string uri))
-  (log-info "Downloading feed uri:~a" u)
+  (log-info "Downloading peer uri:~a" u)
   (with-handlers
     ([exn:fail?
        (λ (e)
           #f)])
     (define-values (_result _tm-cpu-ms tm-real-ms _tm-gc-ms)
       (time-apply uri-download (list uri)))
-    (log-info "Feed downloaded in ~a seconds, uri: ~a" (/ tm-real-ms 1000.0) u)))
+    (log-info "Peer downloaded in ~a seconds, uri: ~a" (/ tm-real-ms 1000.0) u)))
 
-(: timeline-download (-> Integer (Listof Feed) Void))
-(define (timeline-download num-workers feeds)
+(: timeline-download (-> Integer (Listof Peer) Void))
+(define (timeline-download num-workers peers)
   ; TODO No need for map - can just iter
-  (void (concurrent-filter-map num-workers feed-download feeds)))
+  (void (concurrent-filter-map num-workers peer-download peers)))
 
 ; TODO timeline contract : time-sorted list of messages
-(: timeline-read (-> Timeline-Order (Listof Feed) (Listof Msg)))
-(define (timeline-read order feeds)
+(: timeline-read (-> Timeline-Order (Listof Peer) (Listof Msg)))
+(define (timeline-read order peers)
   (define cmp (match order
                 ['old->new <]
                 ['new->old >]))
-  (sort (append* (filter-map feed->msgs feeds))
-        (λ (a b) (cmp (msg-ts-epoch a) (msg-ts-epoch b)))))
+  (sort (append* (filter-map peer->msgs peers))
+        (λ (a b) (cmp (Msg-ts-epoch a) (Msg-ts-epoch b)))))
 
 (: log-writer-stop (-> Thread Void))
 (define (log-writer-stop log-writer)
               (set! num-workers (string->number njobs))]
              #:args (filename)
              (define-values (_res _cpu real-ms _gc)
-               (time-apply timeline-download (list num-workers (file->feeds filename))))
+               (time-apply timeline-download (list num-workers (file->peers filename))))
              (log-info "Timeline downloaded in ~a seconds." (/ real-ms 1000.0))
              (log-writer-stop log-writer)))]
         [(or "u" "upload")
               "Long output format"
               (set! out-format 'multi-line)]
              #:args (filename)
-             (timeline-print out-format (timeline-read order (file->feeds filename)))))]
+             (timeline-print out-format (timeline-read order (file->peers filename)))))]
         ))))
This page took 0.037838 seconds and 4 git commands to generate.